pytest - 在 setup_module 中使用 funcargs [英] pytest - use funcargs inside setup_module

查看:35
本文介绍了pytest - 在 setup_module 中使用 funcargs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 confetst.py 中包含了我自己的命令行选项

I include at conftetst.py my own command line options

def pytest_addoption(parser):
    parser.addoption("--backend" , default="test_backend",
        help="run testx for the given backend, default: test_backend")

def pytest_generate_tests(metafunc):
    if 'backend' in metafunc.funcargnames:
       if metafunc.config.option.backend:
          backend = metafunc.config.option.backend
          backend = backend.split(',')
          backend = map(lambda x: string.lower(x), backend)
        metafunc.parametrize("backend", backend)

如果我在模块内的普通函数中使用这个命令行选项:

If I use this command line option inside a normal function inside a module:

module: test_this.py;  

def test_me(backend): 
  print backend

它按预期工作.

现在我想在一些测试之前包含 setup_module 函数来创建/复制一些东西:

Now I want to include the setup_module function to create /copy some stuff before some tests:

def setup_module(backend):
   import shutil
   shutil.copy(backend, 'use_here')
   ...

不幸的是,我现在知道如何在 setup_module 函数中访问这个命令行选项.没有任何效果,我试过了.

unfortunately I have now idea how to get access to this command line option inside the setup_module function. Nothing works, what I tried.

感谢您的帮助和建议.

干杯

推荐答案

有一个 API 扩展正在讨论中,它允许在设置资源中使用 funcargs,您的用例就是一个很好的例子.有关正在讨论的 V2 草案,请参见此处:http://pytest.org/latest/resources.html

There is a API-extension under discussion which would allow to use funcargs in setup resources and your use case is a good example for it. See here for the V2 draft under discussion: http://pytest.org/latest/resources.html

今天,你可以这样解决你的问题::

Today, you can solve your problem like this::

# contest of conftest.py

import string

def pytest_addoption(parser):
    parser.addoption("--backend" , default="test_backend",
        help="run testx for the given backend, default: test_backend")

def pytest_generate_tests(metafunc):
    if 'backend' in metafunc.funcargnames:
        if metafunc.config.option.backend:
            backend = metafunc.config.option.backend
            backend = backend.split(',')
            backend = map(lambda x: string.lower(x), backend)
        metafunc.parametrize("backend", backend, indirect=True)

def setupmodule(backend):
    print "copying for", backend

def pytest_funcarg__backend(request):
    request.cached_setup(setup=lambda: setupmodule(request.param),
                         extrakey=request.param)
    return request.param

给定一个包含两个测试的测试模块:

Given a test module with two tests:

def test_me(backend):
    print backend

def test_me2(backend):
    print backend

然后您可以运行以检查事情是否如您所愿:

you can then run to check that things happen as you expect:

$ py.test -q -s --backend=x,y

$ py.test -q -s --backend=x,y

收集了 4 件物品复制 xX. 复制 y是.X.y

collected 4 items copying for x x .copying for y y .x .y

4 在 0.02 秒内通过

4 passed in 0.02 seconds

由于有两个后端在测试中,因此您需要进行四个测试,但模块设置对于模块中使用的每个后端仅进行一次.

As there are two backends under test you get four tests but the module-setup is only done once per each backend used in a module.

这篇关于pytest - 在 setup_module 中使用 funcargs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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