pythest

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 ==============================
'''

 

import pytest
# pytest.main([r'D:\pythonProject070901\pytestt\test_1.py','-sv'])
# # 小括号里啥也不写,文件夹内test开头的全部运行

@pytest.fixture(scope='class')
# 范围 function 每个 test_PutAnimal1
# 范围 class 每个 TestPutAnimal1
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

test4.py::TestPutAnimal1::test_PutAnimal2 PASSED                         [100%]动物放入房间的类2
game over~~~~~~~


============================== 2 passed in 0.01s ==============================
'''
import pytest
# pytest.main([r'D:\pythonProject070901\pytestt\test_1.py','-sv'])
# # 小括号里啥也不写,文件夹内test开头的全部运行

@pytest.fixture(scope='module')
# 范围 function 每个 test_PutAnimal1
# 范围 class 每个 TestPutAnimal1
# 范围 module 每个py文件
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

class TestPutAnimal2:
    def  test_PutAnimal3(self, putAnimal):
        print("动物放入房间的类3")
        assert 1 == 1
    def  test_PutAnimal4(self, putAnimal):
        print("动物放入房间的类4")
        assert 1 == 1
if __name__=='__main__':
    pytest.main([__file__,'-sv'])
'''
============================= test session starts =============================
collecting ... collected 4 items

test4.py::TestPutAnimal1::test_PutAnimal1 game start!!!!!
PASSED                         [ 25%]动物放入房间的类1

test4.py::TestPutAnimal1::test_PutAnimal2 PASSED                         [ 50%]动物放入房间的类2

test4.py::TestPutAnimal2::test_PutAnimal3 PASSED                         [ 75%]动物放入房间的类3

test4.py::TestPutAnimal2::test_PutAnimal4 PASSED                         [100%]动物放入房间的类4
game over~~~~~~~


============================== 4 passed in 0.01s ==============================

进程已结束,退出代码0
'''

 

posted @ 2023-07-19 21:55  胖豆芽  阅读(13)  评论(0编辑  收藏  举报