目录结构:
pytest.ini
[pytest]
addopts = -s -v
testpaths = ./scripts
python_files = test_*.py
python_classes = Test*
python_functions = test_*
test_case.py
def test_case_01():
assert 1
class TestCase(object):
def test_case_01(self):
"""再执行三"""
assert 1
def test_case_02(self):
"""再执行六"""
assert {"title":"v2ex"} != {"title":"V2EX"}
def test_case_03(self):
"""再执行九"""
assert 1
def setup_class(self):
"""先执行一setup_class 类级别的setup"""
print("类级别的setup")
def teardown_class(self):
"""再执行十一teardown_class 类级别的teardown"""
print("类级别的teardown")
def setup_method(self):
"""再执行二setup_method 类中方法级别的setup"""
"""再执行五setup_method 类中方法级别的setup"""
"""再执行八setup_method 类中方法级别的setup"""
print("类中方法级别的setup")
def teardown_method(self):
"""再执行四teardown_method 类中方法级别的teardown"""
"""再执行七teardown_method 类中方法级别的teardown"""
"""再执行十teardown_method 类中方法级别的teardown"""
print("类中方法级别的teardown ")
test_login.py
def test_login1():
assert 0
def test_login2():
assert 1
结果:
=========================================================== test session starts ============================================================
platform win32 -- Python 3.6.6, pytest-5.3.2, py-1.8.1, pluggy-0.13.1 -- c:python36python.exe
cachedir: .pytest_cache
rootdir: D:s27day68, inifile: pytest.ini, testpaths: ./scripts
collected 6 items
scripts/test_case.py::test_case_01 PASSED
scripts/test_case.py::TestCase::test_case_01 类级别的setup
类中方法级别的setup
PASSED类中方法级别的teardown
scripts/test_case.py::TestCase::test_case_02 类中方法级别的setup
PASSED类中方法级别的teardown
scripts/test_case.py::TestCase::test_case_03 类中方法级别的setup
PASSED类中方法级别的teardown
类级别的teardown
scripts/test_login.py::test_login1 FAILED
scripts/test_login.py::test_login2 PASSED
================================================================= FAILURES =================================================================
_______________________________________________________________ test_login1 ________________________________________________________________
def test_login1():
> assert 0
E assert 0
scripts est_login.py:4: AssertionError
======================================================= 1 failed, 5 passed in 0.14s ========================================================