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

dli*_*922 4 python testing unit-testing assertions python-unittest

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

/testA
/testA/inputX
/testA/inputY
/testA/expected.out
/testB
/testB/inputX
/testB/inputY
/testB/expected.out
/testC/
...
Run Code Online (Sandbox Code Playgroud)

这是我运行此测试的代码:

def test_folders(self):
    tfolders = glob.iglob(os.environ['testdir'] + '/test_*')                
    for testpath in tfolders:
        testname = os.path.basename(testpath)
        self.assertTrue(self.runTest(testname))
Run Code Online (Sandbox Code Playgroud)

在这种情况下,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
Run Code Online (Sandbox Code Playgroud)

问题: 如何使其继续其余测试并显示失败的数量?(本质上是动态创建单元测试并执行它)

谢谢

man*_*art 5

这样的事怎么样?

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)
Run Code Online (Sandbox Code Playgroud)