简体   繁体   English

如何将额外的参数传递给我的py.test设置方法?

[英]How to pass extra arguments to my py.test setup method?

I need to setup tests depending on where or how I want to run py.test tests. 我需要根据要在哪里或如何运行py.test测试来py.test测试。 I want to be able to do something like this 我希望能够做这样的事情

py.test --do_this

py.test --do_that

and retrieve the values in the setup method of a test class 并在测试类的设置方法中检索值

class TestSuite(object):
    def setup(self):
        if (do_this): 
            ...

Something like that. 这样的事情。 Can this be done? 能做到吗? And if so, how? 如果是这样,怎么办?

From the documentation, you can add arguments to the pytest caller 从文档中,您可以向pytest调用者添加参数

# content of test_sample.py
def test_answer(do):
    if do == "this":
        print ("do this option")
    elif do == "that":
        print ("do that option")
    assert 0 # to see what was printed

# content of conftest.py
import pytest

def pytest_addoption(parser):
    parser.addoption("--do", action="store", default="that",
        help="my option: this or that")

@pytest.fixture
def do(request):
    return request.config.getoption("--do")

Then, you can call pytest as pytest -q test_sample.py to get the default case with that and pytest -q --do=this for the other case 然后,您可以将pytest称为pytest -q test_sample.py以获取默认情况thatpytest -q --do=this用于其他情况

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

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