pytest-fixture固件的使用

fixture 固件:pytest自动化中用于前后置,功能强大,可替代setup,teardown,setup_class,teardown_class
装饰器:
@pytest.fixture(scope="作用域“, params="参数化", autouse=”自动执行“, ids=”参数别名“, name="别名"

下面是fixture的两种用法:

1.fixture固件的自动调用

1)@pytest.fixture(scope='class',params=['name1','name2'],autouse=True)

def login(request):

        print("用户登录")

        yield reqyest.param 

        print("退出登录")

2)类中的每个用例都会自动调用login

2.fixture固件的手动调用

1)@pytest.fixture(scope='function',autouse=False)

def login():

        print("用户登录")

        yield  #启用后置

        print("退出登录")

2)测试用例中手动调用

def test_001(self, login):

只有指定的如上述调用了fixture函数的测试用例才会生效

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用: @pytest.fixture 是pytest测试框架中的一个装饰器。它的作用是为测试函数或测试类提供一个共享的测试环境,可以在测试之前或之后执行一些操作。通过使用 @pytest.fixture 装饰器,我们可以在测试用例中使用这个 fixture,并且可以通过参数的方式将 fixture 传递给测试函数或测试类。 引用: 举例来说,使用 @pytest.fixture 装饰器定义了一个名为 return_data 的 fixture。在测试类 Test_A 中的 test_a 方法中,通过将 return_data 作为参数传入,可以在测试中使用 fixture 中返回的数据。在运行测试之前,fixture 会先执行一些操作,比如打印相关信息,然后返回一个值供测试使用。在 test_a 方法中,我们可以对返回的数据进行断言和测试。 引用: 另外一个例子是使用 @pytest.fixture 名称参数给fixture命名,在测试类 Test_A 中的 test_a 方法中,通过 before_fixture_name 参数来使用这个 fixture。fixture 可以在测试之前或之后执行一些操作,这里的 before_fixture_name fixture 在测试之前打印了一些信息。在 test_a 方法中,我们可以执行测试并进行断言。通过使用名称参数,我们可以在测试类中使用多个不同的 fixture,每个 fixture 都可以执行一些不同的操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [自动化测试学习Day4-@pytest.fixture()入门及应用](https://blog.csdn.net/weixin_49379644/article/details/119780590)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [pytest系列——fixture函数使用pytest测试框架测试固件)](https://blog.csdn.net/mashang_z111/article/details/126962394)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值