在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方法後
類後運行
全局後