0

on MacOS, I want to run pytest in command line:

python3 -m pytest test_function_validation.py 

However, test_function_validation.py has got argparser in it written as:

import pytest
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-f', action='store', nargs=1, dest='file_to_read')
args = parser.parse_args()
file_to_read = args.file_to_read[0] 
...etc

Question: I was running

python3 -m pytest test_function_validation.py -f input_data.json

And it complains:

pytest.py: error: unrecognized arguments: -f input_data.json

I also tried:

pytest test_function_validation.py -f input_data.json

which result in error:

-bash: /usr/local/bin/pytest: /usr/local/opt/python/bin/python3.6: bad interpreter: No such file or directory

So, how can run a pytest with some arguments passed into the test file . I need to do this in command line in terminal.

Kid_Learning_C
  • 2,605
  • 4
  • 39
  • 71
  • 1
    Possible duplicate of [How to pass arguments in pytest by command line](https://stackoverflow.com/questions/40880259/how-to-pass-arguments-in-pytest-by-command-line) – ChrisGPT was on strike Mar 29 '19 at 15:50
  • 1
    `pytest` is running its own `argparse` (or equivalent), so you can only use commandline arguments that it accepts. Your own script should be invoking the parser from a `if __name__` block so doesn't run when the script is imported. – hpaulj Mar 29 '19 at 16:28
  • 1
    A workaround: import sys // sys.argv = ['myscript.py', '-f', 'my_file.txt'] // parser = argparse.ArgumentParser() // ... – dasWesen Aug 16 '19 at 16:19
  • - but you say you need to pass the params via command line, so this is probably not a solution. – dasWesen Aug 16 '19 at 16:20

0 Answers0