简体   繁体   English

在类方法中传递的Python unittest测试用

[英]Python unittest test passed in class method is called with

I have a method that takes two inputs a emailer class and a dict of data. 我有一个方法需要两个输入,一个电子邮件类和一个数据字典。

def send_email(data, email_client):
     **** various data checks and formatting *****
     response_code = email_client.create_email(recipient=receipient
                                               sender=sender
                                               etc...)

I am trying to write a unit test that will assert email_client.create_email was called with the correct values based on the input data. 我正在尝试编写一个单元测试,将断言基于输入数据使用正确的值调用了email_client.create_email。

In my test file I have 在我的测试文件中

from emailer.email import send_email

class TestEmails(unittest.TestCase):

    def test_send_email(self):
        email.send_email(get_transactional_email_data, MagicMock())

I normally test what a method is called with by something similar to: 我通常会测试类似于以下内容的方法所调用的内容:

mock.assert_called_with(recipient=receipient
                       sender=sender
                       etc..)

However, since this time I'm testing what a passed in class is being called with (and a MagicMock) I don't know how it should be done. 但是,由于这一次我正在测试用(和MagicMock)调用传入的类,所以我不知道该怎么做。

I don't think you need MagicMock. 我认为您不需要MagicMock。 Just create the mocks upfront 只是预先创建模拟

from emailer.email import send_email

class TestEmails(unittest.TestCase):

def test_send_email(self):
    myclient = Mock()
    mycreate = Mock()
    myclient.create_email = mycreate
    email.send_email(get_transactional_email_data, myclient)
    self.assertTrue(
        mycreate.called_with(sender='...')
    )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python unittest报告通过测试 - Python unittest report passed test 如何创建一个单元测试来测试是否使用特定参数为特定 class 调用了方法? - How create a unittest that will test if method was called for specific class with specific argument? python unittest没有这样的测试方法 - No such test method for python unittest Python unittest.mock:调用了已修补的类方法,但断言失败 - Python unittest.mock: patched class method called but assertion fails Python unittest - ValueError:没有这样的测试方法 <class 'mytestcase.MyTestCase'> :runTest - Python unittest - ValueError: no such test method in <class 'mytestcase.MyTestCase'>: runTest 如何使用Unittest在Python中编写内部类测试方法 - How to write inner class test method in Python with Unittest python3如何设置在unittest中通过的测试 - python3 how to set test passed in unittest 使用Python单元测试库(unittest,mock),如何断言是否在类A的方法内调用了类B的方法? - Using Python unittesting libraries (unittest, mock), how to assert if a method of class B was called within a method of class A? 未调用 Unittest 模拟类的方法 - Unittest mocked class's method not being called Selenium python如何使用来自unittest.TestCase的assertEqual方法“没有这样的测试方法 <class 'unittest.case.TestCase'> ” - Selenium python how to use assertEqual method from unittest.TestCase “No such test method in <class 'unittest.case.TestCase'>”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM