chanwcom
chanwcom

Reputation: 4660

How to test a python binary in the python unit test?

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?

Upvotes: 0

Views: 1359

Answers (2)

aghast
aghast

Reputation: 15310

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.

Upvotes: 1

Amin Etesamian
Amin Etesamian

Reputation: 3699

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.

Upvotes: 1

Related Questions