【问题标题】:How to pass cmd-line argument to a method in test_file (pytest)?如何将命令行参数传递给测试文件(pytest)中的方法?
【发布时间】:2018-10-23 04:26:32
【问题描述】:

我正在使用 pytest。有两个文件,conftest.pyTest1.py。在执行测试之前需要执行特定的方法。现在将其命名为dummy(如果有任何建议将其更改为test_dummy 也可以)。它现在完全可以正常工作。如何将cmd-line 参数传递给这个被显式调用的dummy 方法?

Test1.py

import time

tmp1 = ""
tmp2 = ""

class Test1:

    def dummy(self):
        global tmp1
        global tmp2
        tmp1 = "Sometext"
        ts = int(time.time())
        tmp2 = tmp1 + str(ts)
        # Need this `ts` value to be passed from command-line and should be accessed in this `dummy`
        # And this dummy to be called before the execution of any tests.
        # Could have added this conftest.py , but my requirement is to create a folder by getting method names here. So making a  separate `dummy`


    def testA(self, set_up):
        val = set_up[0]
        logging.info(val)
        # Do - Something, call some function by passing the `val`

    def testB(self, set_up):
        val = set_up[0]
        logging.info(val)
        # Do - Something, call some function by passing the `val`

    def testC(self, set_up):
        val = set_up[0]
        logging.info(val)
        # Do - Something, call some function by passing the `val`


class Test2:


    def testA(self,set_up):
        val = set_up[0]
        logging.info(val)
        #Do - Something, call some function by passing the `val`

    def testB(self,set_up):
        val = set_up[0]
        logging.info(val)
        # Do - Something, call some function by passing the `val`

    def testC(self,set_up):
        val = set_up[0]
        logging.info(val)
        # Do - Something, call some function by passing the `val`


obj1 = Test1()
obj1.dummy()      #Explicitly calling it.

pytest -s -v test1.py::Test2

conftest.py

import pytest

def tear_down():
    print "\nTEARDOWN after all tests"


def pytest_addoption(parser):
    parser.addoption(
        "--a", action="store", default=None, help="value A")

    parser.addoption(
        "--b", action="store", default=None, help="value B")

    parser.addoption(
        "--c", action="store", default=None, help="value C")

    parser.addoption(
        "--d", action="store", default=None, help="value D") # Need this value to be available in `dummy`

@pytest.fixture(autouse=True)
def set_up(request):
    print "\nSETUP before all tests"

    if request.function.__name__ == "testA": 
        return([request.config.getoption("--a"),request.config.getoption("--b"),request.config.getoption("--c")])
    elif request.function.__name__ == "testB":
        return ([request.config.getoption("--a"), request.config.getoption("--b"), request.config.getoption("--c")])
    elif request.function.__name__ == "testC":
        return ([request.config.getoption("--a"), request.config.getoption("--b"), request.config.getoption("--c")])

【问题讨论】:

    标签: python pytest optional-parameters


    【解决方案1】:

    以此为参考:

    import sys
    
    
    def dummy():
        print(sys.argv[0])
        print(str(sys.argv))
    
    
    dummy()
    

    【讨论】:

    • pytest -s -v test1.py::Test2 --a valueA --b valueB --d valueD ,在这种情况下如何将值传递给'dummy'?
    【解决方案2】:

    你可以使用pytest_addoption:

    def pytest_addoption(parser):
        parser.addoption("--dummy", action="store", default="dummy")
    

    它添加了一个名为dummy 的新命令行选项,然后可以通过以下方式在夹具中访问:

    request.config.getoption("--dummy")
    

    例如

    @pytest.fixture
    def dummy(request):
        dummy_val = request.config.getoption("--dummy")
    

    请注意,以这种方式使您的虚拟函数成为一个固定装置将允许您在所有测试之前以通常的 pytest 方式自动运行。

    【讨论】:

    • 如果您查看 test1.py,您将如何在任何测试执行之前调用此 dummy 方法?
    • 我将dummy 保留在test1.py 中的原因是使dummy_val 成为一个全局变量,并且dummy 通过获取test1.py 中存在的所有方法来创建一个文件夹。这可以通过在夹具中制作吗?
    • @StackGuru 你可以做类似@pytest.fixture(scope="session", autouse=True) 的事情,我认为这应该让 dummy 表现得像你需要的那样?
    • dummyclass Test1 中,我通过了Test2 的所有情况,所以明确调用 dummy。
    • @StackGuru 我不是 100% 清楚应该使用什么 dummy,但在我看来它应该存在于您的 conftest 中。查看the documentation 了解一些可能对您有所帮助的想法。我也不明白set_up 夹具 - 它似乎对所有测试都在做同样的事情?
    猜你喜欢
    • 2020-07-02
    • 2020-10-26
    • 2019-06-01
    • 1970-01-01
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多