回顾

pytest框架用例运行级别

>>模块级(setup_module/teardown_module)开始于横块始末,全局的

>>函数级(setup_function/teardown_function)只对函数用例生效(不在类中)

>>类级(setup_class/teardown_calss)只在类中前后运行一次(在类中)

>>方法级(setup_method/teardown_method)开始于方法始末(在类中)

>>类里面的(setup/teardown)运行在调用方法前后

接下来我们进入今天的小猪脚类与方法的setup、teardown

一、类里面的

pytest 框架类里面的前置与后置用法setup、teardown ]

以下代码是类里面的前置后置简要代码,我们一起看看他的执行顺序

# coding=utf-8# authou:shichaoimport pytestclass Testcaselist():# 类里面的def setup(self):print('setup:每个用例前开始执行')def teardown(self):print('teardown:每个用例后开始执行')# 测试用例def test_001(self):print("正在执行第一条用例")p = "Python"assert "h" in pdef test_002(self):print("正在执行第二条用例")p = 'test'assert 't' in pif __name__ == '__main__':pytest.main(['-s', 'test_fixtclass.py'])

以下是代码执行后控制台输出

Testing started at 17:29 ...F:\python3\python_code\venv\Scripts\python.exe "F:\PyCharm\PyCharm Community Edition 2019.2.2\helpers\pycharm\_jb_pytest_runner.py" --path F:/python3/python_code/Study/API_AutoTest_Pytest/test_fixtclass.pyLaunching pytest with arguments F:/python3/python_code/Study/API_AutoTest_Pytest/test_fixtclass.py in F:\python3\python_code\Study\API_AutoTest_Pytest============================= test session starts =============================platform win32 -- Python 3.7.4, pytest-5.4.3, py-1.8.1, pluggy-0.13.1 -- F:\python3\python_code\venv\Scripts\python.execachedir: .pytest_cacherootdir: F:\python3\python_code\Study\API_AutoTest_Pytestcollecting ... collected 2 itemstest_fixtclass.py::Testcaselist::test_001 setup:每个用例前开始执行PASSED [ 50%]正在执行第一条用例teardown:每个用例后开始执行test_fixtclass.py::Testcaselist::test_002 setup:每个用例前开始执行PASSED[100%]正在执行第二条用例teardown:每个用例后开始执行============================== 2 passed in 0.02s ==============================Process finished with exit code 0

>>类里面的setup、teardown控制台输出解析

# 类里面的[可以看到控制台输出的结果执行顺序 ]

test_fixtclass.py:test_001>>setup:每个用例前开始执行>>PASSED>> [ 50%]正在执行第一条用例>>teardown:每个用例后开始执行

test_fixtclass.py:test_002>>setup:每个用例前开始执行>>PASSED>> [100%]正在执行第二条用例>>teardown:每个用例后开始执行

这是我们的类里面的setup、teardown作用对类里的测试用例生效

* 类里面的在每条测试用例执行前都会去执行一次

二、类级

接着我们在看看[ 类级setup_class、teardown_class前置与后置用法 ]

以下代码是类级的前置后置简要代码,我们一起看看他的执行顺序

# coding=utf-8# authou:shichaoimport pytestclass Testcaselist():print('setup_class:所有用例执行之前')# 类级def setup_class(self):print('setup_class:所有用例执行之前')def teardown_class(self):print('teardown_class:所有用例执行结束之后')# 测试用例def test_001(self):print("正在执行第一条用例")p = "Python"assert "h" in pdef test_002(self):print("正在执行第二条用例")p = 'test'assert 't' in pif __name__ == '__main__':pytest.main(['-s', 'test_fixtclass.py'])

以下是代码执行后控制台输出

