Is it possible to run all unit test?
Solution 1
You could create a TestSuite
and run all your tests in it's if __name__ == '__main__'
block:
import unittest
def create_suite():
test_suite = unittest.TestSuite()
test_suite.addTest(fooTest())
test_suite.addTest(barTest())
return test_suite
if __name__ == '__main__':
suite = create_suite()
runner=unittest.TextTestRunner()
runner.run(suite)
If you do not want to create the test cases manually look at this quesiton/answer, which basically creates the test cases dynamically, or use some of the features of the unittest
module like test discovery feature and command line options ..
Solution 2
When running unit tests based on the built-in python unittest module, at the root level of your project run
python -m unittest discover <module_name>
For the specific example above, it suffices to run
python -m unittest discover .
https://docs.python.org/2/library/unittest.html
Solution 3
I think what you are looking for is the TestLoader. With this you can load specific tests or modules or load everything under a given directory. Also, this post has some useful examples using a TestSuite
instance.
EDIT: The code I usually have in my test.py:
if not popts.tests:
suite = unittest.TestLoader().discover(os.path.dirname(__file__)+'/tests')
#print(suite._tests)
# Print outline
lg.info(' * Going for Interactive net tests = '+str(not tvars.NOINTERACTIVE))
# Run
unittest.TextTestRunner(verbosity=popts.verbosity).run(suite)
else:
lg.info(' * Running specific tests')
suite = unittest.TestSuite()
# Load standard tests
for t in popts.tests:
test = unittest.TestLoader().loadTestsFromName("tests."+t)
suite.addTest(test)
# Run
unittest.TextTestRunner(verbosity=popts.verbosity).run(suite)
Does two things:
- If -t flag (tests) is not present, find and load all tests in directory
- Else, load the requested tests one-by-one
Solution 4
I think you could just run the following command under the folder where your tests files are located:
python -m unittest
as mentioned here in the doc that "when executed without arguments Test Discovery is started"
Solution 5
With PyDev right click on a folder in Eclipse and choose "Run as-> Python unit-test". This will run all tests in that folder (the names of the test files and methods have to start with "test_".)
caren vanderlee
Updated on June 09, 2022Comments
-
caren vanderlee almost 2 years
I have two module with two different classes and their corresponding test classes.
foo.py ------ class foo(object): def fooMethod(self): // smthg bar.py ------ class bar(object): def barMethod(self): // smthg fooTest.py ------ class fooTest(unittest.TestCase): def fooMethodTest(self): // smthg barTest.py ------ class barTest(unittest.TestCase): def barMethodTest(self): // smthg
In any, test and source module, file, I erased the
if __name__ == "__main__":
because of increasing coherency and obeying object-oriented ideology.Like in Java unit test, I'm looking for creating a module to run all unittest. For example,
runAllTest.py ------------- class runAllTest(unittest.TestCase): ????? if __name__ == "__main__": ?????
I looked for search engine but didn't find any tutorial or example. Is it possible to do so? Why? or How?
Note: I'm using eclipse and pydev distribution on windows machine.
-
aaron about 5 yearsBy far the best, most concise approach on here. Good tip, @mcguip