Use console_scripts to call a function which uses arguments using pytest_addoption

Viewed 17

I have a function (defined in test.py) which takes arguments using pytest_addoptions and in addition, I use fixtures to supply arguments to that function. I want to add this function as a command line script. This I do using console_scripts in setup.py. However, I get error:

load_entry_point('swatch==0.3.7', 'console_scripts', 'run-code')() TypeError: test_function() missing 3 required positional arguments: 'arg1', 'arg2', 'arg3'

Below I list the content of my code and the command I use to run the script.

(1) Contents of test.py

@pytest.fixture
def arg1(request):
    arg1_ans = request.config.getoption("arg1")
    return arg1_ans

@pytest.fixture
def arg2(request):
    arg2_ans = request.config.getoption("arg2")
    return arg2_ans

def test_function(arg1, arg2, arg3):
     print "Just a dummy test"
   .....

Then in conftest.py, I have

def pytest_addoption(parser):
    parser.addoption('--arg1', help='arg1')
    parser.addoption('--arg2', help='arg2')
    parser.addoption('--arg3', help='arg3')

def pytest_generate_tests(metafunc):

    if "arg1" in metafunc.fixturenames:
        option_value = metafunc.config.option.arg1
        metafunc.parametrize("arg1", [option_value])

    if "arg2" in metafunc.fixturenames:
        option_value = metafunc.config.option.arg2
        metafunc.parametrize("arg2", [option_value])

    if "arg3" in metafunc.fixturenames:
        option_value = metafunc.config.option.arg3
        metafunc.parametrize("arg3", [option_value])

(2) Relevant Contents of setup.py

entry_points={
        'console_scripts': [
            'run-code = path1.path2:test_function',
        ]
    },

(3) I use the following command to run this code

run-code --arg2 value2 --arg1 value1 --arg3 value3

Any pointers/suggestions would be appreciated.

Thanks

0 Answers
Related