Python unittesting: run tests in another module

安稳与你 提交于 2019-12-03 10:48:44

In test_something.py, do this:

def suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TestMyUnit, 'test'))
    return suite

In testController.py, do this:

from TestUnits import test_something

def suite():
    suite = unittest.TestSuite()
    suite.addTest(test_something.suite())
    return suite

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

The method unittest.main() looks at all the unittest.TestCase classes present in the context. So you just need to import your test classes in your testController.py file and call unittest.main() in the context of this file.

So your file testController.py should simply look like this :

import unittest    
from UnitTests.test_something import *
unittest.main()

There is a workaround of using subprocess.call() to run tests, like:

import subprocess

args = ["python", "test_something.py"]
subprocess.call(args)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!