在第一次断言失败后,我可以测试测试的所有断言吗?

mar*_*rue 1 python django unit-testing

我有一个不同的audioformats列表,应该转换某个文件.我编写的转换函数现在应该转换文件并返回成功信息,新创建文件的路径或一些失败信息.

self.AUDIO_FORMATS = ({'format':'wav', 'samplerate':44100, 'bitdepth':16 },
                      {'format':'aac', 'samplerate':44100, 'bitdepth':16 },
                      {'format':'ogg', 'samplerate':44100, 'bitdepth':16 },
                      {'format':'mp3', 'samplerate':44100, 'bitdepth':16 } )
Run Code Online (Sandbox Code Playgroud)

由于其中一个转换失败的一个可能原因是缺少库,或者这样的库或我的实现中的一些错误或失败,我想测试每个转换到最后都有一个通过和失败的测试列表,失败者告诉我究竟哪个转换导致了麻烦.这是我试过的(有点简化):

def test_convert_to_formats(self):

    for options in self.AUDIO_FORMATS:
        created_file_path, errors = convert_audiofile(self.audiofile,options)
        self.assertFalse( errors )
        self.assertTrue( os.path.isfile(created_file_path),
Run Code Online (Sandbox Code Playgroud)

当然,这是第一次转换失败后立即中止测试.我可以为每个转换编写一个测试函数.这将导致必须为每个添加的格式编写一个新的测试,现在我只需要在我的AUDIO_FORMATS元组中添加一个新的字典.

Ate*_*ral 6

而不是断言,将错误存储在数组中.在迭代结束时,断言errors数组为空,并可能将数组的内容转储为断言失败原因.