Ad

Grouping All Tests Python

- 1 answer


I have 2 modules: test1.py and test2.py.
test1.py

import unittest

class ArithTest (unittest.TestCase):

    def test_run (self):

        """ Test addition and succeed. """
        self.failUnless (1+1==2, 'one plus one fails!')
        self.failIf (1+1 != 2, 'one plus one fails again!')
        self.failUnlessEqual (1+1, 2, 'more trouble with one plus one!')

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

test2.py

import unittest

class AlgTest (unittest.TestCase):

    def test_alg (self):

        """ Test addition and succeed. """
        self.assertEqual(1+1, 2, '1+1 != 2? whaaat?')
        self.assertEqual(6-5, 1, '6-5 != 5 wft python?')

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

-Now- I wanna create a new module test3.py to test test1.py and test2.py. I don't now how, i read on internet about suit tests but i don't understand. I don't want to create one more method with calling tests, and call them on test3.py. I wanna group them and call in test3.py and they run as unitests

Ad

Answer

test1.py

import unittest

class ArithTest (unittest.TestCase):

    def test_run (self):

        """ Test addition and succeed. """
        self.failUnless (1+1==2, 'one plus one fails!')
        self.failIf (1+1 != 2, 'one plus one fails again!')
        self.failUnlessEqual (1+1, 2, 'more trouble with one plus one!')

    def runTest(self):
        self.test_run()

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

test2.py

import unittest

class AlgTest (unittest.TestCase):

    def test_alg (self):

        """ Test addition and succeed. """
        self.assertEqual(1+1, 2, '1+1 != 2? whaaat?')
        self.assertEqual(6-5, 1, '6-5 != 5 wft python?')

    def runTest(self):
        self.test_alg()

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

test3.py

from .test1 import ArithTest
from .test2 import AlgTest
import unittest

def suite_2():
    suite = unittest.TestSuite()
    suite.addTest(ArithTest())
    suite.addTest(AlgTest())

    return suite

if __name__ == '__main__':
    runner = unittest.TextTestRunner()
    test_suite = suite_2()
    runner.run(test_suite)

Also add a __init__.py

Run it with python3 -m folder_name.test3

Ad
source: stackoverflow.com
Ad