如何从基类修饰子类方法?

2024-04-29 14:58:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个基本的测试用例类,它的子类应该用flaky decorator(https://github.com/box/flaky)来装饰,换句话说,我想把flaky decorator应用到每个测试用例(测试方法)上,但是从一个基类开始,使用单个位置来装饰每个测试类或测试用例(有很多测试用例…)。你知道吗

但如果我将decorator应用于基类CustomTestCase,它将影响每个方法(包括helper方法、构造函数等)。是否可以将它仅应用于基类中的测试用例(方法以“test”开头)。我试着通过访问self来实现它。\u testMethod inCustomTestCase.setUp设置没有成功。你知道吗

# base class
@flaky(max_runs=3, min_passes=1)
class CustomTestCase(SimpleTestCase):
    """
    base class for tests using Selenium.
    """
    @classmethod
    def setUpClass(cls):
        pass

    def setUp(self):
        pass


# child class - test suite
class TestSomething(CustomTestCase):
    """
    This class contains all different tests
    """
    def test_something(self):
        """ should be decorated by flaky """
        self.assertEqual(1, randint(0, 1))

    def test_another(self):
        self.assertEqual(1, randint(0, 1))

Tags: 方法testselfbasedefsetup测试用例tests