基本知识
1)
setup和teardown方法,在每次执行def都要运行一次
函数级别的方法
1、 运行于测试方法的始末
2、 运行一次测试函数会运行一次setup和teardown方法
import pytest """ 1)定义类 2)创建测试类以test开头 3)创建setup和teardown 4)查看运行结果 """ class TestFunc: def setup(self): print("---setup---") def teardown(self): print("---teardown---") def test_a(self): print("test_a") def test_b(self): print("test_b") if __name__=="__main__": pytest.main(["-s","pytest_func.py"])
结果
pytest_func.py ---setup--- test_a .---teardown--- ---setup--- test_b .---teardown---
类级别的方法
1、 运行于类的始末
2、 一个而测试内只运行一次setup_class和teardown_class,不关心测试类里面有多少个测试函数
import pytest """ 1)定义类 2)创建测试类以test开头 3)创建setup_class和teardown_class 4)查看运行结果 """ class TestFunc: def setup_class(self): print("---setup_class---") def teardown_class(self): print("---teardown_class---") def test_a(self): print("test_a") def test_b(self): print("test_b") if __name__=="__main__": pytest.main(["-s","pytest_class.py"])
结果
..\..\..\..\..\meiduoshangcheng\testcase\t_pytest\pytest_class.py ---setup_class---
test_a
.test_b
.---teardown_class---