Commit Graph

517 Commits

Author SHA1 Message Date
Miss Islington (bot) cb3c85d40d
[3.10] gh-91676 gh-91260 unittest.IsolatedAsyncioTestCase no longer leaks its executor (GH-91680)
For things like test_asyncio.test_thread this was causing frequent
"environment modified by test" errors as the executor threads had not
always stopped running after the test was over.
(cherry picked from commit 61570ae0bc)

Co-authored-by: Gregory P. Smith <greg@krypto.org>
2022-04-19 09:40:52 -07:00
Miss Islington (bot) 4cc4fe2789
[3.9] GH-89074: Fixed IsolatedAsyncioTestCase from throwing an exception on leaked tasks (GH-27765) (#91471)
(cherry picked from commit 2cb1a6806c)

Co-authored-by: Bar Harel <bar.harel@biocatch.com>
2022-04-11 23:14:43 -05:00
Miss Islington (bot) 1cab44d865
[3.9] bpo-46421: Fix unittest filename evaluation when called as a module (GH-30654) (GH-31970)
(cherry picked from commit a0db11b10f)

Co-authored-by: Bader Zaidan <bader@zaidan.pw>
2022-03-17 20:24:59 -07:00
Irit Katriel f3ea249569
bpo-24959: fix unittest.assertRaises bug where traceback entries are dropped from chained exceptions (GH-23688) (GH-31776)
(cherry picked from commit 88b7d86a73)
2022-03-08 23:00:45 +00:00
Irit Katriel 1398dca838
bpo-41403: Improve error message for invalid mock target (GH-30833) (GH-30835)
(cherry picked from commit f7955a82e3)
2022-01-23 19:35:15 +00:00
Nikita Sobolev 8105dd2411
[3.9] bpo-46425: Fix direct invocation of multiple test modules (GH-30666) (GH-30700) 2022-01-20 10:43:49 +09:00
Andrew Svetlov f8fce5e4e4
[3.9] bpo-46129: Rewrite asyncio.locks tests with IsolatedAsyncioTestCase (GH-30198) (GH-30204)
Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>.
(cherry picked from commit 9c06fd8951)

Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
2021-12-19 18:59:37 +02:00
Miss Islington (bot) 72225b5bdf
bpo-46111: Fix unittest tests in optimized mode (GH-30163)
(cherry picked from commit 95a922b3bb)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2021-12-17 01:35:58 -08:00
Miss Islington (bot) d55a03e02e
[3.10] bpo-13236: Flush the output stream more often in unittest (GH-29929) (GH-30039)
It can prevent some losses when output to buffered stream..
(cherry picked from commit 83fa1291fd)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2021-12-11 01:34:46 +01:00
Miss Islington (bot) 71d842b9c1
bpo-29620: iterate over a copy of sys.modules (GH-4800) (GH-29605)
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.

Patch by Kernc.
(cherry picked from commit 46398fba4d)

Co-authored-by: kernc <kerncece@gmail.com>
2021-11-18 17:09:41 +01:00
Christian Clauss 4346b81cc6
[3.9] Fix typos in the Lib directory (GH-28775) (GH-28803)
Fix typos in the Lib directory as identified by codespell.

Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
(cherry picked from commit 745c9d9dfc)

Automerge-Triggered-By: GH:JulienPalard
2021-10-07 08:42:38 -07:00
Łukasz Langa e06b0fddf6
[3.9] bpo-45238: Fix unittest.IsolatedAsyncioTestCase.debug() (GH-28449) (GH-28522)
It runs now asynchronous methods and callbacks.

If it fails, doCleanups() can be called for cleaning up..
(cherry picked from commit ecb6922ff2)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2021-09-22 18:48:17 +02:00
Miss Islington (bot) 7e465a6b82
bpo-36674: Honour the skipping decorators in TestCase.debug() (GH-28446)
unittest.TestCase.debug() raises now a SkipTest if the class or
the test method are decorated with the skipping decorator.

Previously it only raised a SkipTest if the test method was decorated
with other decorator in addition to the skipping decorator, or
if SkipTest was explicitly raised in the test or setup methods.
(cherry picked from commit dea59cf88a)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2021-09-18 06:22:40 -07:00
Miss Islington (bot) 13257d9fca
bpo-45156: Fixes inifite loop on unittest.mock.seal() (GH-28300)
Fixes infinite loop on unittest.mock.seal() of mocks created by
unittest.create_autospec().

Co-authored-by: Dong-hee Na <donghee.na92@gmail.com>
(cherry picked from commit 7f60c9e1c6)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2021-09-14 03:50:28 -07:00
Miss Islington (bot) f91d974ce6
bpo-45102: unittest: add tests for skipping and errors in cleanup (GH-28166)
(cherry picked from commit 28264269de)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2021-09-05 00:56:24 -07:00
Serhiy Storchaka 9827710a40
[3.9] bpo-43913: Fix bugs in cleaning up classes and modules in unittest. (GH-28006) (GH-28071)
* Functions registered with addModuleCleanup() were not called unless
  the user defines tearDownModule() in their test module.
* Functions registered with addClassCleanup() were not called if
  tearDownClass is set to None.
* Buffering in TestResult did not work with functions registered
  with addClassCleanup() and addModuleCleanup().
* Errors in functions registered with addClassCleanup() and
  addModuleCleanup() were not handled correctly in buffered and
  debug modes.
* Errors in setUpModule() and functions registered with
  addModuleCleanup() were reported in wrong order.
* And several lesser bugs..
(cherry picked from commit 08d9e597c8)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2021-08-30 19:22:21 +02:00
Miss Islington (bot) 0c5e0aa73f
bpo-41620: TestCase.run() now always return a TestResult instance (GH-28030) (GH-28065)
Previously it returned None if the test class or method was
decorated with a skipping decorator.

Co-authored-by: Iman Tabrizian <iman.tabrizian@gmail.com>
(cherry picked from commit 7e246a3a7b)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2021-08-30 15:42:34 +02:00
Serhiy Storchaka 330aabbbbe
[3.9] bpo-25130: Add calls of gc.collect() in tests to support PyPy (GH-28005). (GH-28028)
(cherry picked from commit 2a8127cafe)
2021-08-29 15:08:32 +03:00
Miss Islington (bot) 4e5162fd36
bpo-44955: Always call stopTestRun() for implicitly created TestResult objects (GH-27831) (GH-27882)
Method stopTestRun() is now always called in pair with method startTestRun()
for TestResult objects implicitly created in TestCase.run().
Previously it was not called for test methods and classes decorated with
a skipping decorator.
(cherry picked from commit a9640d7553)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2021-08-22 21:15:10 +03:00
Miss Islington (bot) 9d409d6b47
bpo-39101: Fixes BaseException hang in IsolatedAsyncioTestCase. (GH-22654)
(cherry picked from commit 8374d2ee15)

Co-authored-by: Lisa Roach <lisaroach14@gmail.com>
2020-12-16 09:56:10 -08:00
Karthikeyan Singaravelan 14f2a124e2
[3.9] bpo-42532: Check if NonCallableMock's spec_arg is not None instead of call its __bool__ function (GH-23613) (GH-23676)
Check if NonCallableMock's spec_arg is not None instead of call its __bool__ function
(cherry picked from commit c598a04dd2)

Co-authored-by: idanw206 <31290383+idanw206@users.noreply.github.com>
2020-12-14 11:19:16 +05:30
Furkan Önder 719e14d283
bpo-40462: fix variable and function names (GH-19832)
Automerge-Triggered-By: @vstinner
2020-05-01 05:49:35 -07:00
Karthikeyan Singaravelan 521c8d6806
bpo-39966: Revert "bpo-25597: Ensure wraps' return value is used for magic methods in MagicMock" (GH-19734)
* Revert "bpo-25597: Ensure wraps' return value is used for magic methods in MagicMock (#16029)"

This reverts commit 72b1004657.
2020-04-28 20:22:31 +01:00
Serhiy Storchaka 515fce4fc4
bpo-40275: Avoid importing logging in test.support (GH-19601)
Import logging lazily in assertLogs() in unittest.
Move TestHandler from test.support to logging_helper.
2020-04-25 11:35:18 +03:00
Serhiy Storchaka 3c8a5b459d
bpo-40275: Avoid importing asyncio in test.support (GH-19600)
* Import asyncio lazily in unittest (only when IsolatedAsyncioTestCase is used).
* Import asyncio.events lazily in test.support.
2020-04-25 10:04:10 +03:00
Serhiy Storchaka 4b222c9491
bpo-40126: Fix reverting multiple patches in unittest.mock. (GH-19351)
Patcher's __exit__() is now never called if its __enter__() is failed.
Returning true from __exit__() silences now the exception.
2020-04-11 10:59:24 +03:00
Batuhan Taşkaya 0361556537
bpo-39481: PEP 585 for a variety of modules (GH-19423)
- concurrent.futures
- ctypes
- http.cookies
- multiprocessing
- queue
- tempfile
- unittest.case
- urllib.parse
2020-04-10 07:46:36 -07:00
Karthikeyan Singaravelan e553f204bf
bpo-39915: Ensure await_args_list is updated according to the order in which coroutines were awaited (GH-18924)
Create call objects with awaited arguments instead of using call_args which has only last call value.
2020-03-11 15:06:12 +00:00
Steve Cirelli 032de7324e
bpo-39450 Stripped whitespace before parsing the docstring in TestCase.shortDescription (GH-18175) 2020-02-03 07:06:50 +00:00
Chris Withers db5e86adbc
Get mock coverage back to 100% (GH-18228)
* use the `: pass` and `: yield` patterns for code that isn't expected to ever be executed.

* The _Call items passed to _AnyComparer are only ever of length two, so assert instead of if/else

* fix typo

* Fix bug, where stop-without-start patching dict blows up with `TypeError: 'NoneType' object is not iterable`, highlighted by lack of coverage of an except branch.

* The fix for bpo-37972 means _Call.count and _Call.index are no longer needed.

* add coverage for calling next() on a mock_open with readline.return_value set.

* __aiter__ is defined on the Mock so the one on _AsyncIterator is never called.
2020-01-29 16:24:54 +00:00
Carl Friedrich Bolz-Tereick a327677905
bpo-39485: fix corner-case in method-detection of mock (GH-18252)
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.
2020-01-29 15:43:37 +00:00
Chris Withers a46575a8f2
Clarify and fix assertions that mocks have not been awaited (GH-18196)
- The gc.collect is needed for other implementations, such as pypy
- Using context managers over multiple lines will only catch the warning from the first line in the context!
- remove a skip for a test that no longer fails on pypy
2020-01-27 14:55:56 +00:00
Chris Withers c7dd3c7d87
Use relative imports in mock and its tests to help backporting (GH-18197)
* asyncio.run only available in 3.8+

* iscoroutinefunction has important bungfixes in 3.8

* IsolatedAsyncioTestCase only available in 3.8+
2020-01-27 14:11:19 +00:00
Karthikeyan Singaravelan 72b1004657 bpo-25597: Ensure wraps' return value is used for magic methods in MagicMock (#16029) 2020-01-27 06:48:15 +00:00
Vegard Stikbakke aef7dc8987 bpo-38932: Mock fully resets child objects on reset_mock(). (GH-17409) 2020-01-25 15:44:46 +00:00
Paulo Henrique Silva 40c080934b bpo-37955: correct mock.patch docs with respect to the returned type (GH-15521) 2020-01-25 10:53:54 +00:00
Matthew Kokotovich 62865f4532 bpo-39082: Allow AsyncMock to correctly patch static/class methods (GH-18116) 2020-01-25 10:17:47 +00:00
Karthikeyan Singaravelan 66b00a9d3a bpo-38473: Handle autospecced functions and methods used with attach_mock (GH-16784) 2020-01-24 13:14:29 +00:00
Mario Corchero e131c9720d Fix `mock.patch.dict` to be stopped with `mock.patch.stopall` (#17606)
As the function was not registering in the active patches, the mocks
started by `mock.patch.dict` were not being stopped when
`mock.patch.stopall` was being called.
2020-01-24 08:38:32 +00:00
Emmanuel Arias 1d0c5e16ea bpo-24928: Add test case for patch.dict using OrderedDict (GH -11437)
* add test for path.dict using OrderedDict

Co-authored-by: Yu Tomita nekobon@users.noreply.github.com
2020-01-24 08:14:14 +00:00
Karthikeyan Singaravelan 54f743eb31 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.
2020-01-15 09:49:49 +00:00
Chris Withers 31d6de5aba
remove unused __version__ from mock.py (#17977)
This isn't included in `__all__` and could be a source of confusion.
2020-01-13 19:11:34 +00:00
Elena Oat cd90a52983 bpo-38669: patch.object now raises a helpful error (GH17034)
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.
2019-12-08 20:14:38 +00:00
Jason Fried 046442d02b bpo-38857: AsyncMock fix for awaitable values and StopIteration fix [3.8] (GH-17269) 2019-11-20 16:27:51 -08:00
Pablo Galindo 293dd23477
Remove binding of captured exceptions when not used to reduce the chances of creating cycles (GH-17246)
Capturing exceptions into names can lead to reference cycles though the __traceback__ attribute of the exceptions in some obscure cases that have been reported previously and fixed individually. As these variables are not used anyway, we can remove the binding to reduce the chances of creating reference cycles.

See for example GH-13135
2019-11-19 21:34:03 +00:00
Adam Johnson 892221bfa0 bpo-38839: Fix some unused functions in tests (GH-17189) 2019-11-19 11:45:20 -08:00
Lisa Roach 3667e1ee6c
bpo-38163: Child mocks detect their type as sync or async (GH-16471) 2019-09-29 21:56:47 -07:00
Lisa Roach 25e115ec00
bpo-38161: Removes _AwaitEvent from AsyncMock. (GH-16443) 2019-09-29 21:01:28 -07:00
Lisa Roach 9a7d951950
bpo-38108: Makes mock objects inherit from Base (GH-16060) 2019-09-28 18:42:44 -07:00
Samuel Freilich 2180f6b058 bpo-36871: Avoid duplicated 'Actual:' in assertion message (GH-16361)
Fixes an issue caught after merge of PR 16005.

Tightened test assertions to check the entire assertion message.
2019-09-24 15:04:29 -07:00