ti01878
ti01878

Reputation: 483

How to: setting a testsuite in python

I know it is a bit silly question, but using links provides below, I am still unable to create testsuite.

I have now two test cases (there will be much more), let assume that the name of there are:

class step1(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()

    def test_case1(self):
[...]

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

and:

class step2(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()

    def test_case2(self):
[...]

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

I want to create other file .py file: testsuite, which can aggregate test_case1, test_case2, test_case3...

I tried something like that, for example:

import unittest
import step1
import step2


def suite():
    test_suite = unittest.TestSuite()
    test_suite.addTest(unittest.step1(test_case1))
    test_suite.addTest(unittest.step2(test_case2))

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

Error: AttributeError: 'module' object has no attribute 'step1'

Upvotes: 2

Views: 1210

Answers (1)

alecxe
alecxe

Reputation: 474241

You can use addTest() and pass TestCase instance to it, you also miss return statement:

def suite():
    test_suite = unittest.TestSuite()
    test_suite.addTest(step1())
    test_suite.addTest(step2())
    return test_suite

or, in one line using addTests():

test_suite.addTests([step1(), step2()])

Upvotes: 3

Related Questions