0

I get some parameters from command lines. Then I want to use these parameters as variables in test cases. I want to use parametrised test in same test case. Is it correct to run it as below?

conftest.py

import pytest

def pytest_adoption(parser):
    parser("--arg1",action="store",help="first argument")
    parser("--arg2),action="store",help="second argument")

example_test.py

import pytest

test_data = [("name1","surname1",40),("name2","surname2",30)]

@pytest.fixture(autouse=True)
def getArg(pytestconfig):
    arg1 = pytestconfig.getoption("arg1")
    arg2 = pytestconfig.getoption("arg2")
    return arg1,arg2

@pytest.mark.parametrize("name,surname,age",test_data)
def test_example(name,surname,age):
    assert type(getArg) == tuple
    arg1,arg2 = getArg
    if arg1 == "first" && arg2 == "second" :
       print('My name is {} {} and I am {} years old'.format(name,surname,age)) 

execution:

pytest example_test.py --arg1 "first" --arg2 "second"
ibra
  • 11
  • 2
  • What do you mean by "correct"? Does it work? Normally I'd not have someone specify (and remember) command line arguments to my tests, they should be self-contained. Or are you trying to test your argparse code? In that case I'd still put the arguments hard-coded into the tests. [codereview.se] may be a better fit for this question. – Robert Jun 08 '22 at 22:35
  • As seen in execution I need 2 command line arguments which are used in test cases. Normally when you @pytest.fixture(autouse=True), you don't need to explicitly add fixture as a parameter to test case but it is needed if parametrize is used. At least it worked for me as seen in the answer. – ibra Jun 09 '22 at 08:40

1 Answers1

0

I found answer as fixture function will be given as a test argument:

@pytest.mark.parametrize("name,surname,age",test_data)
def test_example(getArg,name,surname,age):
    assert type(getArg) == tuple
    arg1,arg2 = getArg
    if arg1 == "first" && arg2 == "second" :
       print('My name is {} {} and I am {} years old'.format(name,surname,age)) 
ibra
  • 11
  • 2