为什么用fixture
1.用例里有些需要执行某些步骤,有些不需要,而setup和teardown是针对整个脚本全局生效的
2.用fixture自定义前置条件,可以跨文件使用,即把@pytest.fixture()下定义的所有条件放置在一个文件中,不需要导入到test文件中,在测试用例执行时会自动寻找fixture
import pytest
@pytest.fixture()
def myfixture():
print("登陆操作")
class Test_demo():
def test_1(self, myfixture):
print("查看我的主页")
def test_2(self):
print("注册账户,不需要登陆的操作")
本文介绍了pytest中fixture的功能及优势。fixture允许自定义测试前置条件,并且可以在多个测试用例间共享,实现跨文件使用。通过示例展示了如何定义和使用fixture来简化测试过程。
1372

被折叠的 条评论
为什么被折叠?



