如何将mock.patch移至setUp? [英] How to move the mock.patch to the setUp?

查看:64
本文介绍了如何将mock.patch移至setUp?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下两个单元测试:

I have the following two unit tests:

    @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')

如您所见,代码重复很多.有什么办法可以将模拟文件.patch移到setUp()吗?

As you can see there is a lot of code repetition. Is there a way I could move the mock.patch to the 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个选项-首先,您可以将修补程序移至该类:

You have 2 options here -- First, you could move the patching to the 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

在类的mock.patch系列中使用某些内容时,其行为就像您分别修补了以"test" 1 开头的每个方法一样.

when you use something in the mock.patch family on the class, it behaves as if you had patched each method that starts with "test"1 individually.

您的另一个选择是在安装程序中启动修补程序.在一个虚构的示例中(保存输入内容),它看起来像这样:

Your other option is to start the patches in setup. In a fictitious example (to save typing), it looks like this:

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版本,则应使用它,因为它比其他版本更健壮.最简单的选择是仅停止tearDown中的所有修补程序:

addCleanup was added in python2.7 (and is fantastic!). If you don't need older versions of python2.x, you should use it as it is more robust than the alternatives. The easiest alternative is to just stop all patches in tearDown:

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中单独停止该补丁程序.

but you can also keep references to individual patches self.patch1 = mock.patch(...) and then stop that one individually in tearDown as necessary.

1 实际上,默认为"test"

1Actually, mock.TEST_PREFIX which defaults to "test"

这篇关于如何将mock.patch移至setUp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