如何在Djang中模拟一个类进行多个测试

2024-10-03 19:29:32 发布

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

我有一个通过HTTP调用远程服务的类。现在,这个类检测它是否在“TESTING”模式下运行并相应地操作:虽然“TESTING”不向远程服务发送实际的请求,但它只返回而不执行任何操作。在

class PushService(object):

    def trigger_event(self, channel_name, event_name, data):
        if satnet_cfg.TESTING:
            logger.warning('[push] Service is in testing mode')
            return
        self._service.trigger(channel_name, event_name, data)

几个测试调用部分代码,最后通过调用此方法结束。我的问题如下:

^{pr2}$

Tags: nameselfeventhttpdataif远程object
1条回答
网友
1楼 · 发布于 2024-10-03 19:29:32

如果需要对所有测试执行修补程序,可以使用^{{cd1>}方法执行此操作:

class RemoteServiceTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.patchers = []
        patcher = patch('application.PushService.trigger_event')
        cls.patchers.append(patcher)
        trigger_mock = patcher.start()
        trigger_mock.return_value = 'Some return value'

    @classmethod
    def tearDownClass(cls):
        for patcher in cls.patchers:
            patcher.stop()

    def test1(self):
        # Test actions

    def test2(self):
        # Test actions

    def test3(self):
        # Test actions

^{cd1>}每类调用一次(本例中为测试套件)。在这个方法中,您可以设置所有测试都需要使用的修补程序。

相关问题 更多 >