如何将 mock.patch 移动到设置中?

How to move the mock.patch to the setUp?

我有以下两个单元测试:

    @mock.patch('news.resources.generator.Generator.get_header')
    @mock.patch('news.scraper.bbc_spider.BBCSpider.save_scraped_rss_into_news_model')
    @mock.patch('news.scraper.bbc_spider.BBCSpider.get_news_urls')
    @mock.patch('requests.get')
    def test_get_header_is_called(self, req_get, spi_news, spi_save_rss, get_head):
        spi_news.return_value = {'x': 1}
        gen = Generator()
        gen.get()
        get_head.assert_called_with()

    @mock.patch('news.resources.generator.Generator.get_header')
    @mock.patch('news.scraper.bbc_spider.BBCSpider.save_scraped_rss_into_news_model')
    @mock.patch('news.scraper.bbc_spider.BBCSpider.get_news_urls')
    @mock.patch('requests.get')
    def test_task_url_is_save_scraped_rss_into_news_model(self, req_get, spi_news, spi_save_rss, get_head):
        spi_news.return_value = {'x': 1}
        gen = Generator()
        gen.get()
        tasks = self.taskqueue_stub.GetTasks("newstasks")
        self.assertEqual(tasks[0]['url'], '/v1/worker/save-scraped-rss-into-news-model')

如您所见,有很多代码重复。有什么方法可以将 mock.patch 移动到 setUp()?

Class TestGenerator(TestBase):
    def setUp(self):
        super(TestGenerator, self).setUp()
        mock.patch() ???

    def test_get_header_is_called(self, req_get, spi_news, spi_save_rss, get_head):
            spi_news.return_value = {'x': 1}
            gen = Generator()
            gen.get()
            get_head.assert_called_with()

    def test_task_url_is_save_scraped_rss_into_news_model(self, req_get, spi_news, spi_save_rss, get_head):
            spi_news.return_value = {'x': 1}
            gen = Generator()
            gen.get()
            tasks = self.taskqueue_stub.GetTasks("newstasks")
            self.assertEqual(tasks[0]['url'], '/v1/worker/save-scraped-rss-into-news-model')

这里有 2 个选项 -- 首先,您可以将补丁移动到 class:

@mock.patch('news.resources.generator.Generator.get_header')
@mock.patch('news.scraper.bbc_spider.BBCSpider.save_scraped_rss_into_news_model')
@mock.patch('news.scraper.bbc_spider.BBCSpider.get_news_urls')
@mock.patch('requests.get')
class TestGenerator(TestBase):
    def test_get_header_is_called(self, req_get, spi_news, spi_save_rss, get_head):
      pass

当您在 class 上使用 mock.patch 系列中的某些内容时,它的行为就像您修补了以 "test"1[= 开头的每个方法一样34=]个人。

您的另一个选择是在安装程序中启动补丁程序。在一个虚构的例子中(为了节省打字),它看起来像这样:

class SomeTest(TestCase):
    def setUp(self):
        super(SomeTest, self).setUp()
        patch = mock.patch('foo.bar.baz')
        mock_baz = patch.start()  # may want to keep a reference to this if you need to do per-test configuration
        self.addCleanup(patch.stop)

addCleanup 已添加到 python2.7(太棒了!)。如果您不需要旧版本的 python2.x,您应该使用它,因为它比其他版本更健壮。最简单的替代方法是停止拆解中的所有补丁:

class SomeTest(TestCase):
    def setUp(self):
        super(SomeTest, self).setUp()
        patch = mock.patch('foo.bar.baz')
        mock_baz = patch.start()  # may want to keep a reference to this if you need to do per-test configuration

    def tearDown(self):
        super(SomeTest, self).tearDown()
        mock.patch.stopall()

但您也可以保留对单个补丁的引用 self.patch1 = mock.patch(...),然后根据需要在 tearDown 中单独停止该补丁。

1实际上,mock.TEST_PREFIX 默认为 "test"