Commit Graph

474 Commits

Author SHA1 Message Date
Miss Islington (bot) 3e499cda47
bpo-29620: iterate over a copy of sys.modules (GH-4800) (GH-20816)
unittest.TestCase.assertWarns no longer raises a RuntimeException
when accessing a module's ``__warningregistry__`` causes importation of a new
module, or when a new module is imported in another thread.
(cherry picked from commit 46398fba4d)

Co-authored-by: kernc <kerncece@gmail.com>
2020-06-11 14:31:46 -04:00
Serhiy Storchaka ee249d798b
[3.8] bpo-40126: Fix reverting multiple patches in unittest.mock. (GH-19351) (GH-19483)
Patcher's __exit__() is now never called if its __enter__() is failed.
Returning true from __exit__() silences now the exception.
(cherry picked from commit 4b222c9491)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2020-04-12 14:53:46 +03:00
Miss Islington (bot) f6bdac1bf7
bpo-39915: Ensure await_args_list is updated according to the order in which coroutines were awaited (GH-18927)
Create call objects with awaited arguments instead of using call_args which has only last call value.
(cherry picked from commit e553f204bf)

Co-authored-by: Karthikeyan Singaravelan <tir.karthi@gmail.com>
2020-03-14 07:12:57 +00:00
Miss Islington (bot) 02395fad8e
bpo-39450 Stripped whitespace before parsing the docstring in TestCase.shortDescription (GH-18175) (#18323)
(cherry picked from commit 032de7324e)

Co-authored-by: Steve Cirelli <scirelli+git@gmail.com>
2020-02-03 08:20:41 +00:00
Miss Islington (bot) 696d2324cf
bpo-39485: fix corner-case in method-detection of mock (GH-18255)
Replace check for whether something is a method in the mock module. The
previous version fails on PyPy, because there no method wrappers exist
(everything looks like a regular Python-defined function). Thus the
isinstance(getattr(result, '__get__', None), MethodWrapperTypes) check
returns True for any descriptor, not just methods.

This condition could also return erroneously True in CPython for
C-defined descriptors.

Instead to decide whether something is a method, just check directly
whether it's a function defined on the class. This passes all tests on
CPython and fixes the bug on PyPy.
(cherry picked from commit a327677905)

Co-authored-by: Carl Friedrich Bolz-Tereick <cfbolz@gmx.de>

Co-authored-by: Carl Friedrich Bolz-Tereick <cfbolz@gmx.de>
2020-01-29 16:15:36 +00:00
Matthew Kokotovich 19be85c765 [3.8] bpo-39082: Allow AsyncMock to correctly patch static/class methods (GH-18190)
(cherry picked from commit 62865f4532)

Co-authored-by: Matthew Kokotovich <mkokotovich@gmail.com>
2020-01-26 15:30:27 +00:00
Miss Islington (bot) a5906b2bfc bpo-38473: Handle autospecced functions and methods used with attach_mock (GH-16784) (GH-18167)
If an autospecced object is attached using attach_mock the
child would be a function with mock object as attribute from
which signature has to be derived.

(cherry picked from commit 66b00a9d3a)

Co-authored-by: Karthikeyan Singaravelan <tir.karthi@gmail.com>
2020-01-25 20:23:08 +05:30
Miss Islington (bot) 0ea7309927
Improve test coverage for AsyncMock. (GH-17906)
* Add test for nested async decorator patch.
* Add test for side_effect and wraps with a function.
* Add test for side_effect with an exception in the iterable.
(cherry picked from commit 54f743eb31)

Co-authored-by: Karthikeyan Singaravelan <tir.karthi@gmail.com>
2020-01-24 05:27:04 -08:00
Miss Islington (bot) 4594565b56 bpo-38669: patch.object now raises a helpful error (GH17510)
This means a clearer message is now shown when patch.object is called with two string arguments, rather than a class and a string argument.
(cherry picked from commit cd90a52983)

Co-authored-by: Elena Oat <oat.elena@gmail.com>
2019-12-09 06:59:04 +00:00
Lisa Roach b2744c1be7 [3.8] bpo-38857: AsyncMock fix for awaitable values and StopIteration fix [3.8] (GH-17269) (#17304)
(cherry picked from commit 046442d02b)

Co-authored-by: Jason Fried <fried@fb.com>
2019-11-21 20:14:32 +02:00
Lisa Roach 21f24ead90
[3.8] bpo-38163: Child mocks detect their type as sync or async (GH-16471) (GH-16484) 2019-09-29 22:22:44 -07:00
Miss Islington (bot) 36e7e4aabb bpo-38161: Removes _AwaitEvent from AsyncMock. (GH-16443) (GH-16481) 2019-09-29 21:23:33 -07:00
Miss Islington (bot) b76ab35240 bpo-38108: Makes mock objects inherit from Base (GH-16060) (GH-16470) 2019-09-29 21:02:46 -07:00
Lisa Roach 52bdd414ed
[3.8] bpo-38136: Updates await_count and call_count to be different things (GH-16192) (GH-16431) 2019-09-27 15:44:34 -07:00
Miss Islington (bot) 1a17a054f6
[3.8] bpo-36871: Handle spec errors in assert_has_calls (GH-16005) (GH-16364)
The fix in PR 13261 handled the underlying issue about the spec for specific methods not being applied correctly, but it didn't fix the issue that was causing the misleading error message.

The code currently grabs a list of responses from _call_matcher (which may include exceptions). But it doesn't reach inside the list when checking if the result is an exception. This results in a misleading error message when one of the provided calls does not match the spec.


https://bugs.python.org/issue36871



Automerge-Triggered-By: @gpshead
(cherry picked from commit b5a7a4f0c2)


Co-authored-by: Samuel Freilich <sfreilich@google.com>


https://bugs.python.org/issue36871



Automerge-Triggered-By: @gpshead
2019-09-24 17:29:17 -07:00
Lisa Roach 865bb685a6 [3.8] bpo-38093: Correctly returns AsyncMock for async subclasses. (GH-15947) (GH-16299)
(cherry picked from commit 8b03f943c3)

Co-authored-by: Lisa Roach <lisaroach14@gmail.com>
2019-09-21 08:00:04 +02:00
Miss Islington (bot) f668d2b775
bpo-37828: Fix default mock_name in unittest.mock.assert_called error (GH-16166)
In the format string for assert_called the evaluation order is incorrect and hence for mock's without name, 'None' is printed whereas it should be 'mock' like for other messages. The error message is ("Expected '%s' to have been called." % self._mock_name or 'mock').
(cherry picked from commit 5f5f11faf9)

Co-authored-by: Abraham Toriz Cruz <awonderfulcode@gmail.com>
2019-09-17 04:35:56 -07:00
Miss Islington (bot) cc8edfbd9c
bpo-38100: Fix spelling error in unittest.mock code (GH-16168)
(cherry picked from commit a9187c3118)

Co-authored-by: marcoramirezmx <55331462+marcoramirezmx@users.noreply.github.com>
2019-09-16 09:52:45 -07:00
Miss Islington (bot) db0d8a5b2c bpo-37972: unittest.mock._Call now passes on __getitem__ to the __getattr__ chaining so that call() can be subscriptable (GH-15565) (GH-15965)
* bpo-37972: unittest.mock._Call now passes on __getitem__ to the __getattr__ chaining so that call() can be subscriptable

* 📜🤖 Added by blurb_it.

* Update 2019-08-28-21-40-12.bpo-37972.kP-n4L.rst

added name of the contributor

* bpo-37972: made all dunder methods chainable for _Call

* bpo-37972: delegate only attributes of tuple instead to __getattr__
(cherry picked from commit 72c359912d)

Co-authored-by: blhsing <github@ydooby.com>
2019-09-12 12:52:49 +02:00
Andrew Svetlov 4601f7a49f
[3.8] bpo-36373: Fix deprecation warnings (GH-15889) (GH-15901)
https://bugs.python.org/issue36373
(cherry picked from commit 7264e92b71)

Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
2019-09-11 13:40:36 +03:00
Miss Islington (bot) c3008dd480
bpo-37251: Removes __code__ check from _is_async_obj. (GH-15830)
(cherry picked from commit f1a297acb6)

Co-authored-by: Lisa Roach <lisaroach14@gmail.com>
2019-09-10 06:16:00 -07:00
Miss Islington (bot) eaa1b09412
docs: Add references to AsyncMock in unittest.mock.patch (GH-13681)
Update the docs as patch can now return an AsyncMock if the patched
object is an async function.
(cherry picked from commit f5e7f39d29)

Co-authored-by: Mario Corchero <mcorcherojim@bloomberg.net>
2019-09-10 06:15:19 -07:00
Miss Islington (bot) 3bd4bed78a bpo-34596: Fallback to a default reason when @unittest.skip is uncalled (GH-9082) (#15781)
* bpo-34596: Fallback to a default reason when @unittest.skip is uncalled

* Change default reason to empty string

* Fix rst formatting of NEWS entry
(cherry picked from commit d5fd75c53f)

Co-authored-by: Naitree Zhu <Naitreey@gmail.com>
2019-09-09 17:01:14 +02:00
Miss Islington (bot) bee8bfe5f4
bpo-37212: Preserve keyword argument order in unittest.mock.call and error messages (GH-14310)
(cherry picked from commit 9d607061c9)

Co-authored-by: Xtreak <tir.karthi@gmail.com>
2019-09-09 04:42:43 -07:00
Miss Islington (bot) 4bd1d05ee2
Fix typos mostly in comments, docs and test names (GH-15209)
(cherry picked from commit 39d87b5471)

Co-authored-by: Min ho Kim <minho42@gmail.com>
2019-08-30 13:42:54 -07:00
Miss Islington (bot) c71ae1a45b bpo-36743: __get__ is sometimes called without the owner argument (GH-12992) (GH-15589)
(cherry picked from commit 0dac68f1e5)

Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
2019-08-29 02:02:51 -07:00
Miss Islington (bot) 38d311d79e bpo-36871: Ensure method signature is used when asserting mock calls to a method (GH15578)
* Fix call_matcher for mock when using methods

* Add NEWS entry

* Use None check and convert doctest to unittest

* Use better name for mock in tests. Handle _SpecState when the attribute was not accessed and add tests.

* Use reset_mock instead of reinitialization. Change inner class constructor signature for check

* Reword comment regarding call object lookup logic
(cherry picked from commit c96127821e)

Co-authored-by: Xtreak <tir.karthi@gmail.com>
2019-08-29 07:58:27 +01:00
Miss Islington (bot) 612d3935ec
bpo-35946: Improve assert_called_with documentation (GH-11796)
(cherry picked from commit f5896a05ed)

Co-authored-by: Rémi Lapeyre <remi.lapeyre@henki.fr>
2019-08-28 23:39:47 -07:00
Miss Islington (bot) c30c869e8d
bpo-9949: Enable symlink traversal for ntpath.realpath (GH-15287)
(cherry picked from commit 75e064962e)

Co-authored-by: Steve Dower <steve.dower@python.org>
2019-08-21 14:09:33 -07:00
Miss Islington (bot) 22fd679dc3 bpo-21478: Record calls to parent when autospecced objects are used as child with attach_mock (GH 14688) (GH-14902)
* Clear name and parent of mock in autospecced objects used with attach_mock

* Add NEWS entry

* Fix reversed order of comparison

* Test child and standalone function calls

* Use a helper function extracting mock to avoid code duplication and refactor tests.
(cherry picked from commit 7397cda997)

Co-authored-by: Xtreak <tir.karthi@gmail.com>
2019-07-22 08:59:00 +01:00
Serhiy Storchaka 2085bd0877
bpo-37116: Use PEP 570 syntax for positional-only parameters. (GH-13700) 2019-06-01 11:00:15 +03:00
Andrew Svetlov 4dd3e3f9bb bpo-32972: Async test case (GH-13386)
Add explicit `asyncSetUp` and `asyncTearDown` methods.
The rest is the same as for #13228

`AsyncTestCase` create a loop instance for every test for the sake of test isolation.
Sometimes a loop shared between all tests can speed up tests execution time a lot but it requires control of closed resources after every test finish. Basically, it requires nested supervisors support that was discussed with @1st1 many times. Sorry, asyncio supervisors have no chance to land on Python 3.8.

The PR intentionally does not provide API for changing the used event loop or getting the test loop: use `asyncio.set_event_loop_policy()` and `asyncio.get_event_loop()` instead.

The PR adds four overridable methods to base `unittest.TestCase` class:
```
    def _callSetUp(self):
        self.setUp()

    def _callTestMethod(self, method):
        method()

    def _callTearDown(self):
        self.tearDown()

    def _callCleanup(self, function, /, *args, **kwargs):
        function(*args, **kwargs)
```
It allows using asyncio facilities with minimal influence on the unittest code.

The last but not least: the PR respects contextvars. The context variable installed by `asyncSetUp` is available on test, `tearDown` and a coroutine scheduled by `addCleanup`.


https://bugs.python.org/issue32972
2019-05-29 02:33:59 -07:00
Xtreak 0ae022c6a4 bpo-37075: Fix string concatenation in assert_has_awaits error message (GH-13616)
* Fix the implicit string concatenation in `assert_has_awaits` error message.
* Use "await" instead of "call" in `assert_awaited_with` error message.



https://bugs.python.org/issue37075
2019-05-29 00:02:25 -07:00
Mario Corchero 04530812e9 bpo-32299: Return patched dict when using patch.dict as a context manager (GH-11062) 2019-05-28 08:53:30 -04:00
Xtreak 436c2b0d67 bpo-36996: Handle async functions when mock.patch is used as a decorator (GH-13562)
Return a coroutine while patching async functions with a decorator. 

Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>


https://bugs.python.org/issue36996
2019-05-28 00:07:38 -07:00
Xtreak ff6b2e66b1 bpo-37047: Refactor AsyncMock setup logic for autospeccing (GH-13574)
Handle late binding and attribute access in unittest.mock.AsyncMock
setup for autospeccing.
2019-05-27 14:56:23 +02:00
Damien Nadé 394119afc6 bpo-37008: make mock_open handle able to honor next() (GH-13492)
I've reported the issue on https://bugs.python.org/issue37008 and now I'm trying to bring a solution to this minor issue.

I think it could be trivially backported to 3.7 branch.


https://bugs.python.org/issue37008
2019-05-23 03:03:25 -07:00
Xtreak e7cb23bf20 Fix RuntimeWarning in unittest.mock asyncio example (GH-13449)
* This PR fixes the `RuntimeWarning` in `inspect.isawaitable(mock())` where `mock()` was not awaited.
* Fix typo in asynctest project.
2019-05-21 01:47:17 -07:00
Lisa Roach 77b3b7701a
bpo-26467: Adds AsyncMock for asyncio Mock library support (GH-9296) 2019-05-20 09:19:53 -07:00
Ashwin Ramaswami f665b96e92 Fix typo in test comment (GH-11442) 2019-05-18 21:17:47 -04:00
Zackery Spytz b9b08cd948 bpo-24758: Improve the error msg for unittest.mock.Mock()'s unsafe mode (#12991)
* bpo-24758: Improve the error msg for unittest.mock.Mock()'s unsafe mode

* Make the requested changes.
2019-05-08 23:02:23 +05:30
Rémi Lapeyre 11a8832c98 bpo-31855: unittest.mock.mock_open() results now respects the argument of read([size]) (GH-11521)
unittest.mock.mock_open() results now respects the argument of read([size])

Co-Authored-By: remilapeyre <remi.lapeyre@henki.fr>
2019-05-07 11:48:36 +01:00
Serhiy Storchaka d53cf99dca
bpo-36542: Allow to overwrite the signature for Python functions. (GH-12705) 2019-05-06 22:40:27 +03:00
Chris Withers adbf178e49
Mock 100% coverage (GH-13045)
This was achieved by:
* moving many pass statements in tests onto their own lines, so they pass line coverage and can match an easy ignore pattern if branch coverage is added later.
* removing code that cannot be reached.
* removing long-disabled tests.
* removing unused code.
* adding tests for uncovered code

It turned out that removing `if __name__ == '__main__'` blocks that run unittest.main() at the bottom of test files was surprisingly contentious, so they remain and can be filtered out with an appropriate .coveragerc.
2019-05-01 23:04:04 +01:00
Chris Withers 49e27f0afb
remove jython support from unittest.mock (GH#13033) 2019-05-01 08:48:44 +01:00
Mario Corchero 0df635c7f8 Don't report deleted attributes in __dir__ (GH#10148)
When an attribute is deleted from a Mock, a sentinel is added rather
than just deleting the attribute. This commit checks for such sentinels
when returning the child mocks in the __dir__ method as users won't
expect deleted attributes to appear when performing dir(mock).
2019-04-30 19:56:36 +01:00
Pablo Galindo d5d2b45469
bpo-36751: Deprecate getfullargspec and report positional-only args as regular args (GH-13016)
* bpo-36751: Deprecate getfullargspec and report positional-only args as regular args

* Use inspect.signature in testhelpers
2019-04-30 02:01:14 +01:00
jkleint 39baace622 Document that TestCase.assertCountEqual() can take iterables (GH-686) 2019-04-23 01:34:29 -07:00
Xtreak 9b21856b0f bpo-23078: Add support for {class,static}method to mock.create_autospec() (GH-11613)
Co-authored-by: Felipe <felipe.nospam.ochoa@gmail.com>
2019-04-22 05:30:23 +03:00
Xtreak 830b43d03c bpo-36593: Fix isinstance check for Mock objects with spec executed under tracing (GH-12790)
In Python having a trace function in effect while mock is imported causes isinstance to be wrong for MagicMocks. This is due to the usage of super() in some class methods, as this sets the __class__ attribute. To avoid this, as a workaround, alias the usage of super .
2019-04-13 20:12:33 +01:00