Update Jan 2025, Python 3
This error is happening becuse you have not add Test in Suite
suite = unittest.TestSuite()
suite.addTest(Test()) ^<----- Need to pass method name to run here
unittest.TextTestRunner().run(suite)
Complet Answer with example
Suppose you have a Test class with 2 tests:
import unittest
class TestMainApp(unittest.TestCase):
def setUp(self):
print("staring TestMainApp")
def tearDown(self):
print("stopping TestMainApp")
def test_something(self):
print("testing something")
self.assertEqual(True, True) # add assertion here
def test_something_else(self):
print("testing something else")
self.assertEqual(True, True) # add assertion here
if __name__ == '__main__':
unittest.main()
2 Ways to create Test Suite
Now to create test suite you can either instruct to include all methods in Test class and you can disable tests in test class itself.
OR
Or you can you can select methods you want to run under a test suite.
Here is an example of both. I am loading slected tests by calling load_tests method here
import unittest
from tests.app.app_test import TestMainApp
def create_suite():
# Load and Run partial Test
testToRun = [TestMainApp(TestMainApp.test_something.__name__)]
test_suite = load_tests(testToRun)
# Uncomment below to Load and Run all Tests
# test_suite = load_all_tests()
return test_suite
def load_tests(tests_to_run):
test_suite = unittest.TestSuite()
# Add selected tests to the test suite
for test in tests_to_run:
test_suite.addTest(test)
return test_suite
def load_all_tests():
# Add all tests to the test suite
test_suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestMainApp)
return test_suite
if __name__ == '__main__':
unittest.TextTestRunner().run(create_suite())
Output of creating suite with load_tests will ignore test_something_else
staring TestMainApp
testing something
stopping TestMainApp
Output with load_all_tests method to create test suite will include all tests:
staring TestMainApp
testing something
stopping TestMainApp
staring TestMainApp
testing something else
stopping TestMainApp