2

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'

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
ti01878
  • 483
  • 1
  • 5
  • 18

1 Answers1

3

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()])
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Probalby it is better, but still getting: AttributeError: 'module' object has no attribute 'addTest' – ti01878 Jun 24 '14 at 13:53
  • @ti01878 oops, sure, fixed the mistake. Thanks. Note that the code assumes `step1`, `step2` are test cases. – alecxe Jun 24 '14 at 13:54
  • 1
    Well, my code was so bad that I have to do 2 things more: 1) http://stackoverflow.com/questions/19087189/python-unittest-testcase-object-has-no-attribute-runtest and 2) http://stackoverflow.com/questions/18928826/typeerror-module-object-is-not-callable-for-python-object, but finnaly it works :) Thank you. – ti01878 Jun 24 '14 at 14:17