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

时间:2012-04-23 09:20:30

标签: python django unit-testing

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

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 } )

由于其中一个转换失败的一个可能原因是缺少库,或者这样的库或我的实现中的一些错误或失败,我想测试每个转换以获得已通过和失败的测试列表最后,失败的人告诉我确切的转换确实导致了麻烦。这是我试过的(有点简化):

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),

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

2 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:0)

为什么不使用试试......除了......

  errors = []

  for option in optionlist:
    try:
      assert_and_raise1(option)
      assert_and_raise2(...)
    except Exception, e:
      errors.append("[%s] fail: %s"%(option, e))

   for e in errors:
     print e
相关问题