gpt4 book ai didi

python - 我可以在 conftest.py 中定义 fixture 以外的函数吗

转载 作者:行者123 更新时间:2023-12-04 16:45:50 26 4
gpt4 key购买 nike

我可以在 conftest.py 中定义 fixture 以外的函数吗

如果我有一个函数 add() ,在 conftest.py 中定义

我可以使用 test_add.py 文件中的函数只是调用 add()

最佳答案

两种可能的方式

  • 您可以从 conftest.py 导入函数添加,或者将其移动到更合适的位置。 (例如utils.py)
  • 您可以创建返回函数的 fixture 并在您的测试中使用它。像这样的东西。

  • conftest.py
    import pytest


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

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

    我无法想象什么时候会是一个很好的解决方案。但我确信它存在。

    关于python - 我可以在 conftest.py 中定义 fixture 以外的函数吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61006189/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com