简体   繁体   English

我可以在 conftest.py 中定义固定装置以外的功能吗

[英]Can I define functions other than fixtures in conftest.py

Can I define functions other than fixtures in conftest.py我可以在 conftest.py 中定义固定装置以外的功能吗

if I have a function add(), defined in conftest.py如果我有一个 function add(),在 conftest.py 中定义

can I using the function inside test_add.py file just calling add()我可以在 test_add.py 文件中使用 function 只需调用 add()

Two possible ways两种可能的方式

  1. You could just import function add from conftest.py, or move it to something more appropriate.您可以从 conftest.py 中导入 function 添加,或将其移至更合适的位置。 (for example utils.py) (例如 utils.py)

  2. You could create fixture that returns function and use it in your tests.您可以创建返回 function 的夹具并在您的测试中使用它。 Something like this.像这样的东西。

conftest.py conftest.py

import pytest


@pytest.fixture
def add():
    def inner_add(x, y):
        return x + y
    return inner_add

test_all.py test_all.py

def test_all(add):
    assert add(1, 2) == 3

I can not imagine situation when it will be a good solution.我无法想象什么时候它会是一个很好的解决方案。 But I am sure it exists.但我确信它存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM