前言:1、当 pytest.fixture(scope="module") 时,pytest的yieId 类似unittest的teartownclass

   2、当 pytest.fixture(scope="function") 时,pytest的yieId 类似unittest的teartown

3、其中一个用例出现异常,不会影响yield后面的teardown执行,运行结果互不影响

4、如果在setup就异常了,那么是不会去执行yield后面的teardown内容了

# coding=utf-8
import pytest @pytest.fixture(scope="module")
def login():
print("登录成功") yield
print("用例执行完成,收尾") def test1(login):
print('操作1')
print("-----------------------------------------------") def test12(login):
print('操作2')
print("-----------------------------------------------") def test3(login):
print('操作3') print("-----------------------------------------------") if __name__ == '__main__':
pytest.main(['-s', "text.fix2.py"])

运行结果:

pytest 的 yield-LMLPHP

# coding=utf-8
import pytest @pytest.fixture(scope="function")
def login():
print("登录成功") yield
print("用例执行完成,收尾") def test1(login):
print('操作1')
print("-----------------------------------------------") def test12(login):
print('操作2')
print("-----------------------------------------------") def test3(login):
print('操作3') print("-----------------------------------------------") if __name__ == '__main__':
pytest.main(['-s', "text.fix2.py"])

运行结果:

pytest 的 yield-LMLPHP

04-16 01:59