import pytest class Test1: def test1(self): assert 1 == 1 assert 2 + 2 == 4 assert 3 + 3 == 6 if __name__=='__main__': pytest.main([__file__]) ''' ============================ test session starts ============================= collecting ... collected 1 item test_1.py::Test1::test1 PASSED [100%] ============================== 1 passed in 0.01s ============================== '''
import pytest # pytest.main([r'D:\pythonProject070901\pytestt\test_1.py','-sv']) # # 小括号里啥也不写,文件夹内test开头的全部运行 @pytest.fixture() def putAnimal(): print("我是一个方法!!!!!") class TestPutAnimal1: def test_PutAnimal1(self, putAnimal): print("动物放入房间的类1") assert 1 == 1 def test_PutAnimal2(self, putAnimal): print("动物放入房间的类2") assert 1 == 1 if __name__=='__main__': pytest.main([__file__,'-sv']) ''' ============================= test session starts ============================= collecting ... collected 2 items test4.py::TestPutAnimal1::test_PutAnimal1 我是一个方法!!!!! PASSED [ 50%]动物放入房间的类1 test4.py::TestPutAnimal1::test_PutAnimal2 我是一个方法!!!!! PASSED [100%]动物放入房间的类2 ============================== 2 passed in 0.01s ============================== '''
import pytest # pytest.main([r'D:\pythonProject070901\pytestt\test_1.py','-sv']) # # 小括号里啥也不写,文件夹内test开头的全部运行 @pytest.fixture() def putAnimal(): print("game start!!!!!") yield print('game over~~~~~~~') class TestPutAnimal1: def test_PutAnimal1(self, putAnimal): print("动物放入房间的类1") assert 1 == 1 def test_PutAnimal2(self, putAnimal): print("动物放入房间的类2") assert 1 == 1 if __name__=='__main__': pytest.main([__file__,'-sv']) ,,, ============================= test session starts ============================= collecting ... collected 2 items test4.py::TestPutAnimal1::test_PutAnimal1 game start!!!!! PASSED [ 50%]动物放入房间的类1 game over~~~~~~~ test4.py::TestPutAnimal1::test_PutAnimal2 game start!!!!! PASSED [100%]动物放入房间的类2 game over~~~~~~~ ============================== 2 passed in 0.01s ============================== '''