开发者

Test Suite unittest

开发者 https://www.devze.com 2022-12-17 12:04 出处:网络
I have a test suite and I am trying to get it to work with the tests I have created. The test work if I run them individually but I want to run them all in a test suite. The code below show the test s

I have a test suite and I am trying to get it to work with the tests I have created. The test work if I run them individually but I want to run them all in a test suite. The code below show the test suite created:

import unittest

def suite():
    modules_to_test = ('TestAbsoluteMove', '开发者_JAVA百科TestContinuousMove') # and so on
    alltests = unittest.TestSuite()
    for module in map(__import__, modules_to_test):
        alltests.addTest(unittest.findTestCases(module))
    return alltests

if __name__ == '__main__':
    unittest.main(defaultTest='suite')

I have placed this code into my test code to link up with the suite:

class AbsoluteMoveTestSuite(unittest.TestSuite):

def makeAbsoluteMoveTestSuite():
    suite = unittest.TestSuite()
    suite.addTest(TestAbsoluteMove("BasicAbsolutePan"))
    suite.addTest(TestAbsoluteMove("VerifyAbsolutePan"))
    suite.addTest(TestAbsoluteMove("VerifyAbsoluteTilt"))
    suite.addTest(TestAbsoluteMove("VerifyAbsolutePanSpeed"))
    suite.addTest(TestAbsoluteMove("VerifyAbsoluteTiltSpeed"))
    return suite

def suite():
    return unittest.makeSuite(TestAbsoluteMove)

The error that is produced claim that there is no modules named 'TestAbsoluteMove' and TestContinuousMove'. Does anyone know how to get this code working?

Thanks


Nose makes this sort of thing a no-brainer. It will auto-detect your tests and run them as a suite. (You can also run specific tests by passing it a flag.)


TestAbsoluteMove is a class, and it needs to come from somewhere. Wherever your AbsoluteMoveTestSuite class is defined, you need to import TestAbsoluteMove.


this is how I create my testsuite (loadTestFromTestCase detects your tests automatically)

def suite():
    """ returns all the testcases in this module """
    return TestLoader().loadTestsFromTestCase(AbsoluteMoveTestSuite)

and to run them all at once I have a suite that incorporates all subsuites (notice all the imports, you will need to import them before they are available in your new module)

import sys
import unittest

import test.framework.asyncprocess as a
import test.framework.easyconfig as e
import test.framework.modulegenerator as mg
import test.framework.modules as m
import test.framework.filetools as f
import test.framework.repository as r
import test.framework.robot as robot
import test.framework.easyblock as b
import test.framework.variables as v
import test.framework.github as g
import test.framework.toolchainvariables as tcv
import test.framework.toolchain as tc
import test.framework.options as o
import test.framework.config as c


# call suite() for each module and then run them all
SUITE = unittest.TestSuite([x.suite() for x in [r, e, mg, m, f, a, robot, b, v, g, tcv, tc, o, c]])

# uses XMLTestRunner if possible, so we can output an XML file that can be supplied to Jenkins
xml_msg = ""
try:
    import xmlrunner  # requires unittest-xml-reporting package
    xml_dir = 'test-reports'
    res = xmlrunner.XMLTestRunner(output=xml_dir, verbosity=1).run(SUITE)
    xml_msg = ", XML output of tests available in %s directory" % xml_dir
except ImportError, err:
    sys.stderr.write("WARNING: xmlrunner module not available, falling back to using unittest...\n\n")
    res = unittest.TextTestRunner().run(SUITE)


unittest is a bit of a pain to use like this. I would very much reommend that you follow Alison's advice a look at nose or use my personal favourite Python testing tool py.test. Just create functions following a specific naming convention and let it rip! The whole xUnit thing doesn't really fit into Python territory.

0

精彩评论

暂无评论...
验证码 换一张
取 消