I have two test cases (two different files) that I want to run together in a Test Suite. I can get the tests to run just by running python "normally" but when I select to run a python-unit test it says 0 tests run. Right now I'm just trying to get at least one test to run correectly.
import usertest import configtest # first test import unittest # second test testSuite = unittest.TestSuite() testResult = unittest.TestResult() confTest = configtest.ConfigTestCase() testSuite.addTest(configtest.suite()) test = testSuite.run(testResult) print testResult.testsRun # prints 1 if run "normally"
Here's an example of my test case set up
class ConfigTestCase(unittest.TestCase): def setUp(self): ##set up code def runTest(self): #runs test def suite(): """ Gather all the tests from this module in a test suite. """ test_suite = unittest.TestSuite() test_suite.addTest(unittest.makeSuite(ConfigTestCase)) return test_suite if __name__ == "__main__": #So you can run tests from this module individually. unittest.main()
What do I have to do to get this work correctly?
The following steps are involved in creating and running a test suite. Step 1 − Create an instance of TestSuite class. Step 2 − Add tests inside a TestCase class in the suite. Step 4 − Individual tests can also be added in the suite.
You can invoke testing through the Python interpreter from the command line: python -m pytest [...] This is almost equivalent to invoking the command line script pytest [...] directly, except that calling via python will also add the current directory to sys.
you want to use a testsuit. So you need not call unittest.main(). Use of testsuit should be like this:
#import usertest #import configtest # first test import unittest # second test class ConfigTestCase(unittest.TestCase): def setUp(self): print 'stp' ##set up code def runTest(self): #runs test print 'stp' def suite(): """ Gather all the tests from this module in a test suite. """ test_suite = unittest.TestSuite() test_suite.addTest(unittest.makeSuite(ConfigTestCase)) return test_suite mySuit=suite() runner=unittest.TextTestRunner() runner.run(mySuit)
All of the code to create a loader and suite is unnecessary. You should write your tests so that they are runnable via test discovery using your favorite test runner. That just means naming your methods in a standard way, putting them in an importable place (or passing a folder containing them to the runner), and inheriting from unittest.TestCase
. After you've done that, you can use python -m unittest discover
at the simplest, or a nicer third party runner to discover and then run your tests.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With