Testing started at 20:12 ...F:\python3\python_code\venv\Scripts\python.exe "F:\PyCharm\PyCharm Community Edition 2019.2.2\helpers\pycharm\_jb_pytest_runner.py" --path F:/python3/python_code/Study/API_AutoTest_Pytest/test_fixtclass.pyLaunching pytest with arguments F:/python3/python_code/Study/API_AutoTest_Pytest/test_fixtclass.py in F:\python3\python_code\Study\API_AutoTest_Pytest============================= test session starts =============================platform win32 -- Python 3.7.4, pytest-5.4.3, py-1.8.1, pluggy-0.13.1 -- F:\python3\python_code\venv\Scripts\python.execachedir: .pytest_cacherootdir: F:\python3\python_code\Study\API_AutoTest_Pytestcollecting ... collected 2 itemstest_fixtclass.py::Testcaselist::test_001 setup_class:所有用例执行之前PASSED [ 50%]正在执行第一条用例test_fixtclass.py::Testcaselist::test_002 PASSED [100%]正在执行第二条用例teardown_class:所有用例执行结束之后============================== 2 passed in 0.02s ==============================Process finished with exit code 0

>>类级setup_class、teardown_class控制台输出解析

# 类级 [ 我们可以看到控制台输出的结果执行顺序 ]

>>test_fixtclass.py:Testcaselist: >>setup_class:所有用例执行之前>>test_001>>[ 50%]正在执行第一条用例>>PASSED >>test_002>>[100%]正在执行第二条用例>>PASSED >>teardown_class:所有用例执行结束之后

* 类级前置后置只打开一次就执行所有的测试用

三、方法级

接着我们在看看[ 方法级setup_method、teardown_method在一个测试用例文件里一起写前置与后置用法 看看它的执行顺序 ]

以下代码是模块级的前置后置简要代码,我们一起看看他的执行顺序

# coding=utf-8# authou:shichaoimport pytestclass Testcaselist():# 方法级def setup_method(self):print('setup_method:每个用例开始之前执行')def teardown_method(self):print('teardown_method:每个用例结束后执行')# 测试用例def test_001(self):print("正在执行第一条用例")p = "Python"assert "h" in pdef test_002(self):print("正在执行第二条用例")p = 'test'assert 't' in pif __name__ == '__main__':pytest.main(['-s', 'test_fixtclass.py'])

以下是代码执行后控制台输出

Testing started at 17:48 ...F:\python3\python_code\venv\Scripts\python.exe "F:\PyCharm\PyCharm Community Edition 2019.2.2\helpers\pycharm\_jb_pytest_runner.py" --path F:/python3/python_code/Study/API_AutoTest_Pytest/test_fixtclass.pyLaunching pytest with arguments F:/python3/python_code/Study/API_AutoTest_Pytest/test_fixtclass.py in F:\python3\python_code\Study\API_AutoTest_Pytest============================= test session starts =============================platform win32 -- Python 3.7.4, pytest-5.4.3, py-1.8.1, pluggy-0.13.1 -- F:\python3\python_code\venv\Scripts\python.execachedir: .pytest_cacherootdir: F:\python3\python_code\Study\API_AutoTest_Pytestcollecting ... collected 2 itemstest_fixtclass.py::Testcaselist::test_001 setup_method:每个用例开始之前执行PASSED [ 50%]正在执行第一条用例teardown_method:每个用例结束后执行test_fixtclass.py::Testcaselist::test_002 setup_method:每个用例开始之前执行PASSED [100%]正在执行第二条用例teardown_method:每个用例结束后执行============================== 2 passed in 0.02s ==============================Process finished with exit code 0

>>方法级的setup_method、teardown_method控制台输出解析

# 类里面的 [ 可以看到控制台输出的结果执行顺序 ]

test_fixtclass.py :test_001>>setup_method:每个用例开始之前执行>>PASSED>> [ 50%]正在执行第一条用例>>teardown_method:每个用例结束后执行

test_fixtclass.py :test_002>>setup_method:每个用例开始之前执行>>PASSED>> [100%]正在执行第二条用例>>teardown_method:每个用例结束后执行

