Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在方法发生变化时重用单元测试代码_Python_Unit Testing - Fatal编程技术网

Python 在方法发生变化时重用单元测试代码

Python 在方法发生变化时重用单元测试代码,python,unit-testing,Python,Unit Testing,我知道标题很差,但我想不出一个更雄辩的方式来说明这个问题。我有很多单元测试: def test_send_new_registration_email(self): emails = NewEmail(email_client=MagicMock()) emails.send_email = MagicMock() emails.send_marketing_email(recipients(), new_registration_payload()) emai

我知道标题很差,但我想不出一个更雄辩的方式来说明这个问题。我有很多单元测试:

 def test_send_new_registration_email(self):
    emails = NewEmail(email_client=MagicMock())
    emails.send_email = MagicMock()
    emails.send_marketing_email(recipients(), new_registration_payload())
    emails.send_email.assert_called_with(new_registration_output())

其中有20个单元测试。它们都遵循非常相似的模式。基本上,我比较输入和期望的输出。必须有一种方法来获得输入列表和输出列表并进行比较

我可以做一个for循环

def test_send_new_registration_email(self):
    for index, input in enum(inputs):
        emails = NewEmail(email_client=MagicMock())
        emails.send_email = MagicMock()
        emails.send_marketing_email(input)
        emails.send_email.assert_called_with(output[index])

但是,有没有更干净的方法可以做到这一点?

您正在寻找参数化测试。然而,实际的实现取决于单元测试使用的库。香草
unittest
不提供任何参数化支持,因此您需要安装第三方软件包。(
pip安装参数化
)示例:

现在将使用提供的两个测试输入执行两次测试

如果您打算用编写和运行测试(这是我自己使用的),它已经提供了开箱即用的测试参数化:

import pytest

data = [
    ((recipients(), new_registration_payload(), ), new_registration_output(), ),
    ((recipients(), new_registration_payload(), ), new_comment_output(), ),
]

@pytest.mark.parametrize("input, output", data)
def test_send_new_comment_email(input, output):
    emails = NewEmail(email_client=MagicMock())
    emails.send_email = MagicMock()
    emails.send_marketing_email(*input)
    emails.send_email.assert_called_with(output)
测试将运行两次:

$ pytest test_foo.py --collect-only

======== test session starts ========
platform darwin -- Python 3.6.3, pytest-3.2.5, py-1.5.2, pluggy-0.4.0
rootdir: /private/tmp, inifile:
plugins: mock-1.6.3, cov-2.5.1
collected 2 items                                                                                                                                                                          

<Module 'test_foo.py'>
    <Function 'test_send_new_comment_email[input0-registration_output]'>
    <Function 'test_send_new_comment_email[input1-comment_output]'>

======== no tests ran in 0.01 seconds ========
$pytest\u foo.py--仅收集
=======测试会话开始========
平台darwin——Python 3.6.3、pytest-3.2.5、py-1.5.2、Plugy-0.4.0
rootdir:/private/tmp,文件:
插件:mock-1.6.3,cov-2.5.1
收集2项
==========0.01秒内未运行任何测试========
import pytest

data = [
    ((recipients(), new_registration_payload(), ), new_registration_output(), ),
    ((recipients(), new_registration_payload(), ), new_comment_output(), ),
]

@pytest.mark.parametrize("input, output", data)
def test_send_new_comment_email(input, output):
    emails = NewEmail(email_client=MagicMock())
    emails.send_email = MagicMock()
    emails.send_marketing_email(*input)
    emails.send_email.assert_called_with(output)
$ pytest test_foo.py --collect-only

======== test session starts ========
platform darwin -- Python 3.6.3, pytest-3.2.5, py-1.5.2, pluggy-0.4.0
rootdir: /private/tmp, inifile:
plugins: mock-1.6.3, cov-2.5.1
collected 2 items                                                                                                                                                                          

<Module 'test_foo.py'>
    <Function 'test_send_new_comment_email[input0-registration_output]'>
    <Function 'test_send_new_comment_email[input1-comment_output]'>

======== no tests ran in 0.01 seconds ========