1、在setup中創建不可變變量。各個用例和teardown都可以使用,但是不能修改變量。
示例如下
#coding:utf-8 import pytest class Test_share_var(object): def setup_class(self): print("\nhere is setup_class") self.driver = "driver_setup" # setup中創建變量 def test_step1(self): print("\nhere is test_step1") self.driver = "driver_step1" # 用例中修改變量 def test_step2(self): print("\nhere is test_step2") print("driver:{}".format(self.driver)) # 用例中引用變量 def teardown_class(self): print("\nhere is teardown_class") print("driver:{}".format(self.driver)) # teardown中引用變量 if __name__=="__main__": args = ["share_var.py::Test_share_var","-sv"] pytest.main(args)
結果如下:
python share_var.py =================================================================== test session starts ==================================================================== platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- C:\Users\wangwuhui\AppData\Local\Programs\Python\Python38\python.exe cachedir: .pytest_cache metadata: {'Python': '3.8.10', 'Platform': 'Windows-10-10.0.18363-SP0', 'Packages': {'pytest': '6.2.5', 'py': '1.11.0', 'pluggy': '1.0.0'}, 'Plugins': {'allure-pytest': '2.9.45', 'dependency': '0.5.1', 'html': '3.1.1', 'metadata': '1.11.0', 'mock': '3.7.0', 'rerunfailures': '10.2'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_301'} rootdir: C:\Users\wangwuhui\Desktop plugins: allure-pytest-2.9.45, dependency-0.5.1, html-3.1.1, metadata-1.11.0, mock-3.7.0, rerunfailures-10.2 collected 2 items share_var.py::Test_share_var::test_step1 here is setup_class here is test_step1 PASSED share_var.py::Test_share_var::test_step2 here is test_step2 driver:driver_setup # 可以引用變量。可以看到值為driver_setup,說明test_step1修改變量值沒有成功。 PASSED here is teardown_class driver:driver_setup # 可以引用變量。 ==================================================================== 2 passed in 0.04s =====================================================================
2、在用例中創建不可變變量,不能共享給其它用例使用。
示例如下
#coding:utf-8 import pytest class Test_share_var1(object): def setup_class(self): print("\nhere is setup_class") def test_step1(self): print("\nhere is test_step1") self.var_step1 = "var_step1" # 在用例中創建變量 def test_step2(self): print("\nhere is test_step2") print("var_step1:{}".format(self.var_step1)) # 在用例中引用變量 def teardown_class(self): print("\nhere is teardown_class") print("var_step1:{}".format(self.var_step1)) if __name__=="__main__": args = ["share_var.py::Test_share_var1","-sv"] pytest.main(args)
結果如下:
python share_var.py =================================================================== test session starts ==================================================================== platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- C:\Users\wangwuhui\AppData\Local\Programs\Python\Python38\python.exe cachedir: .pytest_cache metadata: {'Python': '3.8.10', 'Platform': 'Windows-10-10.0.18363-SP0', 'Packages': {'pytest': '6.2.5', 'py': '1.11.0', 'pluggy': '1.0.0'}, 'Plugins': {'allure-pytest': '2.9.45', 'dependency': '0.5.1', 'html': '3.1.1', 'metadata': '1.11.0', 'mock': '3.7.0', 'rerunfailures': '10.2'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_301'} rootdir: C:\Users\wangwuhui\Desktop plugins: allure-pytest-2.9.45, dependency-0.5.1, html-3.1.1, metadata-1.11.0, mock-3.7.0, rerunfailures-10.2 collected 2 items share_var.py::Test_share_var1::test_step1 here is setup_class here is test_step1 PASSED share_var.py::Test_share_var1::test_step2 here is test_step2 FAILED here is teardown_class share_var.py::Test_share_var1::test_step2 ERROR ========================================================================== ERRORS ========================================================================== _____________________________________________________ ERROR at teardown of Test_share_var1.test_step2 ______________________________________________________ self = <class 'share_var.Test_share_var1'> def teardown_class(self): print("\nhere is teardown_class") > print("var_step1:{}".format(self.var_step1)) E AttributeError: type object 'Test_share_var1' has no attribute 'var_step1' share_var.py:30: AttributeError ========================================================================= FAILURES ========================================================================= ________________________________________________________________ Test_share_var1.test_step2 ________________________________________________________________ self = <share_var.Test_share_var1 object at 0x000001B8B0274070> def test_step2(self): print("\nhere is test_step2") > print("var_step1:{}".format(self.var_step1)) E AttributeError: 'Test_share_var1' object has no attribute 'var_step1' share_var.py:27: AttributeError ================================================================= short test summary info ================================================================== FAILED share_var.py::Test_share_var1::test_step2 - AttributeError: 'Test_share_var1' object has no attribute 'var_step1' ERROR share_var.py::Test_share_var1::test_step2 - AttributeError: type object 'Test_share_var1' has no attribute 'var_step1' =========================================================== 1 failed, 1 passed, 1 error in 0.51s ===========================================================
報錯信息
AttributeError: type object 'Test_share_var1' has no attribute 'var_step1'說明,test_step1中創建的變量不能共享給
在test_step2中引用。
3、在setup中創建可變變量。各個用例和teardown都可以使用,還能修改變量。
示例如下
#coding:utf-8 import pytest class Test_share_var3(object): def setup_class(self): print("\nhere is setup_class") self.inner_var = {"var1":"a","var2":"b"} # 在setup中創建可變變量 def test_step1(self): print("\nhere is test_step1") self.inner_var['var1']="var_step1" # 在test_step1中修改可變變量 def test_step2(self): print("\nhere is test_step2") print("var1:{}".format(self.inner_var['var1'])) # 在test_step2中引用可變變量 print("var2:{}".format(self.inner_var['var2'])) def teardown_class(self): print("\nhere is teardown_class") print("var1:{}".format(self.inner_var['var1'])) # 在teardown中引用可變變量 print("var2:{}".format(self.inner_var['var2'])) if __name__=="__main__": args = ["share_var.py::Test_share_var3","-sv"] pytest.main(args)
結果如下:
python share_var.py =================================================================== test session starts ==================================================================== platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- C:\Users\wangwuhui\AppData\Local\Programs\Python\Python38\python.exe cachedir: .pytest_cache metadata: {'Python': '3.8.10', 'Platform': 'Windows-10-10.0.18363-SP0', 'Packages': {'pytest': '6.2.5', 'py': '1.11.0', 'pluggy': '1.0.0'}, 'Plugins': {'allure-pytest': '2.9.45', 'dependency': '0.5.1', 'html': '3.1.1', 'metadata': '1.11.0', 'mock': '3.7.0', 'rerunfailures': '10.2'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_301'} rootdir: C:\Users\wangwuhui\Desktop plugins: allure-pytest-2.9.45, dependency-0.5.1, html-3.1.1, metadata-1.11.0, mock-3.7.0, rerunfailures-10.2 collected 2 items share_var.py::Test_share_var3::test_step1 here is setup_class here is test_step1 PASSED share_var.py::Test_share_var3::test_step2 here is test_step2 var1:var_step1 # var1的值從a變為var_step1,說明用例可以引用和修改共享的可變變量 var2:b PASSED here is teardown_class var1:var_step1 # 說明teardown可以引用共享的可變變量 var2:b ==================================================================== 2 passed in 0.06s =====================================================================