Skip to content

Instantly share code, notes, and snippets.

@santiycr
Created December 13, 2012 06:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save santiycr/4274570 to your computer and use it in GitHub Desktop.
Save santiycr/4274570 to your computer and use it in GitHub Desktop.
Run python unit tests from a different python script
import unittest
import test_example
unittest.main(module=test_example)
import unittest
class myTest(unittest.TestCase):
def test_dummy(self):
assert True
@nodarai
Copy link

nodarai commented Jun 8, 2020

This fails if import_and_run.py has main function with arguments.
To solve that, need to override sys.argv. something like:

if  __name__ == '__main__':
    import sys
    sys.argv = [sys.argv[0]]
    unittest.main(module=test_example)

@santiycr
Copy link
Author

santiycr commented Jun 8, 2020

thanks for sharing the fix!

@kramarov-evg
Copy link

kramarov-evg commented Aug 5, 2021

If changing sys.argv is not an option, one can use unittest.TextTestRunner as described in this StackOverflow answer. IMHO, that's a more convenient way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment