Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing command line arguments in python by pytest

Tags:

python

I am able to pass command line arguments when running

python <filename>.py arg1

But when am trying to pass the command line arguments for running pytest it fails and gives error as below. Can you please advise.

pytest <filename>.py arg1
ERROR: file not found: arg1

EDIT:

For example am thinking of using it this way assuming I have passed an argument and am reading it via sys.argv:

import sys
arg = sys.argv[3]
def f():
    return 3

def test_function():
    assert f() == arg
like image 372
anukb Avatar asked Mar 14 '17 05:03

anukb


1 Answers

Your pytest <filename>.py arg1 command is trying to call pytest on two modules <filename>.py and arg1 , But there is no module arg1.

If you want to pass some argument before running pytest then run the pytest from a python script after extracting your variable.

As others suggested though you would probably want to parameterize your tests in some other way, Try:Parameterized pytest.

# run.py
import pytest
import sys

def main():
    # extract your arg here
    print('Extracted arg is ==> %s' % sys.argv[2])
    pytest.main([sys.argv[1]])

if __name__ == '__main__':
    main()

call this using python run.py filename.py arg1

like image 183
Kashif Siddiqui Avatar answered Oct 11 '22 02:10

Kashif Siddiqui