pytest中夹具内产量的含义 [英] Meaning of yield within fixture in pytest

查看:37
本文介绍了pytest中夹具内产量的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了 docs.pytest.org
我不确定语句的含义:yield smtp_connection

能否请人解释一下 yield 的作用,以及它是否是强制性的?

Can please someone explain what yield does, and if it's mandatory?

推荐答案

首先这不是强制性的!!!Yield execute 测试主体,例如,您可以使用前置条件和后置条件设置测试.为此,我们可以使用 conftest.py:

First of all it's not mandatory!!! Yield execute test body, for example, you can set up your test with pre-condition and post condition. For this thing we can use conftest.py:

import pytest


@pytest.fixture
def set_up_pre_and_post_conditions():
    print("Pre condition")
    yield # this will be executed our test
    print("Post condition")

我们的测试,例如存储在 test.py 中:

Our test, for example store in test.py:

def test(set_up_pre_and_post_conditions):
    print("Body of test")

那么,让我们启动它:pytest test.py -v -s输出:

So, let's launch it: pytest test.py -v -s Output:

test.py::test Pre condition
Body of test
PASSEDPost condition

这不是 yield 的全部功能,只是示例,希望对您有所帮助.

It's not full functionality of yield, just example, I hope it will be helpful.

这篇关于pytest中夹具内产量的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