关于设置的话,已经设置过Default test runner,如下图所示
代码内容
import pytest
def setup_module():
print("这是个setup_module方法")
def teardown_module():
print("这是teardown_module 方法")
def setup_function():
print("setup_function")
def teardown_function():
print("teardown_function")
def test_login():
print("这是一个外部的方法")
class TestDemo():
def setup_class(self):
print("setup_class")
def setup_method(self):
print("setup_method")
def setup(self):
print("setup")
def teardown_class(self):
print("teardown_class")
def teardown_method(self):
print("teardown_method")
def teardown(self):
print("teardown")
def test_one(self):
print("开始执行 test_one 方法")
x = "this"
assert 'h' in x
def test_two(self):
print("开始执行 test_two 方法")
x = "hello"
assert 'e' in x
def test_three(self):
print("开始执行 test_three 方法")
a = "hello"
b = "hello world"
assert a in b
if __name__ == '__main__':
pytest.main(['-v', '-s'])
# pytest.main("-v -s")
下图是pycharm的执行结果
C:\Users\Administrator\PycharmProjects\untitled2\venv\Scripts\python.exe "C:/Users/Administrator/PycharmProjects/untitled2/venv/Hogwarts/Python自动化测试/课程/9. Python编程语言与测试框架/9.3 pytest测试框架/test_setup_teardown.py"
============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-5.4.3, py-1.8.1, pluggy-0.13.1 -- C:\Users\Administrator\PycharmProjects\untitled2\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\Administrator\PycharmProjects\untitled2\venv\Hogwarts\Python自动化测试\课程\9. Python编程语言与测试框架\9.3 pytest测试框架
plugins: assume-2.2.1
collecting ... collected 8 items
pytest_test.py::test_one ERROR
pytest_test.py::TestDemo::test_one 开始执行 test_one 方法
PASSED
pytest_test.py::TestDemo::test_two 开始执行 test_two 方法
PASSED
pytest_test.py::TestDemo::test_three 开始执行 test_three 方法
PASSED
test_setup_teardown.py::test_login 这是个setup_module方法
setup_function
这是一个外部的方法
PASSEDteardown_function
test_setup_teardown.py::TestDemo::test_one setup_class
setup_method
setup
开始执行 test_one 方法
PASSEDteardown
teardown_method
test_setup_teardown.py::TestDemo::test_two setup_method
setup
开始执行 test_two 方法
PASSEDteardown
teardown_method
test_setup_teardown.py::TestDemo::test_three setup_method
setup
开始执行 test_three 方法
PASSEDteardown
teardown_method
teardown_class
这是teardown_module 方法
=================================== ERRORS ====================================
_________________________ ERROR at setup of test_one __________________________
file C:\Users\Administrator\PycharmProjects\untitled2\venv\Hogwarts\Python自动化测试\课程\9. Python编程语言与测试框架\9.3 pytest测试框架\pytest_test.py, line 3
def test_one(self):
E fixture 'self' not found
> available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
> use 'pytest --fixtures [testpath]' for help on them.
C:\Users\Administrator\PycharmProjects\untitled2\venv\Hogwarts\Python自动化测试\课程\9. Python编程语言与测试框架\9.3 pytest测试框架\pytest_test.py:3
=========================== short test summary info ===========================
ERROR pytest_test.py::test_one
========================= 7 passed, 1 error in 0.25s ==========================
Process finished with exit code 0