How to write a custom `.assertFoo()` method in Python?

前端 未结 3 1738
天涯浪人
天涯浪人 2020-12-07 22:52

I\'m writing some test cases for my application using Python\'s unittest. Now I need to compare a list of objects with a list of another objects to check if the objects from

3条回答
  •  独厮守ぢ
    2020-12-07 23:47

    You should create your own TestCase class, derived from unittest.TestCase. Then put your custom assert method into that test case class. If your test fails, raise an AssertionError. Your message should be a string. If you want to test all objects in the list rather than stop on a failure, then collect a list of failing indexes, and after the loop over all the objects, build an assert message that summarizes your findings.

提交回复
热议问题