1.执行级别
模块级(setup_module/teardown_module)开始于模块始末,全局的
函数级(setup_function/teardown_function)只对函数用例生效(不在类中)
类级(setup_class/teardown_class)只在类中前后运行一次(在类中)
方法级(setup_method/teardown_method)开始于方法始末(在类中)
setup_module/teardown_module的优先级是最大的,然后函数里面用到的setup_function/teardown_function与类里面的setup_class/teardown_class互不干涉
作者:sofiiii
链接:https://www.jianshu.com/p/8891a8435ede
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
2.函数
setup_function/teardown_function
每个用例(函数)开始和结束时调用一次
#!/usr/bin/env python
# -*- coding:utf8 -*-
import pytest
def setup_module():
print("[setup_module]:整个.py模块开始时执行一次")
def teardown_module():
print("[setup_module]:整个.py模块结束时执行一次")
def setup_function():
print("[setup_function]: 每个用例(函数)开始时调用一次")
def teardown_function():
print("[teardown_function]: 每个用例(函数)结束时调用一次")
def test_one():
print("[test_one]:正在执行")
x = "this"
assert 'h' in x
def test_two():
print("[test_two]:正在执行")
x = "hello"
assert hasattr(x, 'check')
def test_three():
print("[test_three]:正在执行")
a = "hello"
b = "hello world"
assert a in b
if __name__ == "__main__":
pytest.main(["-s", "test_api.py"])
➜ testcases pytest -s
============================================================================== test session starts ===============================================================================
platform darwin -- Python 3.7.2, pytest-7.4.0, pluggy-1.2.0
rootdir: /Users/qina/workspace/python-space/python-test/testcases
collected 3 items
test_api.py [setup_module]:整个.py模块开始时执行一次
[setup_function]: 每个用例(函数)开始时调用一次
[test_one]:正在执行
.[teardown_function]: 每个用例(函数)结束时调用一次
[setup_function]: 每个用例(函数)开始时调用一次
[test_two]:正在执行
F[teardown_function]: 每个用例(函数)结束时调用一次
[setup_function]: 每个用例(函数)开始时调用一次
[test_three]:正在执行
.[teardown_function]: 每个用例(函数)结束时调用一次
[setup_module]:整个.py模块结束时执行一次
========================FAILURES ========================
______________________test_two ______________________
def test_two():
print("[test_two]:正在执行")
x = "hello"
> assert hasattr(x, 'check')
E AssertionError: assert False
E + where False = hasattr('hello', 'check')
test_api.py:26: AssertionError
===================== short test summary info =====================
FAILED test_api.py::test_two - AssertionError: assert False
===================== 1 failed, 2 passed in 0.11s =====================
2.类
#!/usr/bin/env python
# -*- coding:utf8 -*-
import pytest
class TestCase():
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_one(self):
print("[test_one]:正在执行")
x = "this"
assert 'h' in x
def test_two(self):
print("[test_two]:正在执行")
x = "hello"
assert hasattr(x, 'check')
def test_three(self):
print("[test_three]:正在执行")
a = "hello"
b = "hello world"
assert a in b
if __name__ == "__main__":
pytest.main(["-s", "test_api.py"])
➜ testcases pytest -s
============================================================================== test session starts ===============================================================================
platform darwin -- Python 3.7.2, pytest-7.4.0, pluggy-1.2.0
rootdir: /Users/qina/workspace/python-space/python-test/testcases
collected 3 items
test_api.py [setup_class]:类的所有用例执行前执行
[setup_method]:每个用例执行前执行
[test_one]:正在执行
.[teardown_method]:每个用例执行后执行
[setup_method]:每个用例执行前执行
[test_two]:正在执行
F[teardown_method]:每个用例执行后执行
[setup_method]:每个用例执行前执行
[test_three]:正在执行
.[teardown_method]:每个用例执行后执行
[teardown_class]:类的所有用例执行后执行
=========================FAILURES =========================
__________________________ TestCase.test_two __________________________
self = <testcases.test_api.TestCase object at 0x101bd6160>
def test_two(self):
print("[test_two]:正在执行")
x = "hello"
> assert hasattr(x, 'check')
E AssertionError: assert False
E + where False = hasattr('hello', 'check')
test_api.py:28: AssertionError
=======================short test summary info =======================
FAILED test_api.py::TestCase::test_two - AssertionError: assert False
======================= 1 failed, 2 passed in 0.09s =======================
3.函数+类
#!/usr/bin/env python
# -*- coding:utf8 -*-
import pytest
def setup_module():
print("[setup_module]:整个.py模块开始时执行一次")
def teardown_module():
print("[setup_module]:整个.py模块结束时执行一次")
def setup_function():
print("[setup_function]: 每个用例(函数)开始时调用一次")
def teardown_function():
print("[teardown_function]: 每个用例(函数)结束时调用一次")
def test_four():
print("[test_four]:正在执行")
x = "this"
assert 'h' in x
def test_five():
print("[test_five]:正在执行")
x = "hello"
assert hasattr(x, 'check')
def test_six():
print("[test_six]:正在执行")
a = "hello"
b = "hello world"
assert a in b
class TestCase():
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_one(self):
print("[test_one]:正在执行")
x = "this"
assert 'h' in x
def test_two(self):
print("[test_two]:正在执行")
x = "hello"
assert hasattr(x, 'check')
def test_three(self):
print("[test_three]:正在执行")
a = "hello"
b = "hello world"
assert a in b
if __name__ == "__main__":
pytest.main(["-s", "test_api.py"])
➜ testcases pytest -s
=================== test session starts ===================
platform darwin -- Python 3.7.2, pytest-7.4.0, pluggy-1.2.0
rootdir: /Users/qina/workspace/python-space/python-test/testcases
collected 6 items
test_api.py [setup_module]:整个.py模块开始时执行一次
[setup_function]: 每个用例(函数)开始时调用一次
[test_four]:正在执行
.[teardown_function]: 每个用例(函数)结束时调用一次
[setup_function]: 每个用例(函数)开始时调用一次
[test_five]:正在执行
F[teardown_function]: 每个用例(函数)结束时调用一次
[setup_function]: 每个用例(函数)开始时调用一次
[test_six]:正在执行
.[teardown_function]: 每个用例(函数)结束时调用一次
[setup_class]:类的所有用例执行前执行
[setup_method]:每个用例执行前执行
[test_one]:正在执行
.[teardown_method]:每个用例执行后执行
[setup_method]:每个用例执行前执行
[test_two]:正在执行
F[teardown_method]:每个用例执行后执行
[setup_method]:每个用例执行前执行
[test_three]:正在执行
.[teardown_method]:每个用例执行后执行
[teardown_class]:类的所有用例执行后执行
[setup_module]:整个.py模块结束时执行一次
======================== FAILURES ========================
_________________________ test_five _________________________
def test_five():
print("[test_five]:正在执行")
x = "hello"
> assert hasattr(x, 'check')
E AssertionError: assert False
E + where False = hasattr('hello', 'check')
test_api.py:28: AssertionError
_________________________ TestCase.test_two _________________________
self = <testcases.test_api.TestCase object at 0x104702f60>
def test_two(self):
print("[test_two]:正在执行")
x = "hello"
> assert hasattr(x, 'check')
E AssertionError: assert False
E + where False = hasattr('hello', 'check')
test_api.py:59: AssertionError
========================= short test summary info =========================
FAILED test_api.py::test_five - AssertionError: assert False
FAILED test_api.py::TestCase::test_two - AssertionError: assert False
========================= 2 failed, 4 passed in 0.12s =========================
网友评论