Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i create a test Suite in python unittest

i tried:

def buildTestSuite():
    suite = unittest.TestSuite()
    for testcase in glob.glob('src/testsuite/test_*.py'):
        module = os.path.splitext(testcase)[0]
        print module
        print type(module)
        suite.addTest(__import__(module).buildTestSuite())
    return suite

but i get eror:

Traceback (most recent call last):
  File "runtests.py", line 63, in ?
    results = main()
  File "runtests.py", line 57, in main
    results = unittest.TextTestRunner().run(buildTestSuite())
  File "runtests.py", line 53, in buildTestSuite
    suite.addTest(__import__(module).buildTestSuite())
AttributeError: 'module' object has no attribute 'buildTestSuite'
like image 590
kamal Avatar asked Nov 02 '11 14:11

kamal


People also ask

How do you make a test suite in Python?

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.

What is setUp in Unittest Python?

setUp allows us to write preparation code that is run for all of our tests in a TestCase subclass. Note: If you have multiple test files with TestCase subclasses that you'd like to run, consider using python -m unittest discover to run more than one test file.

How do you create a test unit?

To get started, select a method, a type, or a namespace in the code editor in the project you want to test, right-click, and then choose Create Unit Tests. The Create Unit Tests dialog opens where you can configure how you want the tests to be created.

What is test suite in unit testing framework?

A test suite is a collection of test cases, test suites, or both. It is used to aggregate tests that should be executed together. test runner. A test runner is a component which orchestrates the execution of tests and provides the outcome to the user.


1 Answers

def buildTestSuite():
    suite = unittest.TestSuite()
    for testcase in glob.glob('src/testsuite/test_*.py'):
        modname = os.path.splitext(testcase)[0]
        module=__import__(modname,{},{},['1'])
        suite.addTest(unittest.TestLoader().loadTestsFromModule(module))
    return suite
like image 114
unutbu Avatar answered Oct 13 '22 00:10

unutbu