我可以通过 "runpy.run_module" 呼叫 "python -m unittest test_code" 吗?

Can I call "python -m unittest test_code" by "runpy.run_module"?

我正在学习 python 的单元测试。

我了解到我可以 运行 测试,test_code,

python -m unittest test_code

从命令行。

现在我想 运行 在 python 脚本中进行单元测试。 我了解到 "runpy.run_module()" 对应于 "python -m"。 但是我不明白如何为单元测试提供参数 以 "runpy.run_module()" 的方式。 也就是说,

runpy.run_module(unittest)  # where should I put 'test_code'?

我可以 运行 在 python 脚本中使用 test_code 和 runpy.run_module() 进行单元测试吗?

非常感谢。

这里有一些方法:

import unittest

import tests # where my unit tests are at
import tests_copy # where my unit tests are at

# make a collection of TestCases
suit = unittest.TestSuite()

# add all testcases in tests module
suit.addTest(unittest.defaultTestLoader.loadTestsFromName('tests'))

# add testcase tester1 from module  tests_copy
suit.addTest(unittest.defaultTestLoader.loadTestsFromName('tests_copy.tester1'))

# add all testcases in tests module
suit.addTest(unittest.defaultTestLoader.loadTestsFromModule(tests))

# add testcase tester1 from module tests_copy
suit.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(tests.tester1))


# run the tests
runner = unittest.TextTestRunner()
runner.run(suit)

阅读docs