我有2个模块:test1.py和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()




-现在-
我想创建一个新模块test3.py来测试test1.py和test2.py。我现在不知道如何,我在网上阅读了有关西服测试的信息,但我听不懂。
我不想再创建一个调用测试的方法,并在test3.py上调用它们。
我想将它们分组并调用test3.py,它们以unitests的身份运行

最佳答案

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)


还要添加一个__init__.py

python3 -m folder_name.test3运行

关于python - 分组所有测试Python,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34258830/

10-10 08:25