Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TestSuite with testsuites and testcases

I need to make a big python suitecase consisted of other suitcases and testcase which I have already made to execute together.

How do I do this?

For example, here there is a suitecase (suiteFilter.py) which I want to add:

import testFilter1
import testFilter2
import unittest
import sys

def suite():
    return unittest.TestSuite((\
        unittest.makeSuite(testFilter1.TestFilter1),
        unittest.makeSuite(testFilter2.TestFilter2),
        ))


if __name__ == "__main__":
    result = unittest.TextTestRunner(verbosity=2).run(suite())
    sys.exit(not result.wasSuccessful())

And a testcase structure (Invoice.py):

from selenium import selenium
import unittest, time, re
from setup_tests import filename, fileForNrTest, username, password, server_url
fileW=open(filename,'a')


class TestInvoice(unittest.TestCase):

    def setUp(self):
        self.verificationErrors = []
        self.selenium = selenium("localhost", 4444, "*firefox", server_url)
        self.selenium.start()

    def test_invoice(self):
        sel = self.selenium
        [...] 

    def tearDown(self):
        self.selenium.stop()
        self.assertEqual([], self.verificationErrors)


    if __name__ == "__main__":
        unittest.main()

Thank you!

like image 379
Gabriel Quesada Avatar asked Aug 09 '11 09:08

Gabriel Quesada


1 Answers

You could give some additional information like the structure of your program / test cases and suites. The way I do it is define a suite() for each module. So I have say for UserServiceTest module:

def suite():
    """
        Gather all the tests from this module in a test suite.
    """
    test_suite = unittest.TestSuite()
    test_suite.addTest(unittest.makeSuite(UserServiceTest))
    return test_suite

if __name__ == "__main__":
    #So you can run tests from this module individually.
    unittest.main()   

Then I have a main test for each package:

def suite():
"""
    Gather all the tests from this package in a test suite.
"""
    test_suite = unittest.TestSuite()
    test_suite.addTest(file_tests_main.suite())
    test_suite.addTest(userservice_test.suite())
    return test_suite


if __name__ == "__main__":
    #So you can run tests from this package individually.
    TEST_RUNNER = unittest.TextTestRunner()
    TEST_SUITE = suite()
    TEST_RUNNER.run(TEST_SUITE)

You can do this the recursevly until the root of your project. So main test from package A will gather all module in package A + main test from subpackages of package A and so on. I was assuming you-re using unittest since you didn't give any additional details but I think this structure can be applied to other python testing frameworks as well.


Edit: Well I'm not quite sure I fully understand your problem, but from what I can understand you want to add both the suite defined in suiteFilter.py and the testcase defined in Invoice.py in the same suite? If so why not just do in a mainTest.py for example:

import unittest
import suiteFilter
import Invoice


def suite()
    test_suite = unittest.TestSuite()
    test_suite.addTest(suiteFilter.suite())
    test_suite.addTest(unittest.makeSuite(Invoice))


if __name__ == "__main__":
    result = unittest.TextTestRunner(verbosity=2).run(suite())
    sys.exit(not result.wasSuccessful())

You can add tests and suites all the same to a test_suite.

like image 128
Bogdan Avatar answered Oct 01 '22 23:10

Bogdan