TestSuite with testsuites and testcases
Asked Answered
S

1

5

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!

Southwestwards answered 9/8, 2011 at 9:0 Comment(0)
P
12

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.

Perrie answered 9/8, 2011 at 9:15 Comment(5)
Thank you, I have given more additional infoSouthwestwards
Thanks a lot! You have solved my problem! Maybe you can help me with another thing: I want to finish the big suitecase if one of the suitecase or testcase inside fail. At the moment, if one of them fail, the big suitecase continue with the next suitcase/testcase.Southwestwards
unittest has a -f option (fail_fast) option so running: python -m unittest -f mainTest.suite fro the above mainTest class should do that. I don't know any other straight forward options for that.Perrie
@Perrie In these tests we are hard coding browser to firefox, is there a way TestSuite can take arguments from command line and then send to the all TestCase, so that we can run the tests on desired browsers? Could you please take a look at this question, I am really struggling with this? #37675301Faefaeces
@GabrielQuesada Could you please help me if you achieved this? Please take a look at this questions #37675301Faefaeces

© 2022 - 2024 — McMap. All rights reserved.