Python unittest:在一个循环中运行多个断言,第一个断言不会失败,但要继续

2024-05-29 02:22:12 发布

您现在位置:Python中文网/ 问答频道 /正文

场景: 我的一个测试用例是用几个输入文件和一个特定的输出执行一个shell程序。我想测试这些输入/输出的不同变体,每个变体都保存在自己的文件夹中,即文件夹结构

/testA
/testA/inputX
/testA/inputY
/testA/expected.out
/testB
/testB/inputX
/testB/inputY
/testB/expected.out
/testC/
...

以下是我运行此测试的代码:

^{pr2}$

本例中的testname是“testX”。它使用该文件夹中的inputs执行外部程序,并将其与同一文件夹中的expected.out进行比较。在

问题: 一旦测试失败,测试就会停止并得到:

Could not find or read expected output file: C:/PROJECTS/active/CMDR-Test/data/test_freeTransactional/expected.out
======================================================================
FAIL: test_folders (cmdr-test.TestValidTypes)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\PROJECTS\active\CMDR-Test\cmdr-test.py", line 52, in test_folders
    self.assertTrue(self.runTest(testname))
AssertionError: False is not true

问题: 我如何让它继续剩下的测试并显示有多少测试失败? (本质上是动态地创建一个unittest并执行它)

谢谢


Tags: test程序文件夹not变体outprojectsactive
1条回答
网友
1楼 · 发布于 2024-05-29 02:22:12

这样的怎么样?在

def test_folders(self):
    tfolders = glob.iglob(os.environ['testdir'] + '/test_*')    

    failures = []    
    for testpath in tfolders:
        testname = os.path.basename(testpath)
        if not self.runTest(testname):
            failures.append[testname]

    self.assertEqual([], failures)

相关问题 更多 >

    热门问题