在pytest中有四种setup和teardown,其中setup_module
和teardown_module
在整个测试用例所在的文件中所有的方法运行前和运行后运行,只会运行一次;而setup_class
和setup_class
则在整个文件中的一个class中所有用例的前后运行,setup_method
和teardown_method
在class内的每个方法运行前后运行,而setup_function
、teardown_function
则是在非class下属的每个测试方法的前后运行;
setup_function
、teardown_function
这两个不能在class内部使用,均在方法前后运行;
def setup_function():
print("setup_function")
def teardown_function():
print("teardown_function")
def test_case1():
tof = True
assert tof
def test_case2():
assert False
如下所示的结果,为每个用例前运行setup_function
,方法完成后运行tear_down
;
E:\pyspace\testSimple>pytest -s
========================================================================================================================= test session starts ==========================================================================================================================
platform win32 -- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0
rootdir: E:\pyspace\testSimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 2 items
testcase\Test_simple.py setup_function
.teardown_function
setup_function
Fteardown_function
=============================================================================================================================== FAILURES ===============================================================================================================================
______________________________________________________________________________________________________________________________ test_case2 ______________________________________________________________________________________________________________________________
def test_case2():
> assert False
E assert False
testcase\Test_simple.py:18: AssertionError
================================================================================================================== 1 failed, 1 passed in 0.12 seconds ==================================================================================================================
setup_class
和setup_class
这两个方法在类中使用,且在类运行前和运行后运行,如下所示范例:
#-*- coding: utf-8 -*-
import pytest
class Test_simple():
@pytest.mark.test
def test_case1(self):
tof = True
assert tof
@pytest.mark.normal
@pytest.mark.test
def test_case2(self):
tof = False
assert tof
def setup_class(self):
print("类前运行")
def teardown_class(self):
print("类后运行")
结果如下所示,在两条用例的前后执行了setup_class
和setup_class
platform win32 -- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0
rootdir: E:\pyspace\testSimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 2 items
testcase\Test_simple.py 类前运行
.F类后运行
setup_method
和teardown_method
这两个方法是在类中的函数上使用的,不在类中的函数是不能使用的,此处要和setup_function
和teardown_function
区别开来;
范例:
#-*- coding: utf-8 -*-
import pytest
class Test_simple():
@pytest.mark.test
def test_case1(self):
tof = True
assert tof
@pytest.mark.normal
@pytest.mark.test
def test_case2(self):
tof = False
assert tof
def setup_class(self):
print("类前运行")
def teardown_class(self):
print("类后运行")
def setup_method(self):
print("方法前")
def teardown_method(self):
print("方法后")
运行结果可以看到是在每个测试用例方法前后运行的;
testcase\Test_simple.py 类前运行
方法前
.方法后
方法前
F方法后
类后运行
setup_module
和teardown_module
这两个方法为全局的,即在模块运行前和运行后执行,范例如下:
#-*- coding: utf-8 -*-
import pytest
def setup_module():
print("全局前")
def teardown_module():
print("全局后")
class Test_simple():
@pytest.mark.test
def test_case1(self):
tof = True
assert tof
@pytest.mark.normal
@pytest.mark.test
def test_case2(self):
tof = False
assert tof
def setup_class(self):
print("类前运行")
def teardown_class(self):
print("类后运行")
def setup_method(self):
print("方法前")
def teardown_method(self):
print("方法后")
运行结果:setup_module
和teardown_module
在整个py文件的前后各运行了一次;
platform win32 -- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0
rootdir: E:\pyspace\testSimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 2 items
testcase\Test_simple.py 全局前
类前运行
方法前
.方法后
方法前
F方法后
类后运行
全局后