且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Python 中编写自定义的 `.assertFoo()` 方法?

更新时间:2022-12-11 17:25:59

我在这些情况下使用多重继承.例如:

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 + '".')

现在我定义了一个继承自 unittest.TestCase 和 CustomAssertion 的类

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