Trying to implement python TestSuite
Asked Answered
W

4

39

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?

Withers answered 17/8, 2012 at 18:15 Comment(0)
M
59

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)
Melliemelliferous answered 17/8, 2012 at 18:46 Comment(5)
Thank you, thank worked perfectly for what I needed. Thanks a bunch!Withers
Thank you so much for this answer. Why on earth do I need to call unittest.makeSuite in order to add a test to an existing suite?Faker
In the comments, it's Gather or Gathers?Mauchi
@OlivierPons its gather. Advises the testerMelliemelliferous
Ok I thought "Gather all the tests from this module in a test suite." meant "This function gathers all the tests from this module in a test suite." (That's why I thought an s should be added but I'm very bad in english, sorry)Mauchi
C
9

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.

Carny answered 17/8, 2012 at 18:44 Comment(2)
I'm running into some weird test isolation issues using unittest discover, which don't seem to show up if I run a single test file or one TestCase class at a time. I'm not sure if it's a race condition or what, but it's one situation where a defined TestSuite might be helpful.Domett
@Carny Is there any reason for suites? As you said python -m unittest is enough, if proper names are used.Giliana
B
3

If you are trying to manually collect TestCases, this is useful: unittest.loader.findTestCases():

# Given a module, M, with tests:
mySuite = unittest.loader.findTestCases(M)
runner = unittest.TextTestRunner()
runner.run(mySuit)
Backfire answered 25/11, 2013 at 17:8 Comment(0)
B
1

I am assuming you are referring to running python-unit test against the module that consolidates the two test. It will work if you create test case for that module ie. subclassing unittest.TestCase and having a simple test that starts with the word 'test'.

e.g.

class testall(unittest.TestCase):

    def test_all(self):           
        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"

if __name__ == "__main__": 
      unittest.main()
Blayze answered 17/8, 2012 at 19:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.