测试用例运行级别
摘要:pytest框架中,测试用例运行级别,setup,teardown
测试用例运行级别
- 模块级(setup_module/teardown_module):开始于模块始末,全局的
- 函数级(setup_function/teardown_function):只对函数用例生效
- 类级(setup_class/teardown_class):只在类中前后运行一次
- 方法级(setup_method/teardown_method):开始于方法始末
- 类里面的(setup/teardown):运行在方法前后
函数式
setup_function/teardown_function
在每个函数的前后执行一次。
示例:
def setup_function():
print("\n执行setup_function")
def teardown_function():
print("\n执行teardown_funciton")
def test001():
print("\n执行用例1 >>>>>>>>>>>>>>>>>>>>")
assert True
def test002():
print("\n执行用例2 >>>>>>>>>>>>>>>>>>>>")
assert True
pytest执行结果:
(venv) zydeMacBook-Air:learnpytest zy$ pytest -s test_a.py
=========================================================== test session starts ============================================================
platform darwin -- Python 3.7.4, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /Users/zy/PycharmProjects/learnpytest
collected 2 items
test_a.py
执行setup_function
执行用例1 >>>>>>>>>>>>>>>>>>>>
.
执行teardown_funciton
执行setup_function
执行用例2 >>>>>>>>>>>>>>>>>>>>
.
执行teardown_funciton
============================================================ 2 passed in 0.02s =============================================================
setup_module/teardown_module
在每个模块的前后执行一次。
def setup_module():
print("\n执行setup_module")
def teardown_module():
print("\n执行teardown_module")
def test001():
print("\n执行用例1 >>>>>>>>>>>>>>>>>>>>")
assert True
def test002():
print("\n执行用例2 >>>>>>>>>>>>>>>>>>>>")
assert True
pytest执行结果:
(venv) zydeMacBook-Air:learnpytest zy$ pytest -s test_a.py
=========================================================== test session starts ============================================================
platform darwin -- Python 3.7.4, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /Users/zy/PycharmProjects/learnpytest
collected 2 items
test_a.py
执行setup_module
执行用例1 >>>>>>>>>>>>>>>>>>>>
.
执行用例2 >>>>>>>>>>>>>>>>>>>>
.
执行teardown_module
============================================================ 2 passed in 0.02s =============================================================
setup_function/teardown_function和setup_module/teardown_module四种方法是可以任意组合的,用一个或多个都没关系。见下面的示例。
使用setup_function和setup_module。
def setup_function():
print("\n执行setup_function")
def setup_module():
print("\n执行setup_module")
def test001():
print("\n执行用例1 >>>>>>>>>>>>>>>>>>>>")
assert True
def test002():
print("\n执行用例2 >>>>>>>>>>>>>>>>>>>>")
assert True
pytest执行结果:
(venv) zydeMacBook-Air:learnpytest zy$ pytest -s test_a.py
=========================================================== test session starts ============================================================
platform darwin -- Python 3.7.4, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /Users/zy/PycharmProjects/learnpytest
collected 2 items
test_a.py
执行setup_module
执行setup_function
执行用例1 >>>>>>>>>>>>>>>>>>>>
.
执行setup_function
执行用例2 >>>>>>>>>>>>>>>>>>>>
.
============================================================ 2 passed in 0.01s =============================================================
类和方法
setup/teardwon
在每个方法的前后执行一次。
class TestCase:
def setup(self):
print("\n执行setup")
def teardown(self):
print("\n执行teardown")
def test001(self):
print("\n执行用例1 >>>>>>>>>>>>>>>>>>>>")
assert True
def test002(self):
print("\n执行用例2 >>>>>>>>>>>>>>>>>>>>")
assert True
pytest执行结果:
(venv) zydeMacBook-Air:learnpytest zy$ pytest -s test_a.py
=========================================================== test session starts ============================================================
platform darwin -- Python 3.7.4, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /Users/zy/PycharmProjects/learnpytest
collected 2 items
test_a.py
执行setup
执行用例1 >>>>>>>>>>>>>>>>>>>>
.
执行teardown
执行setup
执行用例2 >>>>>>>>>>>>>>>>>>>>
.
执行teardown
============================================================ 2 passed in 0.01s =============================================================
setup_method/teardown_method和setup/teardown效果是一样的,两者选其一就可以了。当类中同时出现setup_method/teardown_method和setup/teardown,效果如何呢?见下面的示例。
class TestCase:
def setup(self):
print("\n执行setup")
def teardown(self):
print("\n执行teardown")
def setup_method(self):
print("\n执行setup_method")
def teardown_method(self):
print("\n执行teardown_method")
def test001(self):
print("\n执行用例1 >>>>>>>>>>>>>>>>>>>>")
assert True
def test002(self):
print("\n执行用例2 >>>>>>>>>>>>>>>>>>>>")
assert True
pytest执行结果:
(venv) zydeMacBook-Air:learnpytest zy$ pytest -s test_a.py
=========================================================== test session starts ============================================================
platform darwin -- Python 3.7.4, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /Users/zy/PycharmProjects/learnpytest
collected 2 items
test_a.py
执行setup_method
执行setup
执行用例1 >>>>>>>>>>>>>>>>>>>>
.
执行teardown
执行teardown_method
执行setup_method
执行setup
执行用例2 >>>>>>>>>>>>>>>>>>>>
.
执行teardown
执行teardown_method
============================================================ 2 passed in 0.01s =============================================================
由此可见,虽然setup/teardown和setup_method/teardown_method都是作用于每一个方法前后,但setup_method/teardown_method的作用范围比setup/teardown大。
setup_class/teardown_class
在每个类的前后执行一次。
class TestCase:
def setup_class(self):
print("\n执行setup_class")
def teardown_class(self):
print("\n执行teardown_class")
def test001(self):
print("\n执行用例1 >>>>>>>>>>>>>>>>>>>>")
assert True
def test002(self):
print("\n执行用例2 >>>>>>>>>>>>>>>>>>>>")
assert True
pytest执行结果:
(venv) zydeMacBook-Air:learnpytest zy$ pytest -s test_a.py
=========================================================== test session starts ============================================================
platform darwin -- Python 3.7.4, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /Users/zy/PycharmProjects/learnpytest
collected 2 items
test_a.py
执行setup_class
执行用例1 >>>>>>>>>>>>>>>>>>>>
.
执行用例2 >>>>>>>>>>>>>>>>>>>>
.
执行teardown_class
============================================================ 2 passed in 0.01s =============================================================
混合使用
在函数和类混合的情况下用例优先级的应用示例如下:
class TestCase:
def setup(self):
print("\n执行setup")
def teardown(self):
print("\n执行teardown")
def setup_method(self):
print("\n执行setup_method")
def teardown_method(self):
print("\n执行teardown_method")
def setup_class(self):
print("\n执行setup_class")
def teardown_class(self):
print("\n执行teardown_class")
def test001(self):
print("\n执行用例1 >>>>>>>>>>>>>>>>>>>>")
assert True
def test002(self):
print("\n执行用例2 >>>>>>>>>>>>>>>>>>>>")
assert True
def setup_function():
print("\n执行teardown_function")
def teardown_function():
print("\n执行teardown_function")
def setup_module():
print("\n执行teardown_module")
def teardown_module():
print("\n执行teardown_module")
def test003():
print("\n执行用例3 >>>>>>>>>>>>>>>>>>>>")
assert True
def test004():
print("\n执行用例4 >>>>>>>>>>>>>>>>>>>>")
assert True
pytest执行结果:
(venv) zydeMacBook-Air:learnpytest zy$ pytest -s test_a.py
=========================================================== test session starts ============================================================
platform darwin -- Python 3.7.4, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /Users/zy/PycharmProjects/learnpytest
collected 4 items
test_a.py
执行teardown_module
执行setup_class
执行setup_method
执行setup
执行用例1 >>>>>>>>>>>>>>>>>>>>
.
执行teardown
执行teardown_method
执行setup_method
执行setup
执行用例2 >>>>>>>>>>>>>>>>>>>>
.
执行teardown
执行teardown_method
执行teardown_class
执行teardown_function
执行用例3 >>>>>>>>>>>>>>>>>>>>
.
执行teardown_function
执行teardown_function
执行用例4 >>>>>>>>>>>>>>>>>>>>
.
执行teardown_function
执行teardown_module
============================================================ 4 passed in 0.03s =============================================================
|