0

I would like to test a python binary "main.py" with command line arguments in the unit test. The usage of this binary is as follows.

main.py --input_file=input.txt --output_file=out.txt

When designing unit tests, I think it is better to test each component like a class or a method.

However, in some cases like the above one, I would like to do end-to-end testing of the whole python binary especially when it is already created by someone else. In the above case, I want to make it sure whether "main.py" generates "out.txt" correctly.

One option is using subprocess.check_call and create it to a temporary directory, and loads it and compares it with the golden (expected output).

Is it a good way?

Or, if there is any better way, could you please advise me?

1
  • As a side note: a .py file is not a "binary". The C python interpreter is one. And don't mix unit test and end to end tests. Keep them separated. Commented Aug 12, 2017 at 23:15

2 Answers 2

1

This is called blackbox testing as the inner structure of the program is unknown to the tester. If you insist on testing the module without getting to know what's happening inside, You could (as you mentioned) use exec or subprocess to check the validity of the output. But the more logical way is to use unittest libraries and try to test the code using the API it provides.

Sign up to request clarification or add additional context in comments.

Comments

1

If you're testing the handling of arguments as part of the unit tests, just look inside main.py and see how it handles arguments.

For example, you might have a test case that sets sys.argv and then calls main:

import sys

import myapp.main

sys.argv = '''main.py --input_file=input.txt --output_file=out.txt'''.split()

myapp.main.main()

# I have no idea what test you want to run.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.