0
votes

I have no problems with one command line argument to pytest.

When I run the program:
python -m pytest -q -v --confcutdir=/usr/local/penguin/home/px09/p001 --cmdopt=type1 test-suite.py

I get the expected response:

================================================ FAILURES ================================================
______________________________________________ test_answer _______________________________________________

cmdopt = 'type1'

    def test_answer(cmdopt):
        if cmdopt == "type1":
            print ("first")
        elif cmdopt == "type2":
            print ("second")
>       assert 0 # to see what was printed
E       assert 0

test-suite.py:7: AssertionError
------------------------------------------ Captured stdout call ------------------------------------------
first
======================================== 1 failed in 0.01 seconds ==============

When I try multiple arguments I run into issues

content of test_sample.py

def test_answer(cmdopt):
    if cmdopt == "type1":
        print ("first")
    elif cmdopt == "type2":
        print ("second")
    assert 0 # to see what was printed

def test_answer2(cmdopt2):
    if cmdopt2 == "type1":
        print ("first")
    elif cmdopt2 == "type2":
        print ("second")
    assert 0 # to see what was printed

content of conftest.py

import pytest

def pytest_addoption(parser):
    parser.addoption("--cmdopt", action="store", default="type1",
        help="my option: type1 or type2")
    parser.addoption("--cmdopt2", action="store", default="type3",
                     help="my option: type3 or type4")

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

def cmdopt2(request):
    return request.config.getoption("--cmdopt2")

_____________________________________ ERROR at setup of test_answer2 _____________________________________ file /usr/local/penguin/home/px09/p001/test-suite.py, line 9 def test_answer2(cmdopt2): E fixture 'cmdopt2' not found
> available fixtures: cache, capfd, capsys, cmdopt, doctest_namespace, monkeypatch, pytestconfig, record_xml_property, recwarn, tmpdir, tmpdir_factory
> use 'pytest --fixtures [testpath]' for help on them.

/usr/local/penguin/home/px09/p001/test-suite.py:9

================================================ FAILURES ================================================
______________________________________________ test_answer _______________________________________________

cmdopt = 'type1'

    def test_answer(cmdopt):
        if cmdopt == "type1":
            print ("first")
        elif cmdopt == "type2":
            print ("second")
>       assert 0 # to see what was printed
E       assert 0

test-suite.py:7: AssertionError
-
1

1 Answers

2
votes

The problem, I think, is simple. Just add @pytest.fixture decorator before cmdopt2 function

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

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