* 方法级的在每条测试用例执行前都会去执行一次

四、类级+方法级+类里面的

接着我们在看看[ 类级+方法级setup、teardown、setup_class、teardown_class、setup_method、teardown_method在一个测试用例文件里一起写前置与后置用法 看看它的执行顺序 ]

以下代码是类级+模块级的前置后置简要代码,我们一起看看他的执行顺序

# coding=utf-8# authou:shichaoimport pytestclass Testcaselist():# 类里面的def setup(self):print('setup:每个用例前开始执行')def teardown(self):print('teardown:每个用例后开始执行')# 类级def setup_class(self):print('setup_class:所有用例执行之前')def teardown_class(self):print('teardown_class:所有用例执行结束之后')# 方法级def setup_method(self):print('setup_method:每个用例开始之前执行')def teardown_method(self):print('teardown_method:每个用例结束后执行')# 测试用例def test_001(self):print("正在执行第一条用例")p = "Python"assert "h" in pdef test_002(self):print("正在执行第二条用例")p = 'test'assert 't' in pif __name__ == '__main__':pytest.main(['-s', 'test_fixtclass.py'])

以下是代码执行后控制台输出

Testing started at 17:57 ...F:\python3\python_code\venv\Scripts\python.exe "F:\PyCharm\PyCharm Community Edition 2019.2.2\helpers\pycharm\_jb_pytest_runner.py" --path F:/python3/python_code/Study/API_AutoTest_Pytest/test_fixtclass.pyLaunching pytest with arguments F:/python3/python_code/Study/API_AutoTest_Pytest/test_fixtclass.py in F:\python3\python_code\Study\API_AutoTest_Pytest============================= test session starts =============================platform win32 -- Python 3.7.4, pytest-5.4.3, py-1.8.1, pluggy-0.13.1 -- F:\python3\python_code\venv\Scripts\python.execachedir: .pytest_cacherootdir: F:\python3\python_code\Study\API_AutoTest_Pytestcollecting ... collected 2 itemstest_fixtclass.py::Testcaselist::test_001 setup_class:所有用例执行之前setup_method:每个用例开始之前执行setup:每个用例前开始执行PASSED [ 50%]正在执行第一条用例teardown:每个用例后开始执行teardown_method:每个用例结束后执行test_fixtclass.py::Testcaselist::test_002 setup_method:每个用例开始之前执行setup:每个用例前开始执行PASSED [100%]正在执行第二条用例teardown:每个用例后开始执行teardown_method:每个用例结束后执行teardown_class:所有用例执行结束之后============================== 2 passed in 0.02s ==============================Process finished with exit code 0
>>类里面的+类级+方法级前置后置控制台输出解析# 类里面的+类级+方法级 [ 我们可以看到控制台输出的结果执行顺序 ]>>test_fixtclass.py::Testcaselist:setup_class:所有用例执行之前>>比如:所有用例开始前只打开一次浏览器setup:每个用例前开始执行>>setup_method:每个用例开始之前执行>>test_001>>[ 50%]正在执行第一条用例>>PASSED >>teardown:每个用例后开始执行>>teardown_method:每个用例结束后执行setup:每个用例前开始执行>>setup_method:每个用例开始之前执行>>test_002>>[ 50%]正在执行第一条用例>>PASSED >>teardown:每个用例后开始执行>>teardown_method:每个用例结束后执行>>teardown_class:所有用例执行结束之后>>比如:所有用例结束只最后关闭浏览器从结果看出,运行的优先级:setup_class>>setup_method>setup >用例>teardown>teardown_method>>teardown_class

以上就是今天给大家介绍的pytest前置后置[ 类级以及方法级] 的用法以及在实际代码中他们的执行优先级,小小的顺序结构可能会影响你这条case是否执行通过,希望本次分享对大家有所帮助

B站讲的最详细的Python接口自动化测试实战教程全集(实战最新版)