本文介绍了无法让pytest理解安装程序上的命令行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我一直在尝试让pytest根据一些命令行参数在不同的环境上运行硒测试。但是它一直抛出此错误:

So I have been trying to get pytest to run selenium tests on different environments based on some command-line argument. But it keeps throwing this error:

TypeError: setup_class() takes exactly 2 arguments (1 given)

似乎可以理解 setup_class 需要2个参数,但是 host 未通过。以下是 setup_class 的代码:

It seems that it is understanding that setup_class takes 2 arguments, but host is not being passed. Here's the code for setup_class:

def setup_class(cls, host):
        cls.host = host

这是conftest.py文件:

And here is the conftest.py file:

def pytest_addoption(parser):
    parser.addoption("--host", action="store", default='local',
        help="specify the host to run tests with. local|stage")

@pytest.fixture(scope='class')
def host(request):
    return request.config.option.host

奇怪的是 host 被函数看到(因此,如果我创建一个test_function并将host作为参数传递,它就可以了),它只是设置的固定装置不起作用。

What is strange is that host is being seen by the functions (so if I make a test_function and pass host as a parameter, it gets it just fine), it is just the setup fixtures that are not working.

我环顾四周,发现了这个,但这似乎不起作用(并且自2.3起已过时。

I looked around and found this, pytest - use funcargs inside setup_module but that doesn't seem to be working (and it is outdated since 2.3.

有人知道我在做什么吗? m做错了吗?使用py.test 2.3.2

Does anyone know what I'm doing wrong? Using py.test 2.3.2.

谢谢

推荐答案

setup_module / class / method和朋友不能直接使用pytest固定装置。pytest-2.3的等效方法是使用。如果可以的话,最好明确地传递固定装置以测试功能或放置。

setup_module/class/method and friends cannot work with pytest fixtures directly. The pytest-2.3 way of doing the equivalent is to use autouse fixtures. If you can it's better to pass fixtures explicitely to test functions or to put a usefixtures marker on a class or module.

这篇关于无法让pytest理解安装程序上的命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 06:35