I have a main() function in python that gets command line arguments. Is there a way for me to write pytest tests for this function and define the arguments in the code?
To add to the previous answers, instead of modifying sys.argv
It is safer to use a context manager which can cover up and protect the underlying object. An example would be
with unittest.mock.patch('sys.argv', ['program_name', '--option1', 'inputFile']):
main()
This works only with python3. For python2 the Mock library does the trick.
I found this solution on a different stackoverflow post here.