python - 在 py.test 中多次调用同一个 fixture

标签 python testing pytest

我正在使用 Pytest 在 Python 中编写一些测试。

我正在使用以下装置来创建一个用户并在我的测试中使用它,然后在测试后删除它(终结器):

@pytest.fixture(scope='function')
def test_user(request):
    def finalizer():
        delete_the_user()

    request.addfinalizer(finalizer)
    return user()

delete_the_user()user()两个函数,这里不详述。

问题是:我想使用 2 个用户进行一些测试。

我试过这样调用 fixture :

def test_function(test_user_1 = test_user, test_user_2 = test_user):
    # Test here

没有成功。

如何在测试中多次使用同一个 fixture ?

最佳答案

我能想到的唯一方法是:

  • 有第二个固定装置
  • 改为让 fixture 返回一个函数(但随后您还必须使用 fixture 重构其他函数)。

关于python - 在 py.test 中多次调用同一个 fixture ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37001586/

相关文章:

android - 如何在 Android 中手动测试 In App Update

python - pytest中的全局变量

python - 如何为keras源代码运行pytest

python - 如何使 pytest 固定装置与装饰功能一起使用?

python - 删除 Numpy 中重新排列的数组

python - IPython + Pandas 中的 Matplotlib : ImportError Symbol not found: _png_create_info_struct

python - 将加载的图像数组保存为 csv

python - Matplotlib 1.3.0,图例行和文本不匹配

haskell - 如何使我的类型成为 Arbitrary 的实例?

windows - 保持构建服务器更新的建议