Run unittest from a Python program via a command-line option
Question:
Here is my set up –
project/
__init__.py
prog.py
test/
__init__.py
test_prog.py
I would like to be able to run my unit tests by calling a command-line option in prog.py. This way, when I deploy my project, I can deploy the ability to run the unit tests at any time.
python prog.py --unittest
What do I need in prog.py, or the rest of my project for this to work?
Answers:
Perhaps this is what you’re looking for. Implement a load_tests
function in test_prog.py
and use the following code in prog.py
to load and run the tests:
import unittest
import test.test_prog
suite = unittest.TestLoader().loadTestsFromModule(test.test_prog)
unittest.TextTestRunner().run(suite)
You must make sure that you consistently follow some naming conventions (which you seem to be doing):
-
All tests are named with the same prefix (test_
is the norm), followed by the name of the module you wish to test.
prog.py
=> test_prog.py
-
Tests reside in test/
directory.
Then you can do something like this:
prog.py
import sys
...
... do module stuff here...
...
if __name__ == "__main__":
# Check if we want to run the tests for this file
if "--unittest" in sys.argv:
import unittest
test_filename = 'test_' + __file__
test_directory = 'test'
suite = unittest.TestLoader().discover(test_directory, pattern=test_filename)
unittest.TextTestRunner(verbosity=2).run(suite)
What we are doing, is:
-
Checking the command arguments to see if --unittest
is present (since that’s the only time you want to run the tests).
-
If it is, then we create the test_prog.py
– following the naming conventions we have set.
-
Then we pass that to the TestLoader().discover
function.
discover(…) starts at the specified directory and finds all test modules (recursing into subdirectories ) that match the pattern provided.
In our case, it will look inside the test/
directory for any module named test_prog.py
. When it does, it loads it and creates a TestSuite with the TestCases that we want to run.
-
Lastly, we manually test unittest
to run the suite
obtained in the previous step.
Normally, unittest
will do all of this for us in the background, but since we are trying to run a specific test module, we have to tell exactly how and where to get it from.
Also, note that you will have to do this for every file where you want to do this at.
The Python unittest
module contains its own test discovery function, which you can run from the command line:
$ python -m unittest discover
To run this command from within your module, you can use the subprocess
module:
#!/usr/bin/env python
import sys
import subprocess
# ...
# the rest of your module's code
# ...
if __name__ == '__main__':
if '--unittest' in sys.argv:
subprocess.call([sys.executable, '-m', 'unittest', 'discover'])
If your module has other command-line options you probably want to look into argparse
for more advanced options.
I just removed the .idea
hidden directory under the project in the file system.
I restarted PyCharm, reopened the project, and this issue was resolved.
Here is my set up –
project/
__init__.py
prog.py
test/
__init__.py
test_prog.py
I would like to be able to run my unit tests by calling a command-line option in prog.py. This way, when I deploy my project, I can deploy the ability to run the unit tests at any time.
python prog.py --unittest
What do I need in prog.py, or the rest of my project for this to work?
Perhaps this is what you’re looking for. Implement a load_tests
function in test_prog.py
and use the following code in prog.py
to load and run the tests:
import unittest
import test.test_prog
suite = unittest.TestLoader().loadTestsFromModule(test.test_prog)
unittest.TextTestRunner().run(suite)
You must make sure that you consistently follow some naming conventions (which you seem to be doing):
-
All tests are named with the same prefix (
test_
is the norm), followed by the name of the module you wish to test.prog.py
=>test_prog.py
-
Tests reside in
test/
directory.
Then you can do something like this:
prog.py
import sys
...
... do module stuff here...
...
if __name__ == "__main__":
# Check if we want to run the tests for this file
if "--unittest" in sys.argv:
import unittest
test_filename = 'test_' + __file__
test_directory = 'test'
suite = unittest.TestLoader().discover(test_directory, pattern=test_filename)
unittest.TextTestRunner(verbosity=2).run(suite)
What we are doing, is:
-
Checking the command arguments to see if
--unittest
is present (since that’s the only time you want to run the tests). -
If it is, then we create the
test_prog.py
– following the naming conventions we have set. -
Then we pass that to the
TestLoader().discover
function.discover(…) starts at the specified directory and finds all test modules (recursing into subdirectories ) that match the pattern provided.
In our case, it will look inside the
test/
directory for any module namedtest_prog.py
. When it does, it loads it and creates a TestSuite with the TestCases that we want to run. -
Lastly, we manually test
unittest
to run thesuite
obtained in the previous step.
Normally, unittest
will do all of this for us in the background, but since we are trying to run a specific test module, we have to tell exactly how and where to get it from.
Also, note that you will have to do this for every file where you want to do this at.
The Python unittest
module contains its own test discovery function, which you can run from the command line:
$ python -m unittest discover
To run this command from within your module, you can use the subprocess
module:
#!/usr/bin/env python
import sys
import subprocess
# ...
# the rest of your module's code
# ...
if __name__ == '__main__':
if '--unittest' in sys.argv:
subprocess.call([sys.executable, '-m', 'unittest', 'discover'])
If your module has other command-line options you probably want to look into argparse
for more advanced options.
I just removed the .idea
hidden directory under the project in the file system.
I restarted PyCharm, reopened the project, and this issue was resolved.