Trying to implement python TestSuite

我只是一个虾纸丫 提交于 2019-11-28 06:47:53
Dileep Nandanam

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.

Cheng

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()

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!