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

前端 未结 3 1751
天涯浪人
天涯浪人 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:30

    I use the multiple inheritance in these cases. For example:

    First. I define a class with methods that will incorporate.

    import os
    
    class CustomAssertions:
        def assertFileExists(self, path):
            if not os.path.lexists(path):
                raise AssertionError('File not exists in path "' + path + '".')
    

    Now I define a class that inherits from unittest.TestCase and CustomAssertion

    import unittest
    
    class MyTest(unittest.TestCase, CustomAssertions):
        def test_file_exists(self):
            self.assertFileExists('any/file/path')
    
    if __name__ == '__main__':
        unittest.main()
    

提交回复
热议问题