一、pytest运行方式

import pytest def test_func(): """pytest命令会执行当前目录及子目录下所有test_*.py及*_test.py格式的文件""" print("---test----") def test_two(): assert False # if __name__ == '__main__': # pytest.main(["-s", "test_pystart.py"]) """ 执行所有pytest测试用例:pytest 执行指定路径下测试用例:pytest -s cases/ 执行单个:pytest -s test_pystart.py::test_two """
二、pytest运行类测试用例
class TestApi: """pytest能找到Test开头的测试类外以及类中所有以test_开头的函数及方法""" def test_one(self): x = 1 assert x == 1 def test_two(self): print('-----------two--------') assert False """ 执行命令:pytest -s test_myclass.py 执行单个:pytest test_myclass.py::TestApi::test_two """
三、pytest前置后置方法
def setup_function(): print("-----setup_function-----") def teardown_function(): print("--------teardown_function------") def test_one(): print("---------one----------") def test_two(): print("---------two------")
浙公网安备 33010602011771号