环境
- ubuntu16.04
- python3.7
- pytest5.4.3
样例1
test_demo1.py 内容
import pytest
def setup_module():
print("\n-------------- setup_module --------------")
def teardown_module():
print("\n-------------- teardown_module --------------")
def setup_function():
print("\n-------------- setup_function --------------")
def teardown_function():
print("\n-------------- teardown_function --------------")
def test_no_class_func1():
print("\n正在执行test_no_class_func1")
assert 1 + 2 == 3
def test_no_class_func2():
print("\n正在执行test_no_class_func2")
assert 1 + 2 == 4
class TestCase(object):
def setup(self):
print("\n-------------- TestCase.setup --------------")
def teardown(self):
print("\n-------------- TestCase.teardown --------------")
def setup_method(self):
print("\n-------------- TestCase.setup_method --------------")
def teardown_method(self):
print("\n-------------- TestCase.teardown_method --------------")
def setup_class(self):
print("\n-------------- TestCase.setup_class --------------")
def teardown_class(self):
print("\n-------------- TestCase.teardown_class --------------")
def test_one(self):
print("\n正在执行----test_one")
x = "he"
assert 'h' in x
def test_two(self):
print("\n正在执行test_two")
assert "she" not in "she is a girl"
def test_three(self):
print("\n正在执行test_tree")
assert self.add(1, 2) == 3
def add(self,a, b):
print("\n这是加减法")
return a + b
结果
...
============================= test session starts ==============================
...
collecting ... collected 5 items
test_demo1.py::test_no_class_func1
-------------- setup_module --------------
-------------- setup_function --------------
PASSED [ 20%]
正在执行test_no_class_func1
-------------- teardown_function --------------
test_demo1.py::test_no_class_func2
-------------- setup_function --------------
FAILED [ 40%]
正在执行test_no_class_func2
test_demo1.py:45 (test_no_class_func2)
3 != 4
Expected :4
Actual :3
<Click to see difference>
def test_no_class_func2():
print("\n正在执行test_no_class_func2")
> assert 1 + 2 == 4
E assert 3 == 4
test_demo1.py:48: AssertionError
-------------- teardown_function --------------
test_demo1.py::TestCase::test_one
-------------- TestCase.setup_class --------------
-------------- TestCase.setup_method --------------
-------------- TestCase.setup --------------
PASSED [ 60%]
正在执行----test_one
-------------- TestCase.teardown --------------
-------------- TestCase.teardown_method --------------
test_demo1.py::TestCase::test_two
-------------- TestCase.setup_method --------------
-------------- TestCase.setup --------------
FAILED [ 80%]
正在执行test_two
test_demo1.py:74 (TestCase.test_two)
self = <test_rrrcpy.src.test_demo1.TestCase object at 0x7ff048c735d0>
def test_two(self):
print("\n正在执行test_two")
> assert "she" not in "she is a girl"
E AssertionError: assert 'she' not in 'she is a girl'
E 'she' is contained here:
E she is a girl
E ? +++
test_demo1.py:77: AssertionError
-------------- TestCase.teardown --------------
-------------- TestCase.teardown_method --------------
test_demo1.py::TestCase::test_three
-------------- TestCase.setup_method --------------
-------------- TestCase.setup --------------
PASSED [100%]
正在执行test_tree
这是加减法
-------------- TestCase.teardown --------------
-------------- TestCase.teardown_method --------------
-------------- TestCase.teardown_class --------------
-------------- teardown_module --------------
=================================== FAILURES ===================================
_____________________________ test_no_class_func2 ______________________________
def test_no_class_func2():
print("\n正在执行test_no_class_func2")
> assert 1 + 2 == 4
E assert 3 == 4
test_demo1.py:48: AssertionError
---------------------------- Captured stdout setup -----------------------------
-------------- setup_function --------------
----------------------------- Captured stdout call -----------------------------
正在执行test_no_class_func2
--------------------------- Captured stdout teardown ---------------------------
-------------- teardown_function --------------
______________________________ TestCase.test_two _______________________________
self = <test_rrrcpy.src.test_demo1.TestCase object at 0x7ff048c735d0>
def test_two(self):
print("\n正在执行test_two")
> assert "she" not in "she is a girl"
E AssertionError: assert 'she' not in 'she is a girl'
E 'she' is contained here:
E she is a girl
E ? +++
test_demo1.py:77: AssertionError
---------------------------- Captured stdout setup -----------------------------
-------------- TestCase.setup_method --------------
-------------- TestCase.setup --------------
----------------------------- Captured stdout call -----------------------------
正在执行test_two
--------------------------- Captured stdout teardown ---------------------------
-------------- TestCase.teardown --------------
-------------- TestCase.teardown_method --------------
=========================== short test summary info ============================
FAILED test_demo1.py::test_no_class_func2 - assert 3 == 4
FAILED test_demo1.py::TestCase::test_two - AssertionError: assert 'she' not i...
========================= 2 failed, 3 passed in 0.20s ==========================
Process finished with exit code 1
Assertion failed
Assertion failed
Assertion failed
总结
- 模块级(setup_module/teardown_module):在模块起止生效
- 函数级(setup_function/teardown_function):在非类方法的函数起止生效
- 执行顺序是:
setup_model->
- setup_class->setup_method-> setup ->用例class func -> teardown-> teardown_method-> teardown_class-> teardown_model
- setup_function ->用例no class func -> teardown_function-> teardown_model