43

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?

Braiam
4,49611 gold badges50 silver badges83 bronze badges
asked May 31, 2013 at 18:39
0

4 Answers 4

84

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.

Anton Tarasenko
8,53512 gold badges69 silver badges98 bronze badges
answered May 31, 2013 at 21:21
Sign up to request clarification or add additional context in comments.

Comments

17

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)
answered May 31, 2013 at 20:28

Comments

13

You must make sure that you consistently follow some naming conventions (which you seem to be doing):

  1. 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

  2. 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.

answered May 31, 2013 at 21:13

Comments

1

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.

Peter Mortensen
31.5k22 gold badges110 silver badges134 bronze badges
answered Dec 2, 2020 at 22:48

1 Comment

that actually worked for me.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.