@@ -1493,6 +1493,10 @@ msgid ""
1493
1493
":mod:`unittest` finds tests. You can specify an alternative prefix by "
1494
1494
"setting ``patch.TEST_PREFIX``."
1495
1495
msgstr ""
1496
+ "Patch 可以被用作 :class:`~unittest.TestCase` 类装饰器。 它是通过装饰类中的每个测试方法来发挥作用的。 "
1497
+ "当你的测试方法共享同一个补丁集时这将减少模板代码。 :func:`patch` 会通过查找以 ``patch.TEST_PREFIX`` "
1498
+ "打头的名称来找到测试。 其默认值为 ``'test'``,这与 :mod:`unittest` 找到测试的方式一致。 你可以通过设置 "
1499
+ "``patch.TEST_PREFIX`` 来指定其他的前缀。"
1496
1500
1497
1501
#: ../../library/unittest.mock.rst:1465
1498
1502
msgid ""
@@ -1565,7 +1569,7 @@ msgstr ""
1565
1569
msgid ""
1566
1570
"To configure return values on methods of *instances* on the patched class "
1567
1571
"you must do this on the :attr:`~Mock.return_value`. For example::"
1568
- msgstr ""
1572
+ msgstr "要在被打补丁的类的 *实例* 的方法上配置返回值你必须在 :attr:`~Mock.return_value` 进行操作。 例如:: "
1569
1573
1570
1574
#: ../../library/unittest.mock.rst:1498
1571
1575
msgid ""
@@ -2082,6 +2086,8 @@ msgid ""
2082
2086
"it simpler to do patching in ``setUp`` methods or where you want to do "
2083
2087
"multiple patches without nesting decorators or with statements."
2084
2088
msgstr ""
2089
+ "所有 patcher 对象都具有 :meth:`!start` 和 :meth:`!stop` 方法。 使用这些方法可以更简单地在 ``setUp`` "
2090
+ "方法上打补丁,还可以让你不必嵌套使用装饰器或 with 语句就能打多重补丁。"
2085
2091
2086
2092
#: ../../library/unittest.mock.rst:1821
2087
2093
msgid ""
@@ -2090,6 +2096,9 @@ msgid ""
2090
2096
"then call :meth:`!start` to put the patch in place and :meth:`!stop` to undo"
2091
2097
" it."
2092
2098
msgstr ""
2099
+ "要使用这些方法请按正常方式调用 :func:`patch`, :func:`patch.object` 或 :func:`patch.dict` "
2100
+ "并保留一个指向所返回 ``patcher`` 对象的引用。 你可以随后调用 :meth:`!start` 来原地打补丁并调用 :meth:`!stop`"
2101
+ " 来恢复它。"
2093
2102
2094
2103
#: ../../library/unittest.mock.rst:1825
2095
2104
msgid ""
@@ -2123,7 +2132,7 @@ msgstr ""
2123
2132
msgid ""
2124
2133
"A typical use case for this might be for doing multiple patches in the "
2125
2134
"``setUp`` method of a :class:`~unittest.TestCase`::"
2126
- msgstr ""
2135
+ msgstr "此操作的一个典型应用场景是在一个 :class:`~unittest.TestCase` 的 ``setUp`` 方法中执行多重补丁:: "
2127
2136
2128
2137
#: ../../library/unittest.mock.rst:1842
2129
2138
msgid ""
@@ -3166,6 +3175,8 @@ msgid ""
3166
3175
"``dir(type(my_mock))`` (type members) to bypass the filtering irrespective "
3167
3176
"of :const:`FILTER_DIR`."
3168
3177
msgstr ""
3178
+ "或者你也可以直接使用 ``vars(my_mock)`` (实例成员) 和 ``dir(type(my_mock))`` (类型成员) 来绕过不考虑 "
3179
+ ":const:`FILTER_DIR` 的过滤。"
3169
3180
3170
3181
#: ../../library/unittest.mock.rst:2517
3171
3182
msgid "mock_open"
@@ -3199,6 +3210,11 @@ msgid ""
3199
3210
"that is insufficient, one of the in-memory filesystem packages on `PyPI "
3200
3211
"<https://pypi.org>`_ can offer a realistic filesystem for testing."
3201
3212
msgstr ""
3213
+ "*read_data* 是供文件句柄的 :meth:`~io.RawIOBase.read`, :meth:`~io.IOBase.readline` "
3214
+ "和 :meth:`~io.IOBase.readlines` 方法返回的字符串。 调用这些方法将会从 *read_data* 获取数据直到它被耗尽。 "
3215
+ "对这些方法的模拟是相当简化的:每次 *mock* 被调用时,*read_data* 都将从头开始。 "
3216
+ "如果你需要对你提供给测试代码的数据有更多控制那么你将需要自行定制这个 mock。 如果这还不够用,那么通过一些 `PyPI "
3217
+ "<https://pypi.org>`_ 上的内存文件系统包可以提供更真实的测试用文件系统。"
3202
3218
3203
3219
#: ../../library/unittest.mock.rst:2538
3204
3220
msgid ""
@@ -3423,6 +3439,10 @@ msgid ""
3423
3439
" ...\n"
3424
3440
"TypeError: <lambda>() takes at least 2 arguments (1 given)"
3425
3441
msgstr ""
3442
+ ">>> req = request.Request()\n"
3443
+ "Traceback (most recent call last):\n"
3444
+ " ...\n"
3445
+ "TypeError: <lambda>() takes at least 2 arguments (1 given)"
3426
3446
3427
3447
#: ../../library/unittest.mock.rst:2665
3428
3448
msgid ""
@@ -3436,6 +3456,9 @@ msgid ""
3436
3456
">>> req\n"
3437
3457
"<NonCallableMagicMock name='request.Request()' spec='Request' id='...'>"
3438
3458
msgstr ""
3459
+ ">>> req = request.Request('foo')\n"
3460
+ ">>> req\n"
3461
+ "<NonCallableMagicMock name='request.Request()' spec='Request' id='...'>"
3439
3462
3440
3463
#: ../../library/unittest.mock.rst:2672
3441
3464
msgid ""
@@ -3455,6 +3478,13 @@ msgid ""
3455
3478
"AttributeError: Mock object has no attribute 'assret_called_with'\n"
3456
3479
">>> req.add_header.assert_called_with('spam', 'eggs')"
3457
3480
msgstr ""
3481
+ ">>> req.add_header('spam', 'eggs')\n"
3482
+ "<MagicMock name='request.Request().add_header()' id='...'>\n"
3483
+ ">>> req.add_header.assret_called_with # 故意的拼写错误!\n"
3484
+ "Traceback (most recent call last):\n"
3485
+ " ...\n"
3486
+ "AttributeError: Mock object has no attribute 'assret_called_with'\n"
3487
+ ">>> req.add_header.assert_called_with('spam', 'eggs')"
3458
3488
3459
3489
#: ../../library/unittest.mock.rst:2684
3460
3490
msgid ""
@@ -3512,6 +3542,17 @@ msgid ""
3512
3542
" ...\n"
3513
3543
"AttributeError: Mock object has no attribute 'a'"
3514
3544
msgstr ""
3545
+ ">>> class Something:\n"
3546
+ "... def __init__(self):\n"
3547
+ "... self.a = 33\n"
3548
+ "...\n"
3549
+ ">>> with patch('__main__.Something', autospec=True):\n"
3550
+ "... thing = Something()\n"
3551
+ "... thing.a\n"
3552
+ "...\n"
3553
+ "Traceback (most recent call last):\n"
3554
+ " ...\n"
3555
+ "AttributeError: Mock object has no attribute 'a'"
3515
3556
3516
3557
#: ../../library/unittest.mock.rst:2722
3517
3558
msgid ""
0 commit comments