我已经编写了一个BaseTestCase
类(打算由所有测试用例继承)和许多TestCase<Number>(BaseTestCase)
类(每个都在其自己的.py文件中)。
项目结构为:
示例:
class TestCase2(BaseTestCase):
# methods
def execute(self):
self.method1()
self.method2()
self.method3()
应该在execute
的实例上调用TestCase<Number>
方法以运行特定的测试。运行所有测试用例(即将它们全部编排)的正确方法是什么?
答案 0 :(得分:0)
我假设您正在使用Unittest框架,并且希望以编程方式运行测试,并能够选择要运行的测试。如果那是正确的假设,那么答案就在这里。
让我们假设您已经进行了这些测试:
class TestExample1(unittest.TestCase):
def test_something(self):
pass
def test_something_else(self):
pass
class TestExample2(unittest.TestCase):
def test_something(self):
pass
def test_something_else(self):
pass
如果这些文件位于同一文件中,则可以像这样简单地运行它们
# assuming your tests are above in this same file
unittest.main()
但是,如果它们位于不同的文件中,则必须像这样从它们中提取测试
for example in [TestExample1, TestExample2]:
tests = unittest.TestLoader().loadTestsFromTestCase(example)
unittest.TextTestRunner().run(tests)
这将导致两个不同的执行,像这样
..
----------------------------------------------------------------------
Ran 2 tests in 0.001s
OK
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
如果您正在考虑用于运行测试的其他框架,建议您看一下Test Junkie:https://www.test-junkie.com/get-started/,因为它更容易上手并且功能强大。 (免责声明:我是作者!)。