Commit Graph

366 Commits

Author SHA1 Message Date
Miss Islington (bot) f49cc3c805
gh-100739: Respect mock spec when checking for unsafe prefixes (GH-100740)
(cherry picked from commit 7f1eefc6f4)

Co-authored-by: Christian Klein <167265+cklein@users.noreply.github.com>
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2023-01-04 15:11:46 -08:00
Shantanu 9975d4e7ba
[3.10] gh-100287: Fix unittest.mock.seal with AsyncMock (GH-100496) (#100508)
(cherry picked from commit e4b43ebb3a)

Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
2022-12-24 14:39:27 -06:00
Miss Islington (bot) e699e5c20f
gh-98458: unittest: bugfix for infinite loop while handling chained exceptions that contain cycles (GH-98459)
* Bugfix addressing infinite loop while handling self-referencing chained exception in TestResult._clean_tracebacks()
* Bugfix extended to properly handle exception cycles in _clean_tracebacks. The "seen" set follows the approach used in the TracebackException class (thank you @iritkatriel for pointing it out)
* adds a test for a single chained exception that holds a self-loop in its __cause__ and __context__ attributes
(cherry picked from commit 72ec518203)

Co-authored-by: AlexTate <0xalextate@gmail.com>
2022-12-04 11:58:31 -08:00
Serhiy Storchaka 400d41b8ea
[3.10] gh-99645: Fix a bug in handling class cleanups in unittest.TestCase (GH-99646) (GH-99699)
Now addClassCleanup() uses separate lists for different TestCase subclasses,
and doClassCleanups() only cleans up the particular class.

(cherry picked from commit c2102136be)
2022-11-22 19:11:57 +02:00
Serhiy Storchaka 190331ed28
[3.10] Add more details in test_unittest (GH-99626). (GH-99692)
(cherry picked from commit 653e563d80)
2022-11-22 17:13:37 +02:00
Miss Islington (bot) 88b101ff52
[3.10] gh-98086: Now ``patch.dict`` can decorate async functions (GH-98095) (GH-99366)
gh-98086: Now ``patch.dict`` can decorate async functions (GH-98095)
(cherry picked from commit 67b4d2772c)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Co-authored-by: Chris Withers <chris@withers.org>
2022-11-20 21:36:57 +05:30
Miss Islington (bot) 9b070d361d
gh-96021: Explicitly tear down the IsolatedAsyncioTestCase loop in tests (GH-96135) (GH-96235)
Tests for IsolatedAsyncioTestCase.debug() rely on the runner be closed
in __del__. It makes tests depending on the GC an unreliable on other
implementations. It is better to tear down the loop explicitly even if
currently there is no a public API for this.
(cherry picked from commit 4de06e3cc0)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2022-08-28 08:35:39 +03:00
Miss Islington (bot) 26fa25a9a7
bpo-24959: fix unittest.assertRaises bug where traceback entries are dropped from chained exceptions (GH-23688)
(cherry picked from commit 88b7d86a73)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
2022-03-08 14:09:28 -08:00
Irit Katriel e3ade66ec5
bpo-41403: Improve error message for invalid mock target (GH-30833) (GH-30834)
(cherry picked from commit f7955a82e3)
2022-01-23 19:34:43 +00:00
Nikita Sobolev a6a0885480
[3.10] bpo-46425: Fix direct invocation of multiple test modules (GH-30666) (GH-30699) 2022-01-20 10:44:21 +09:00
Miss Islington (bot) 9fe8fb74a1
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:29:54 -08:00
Serhiy Storchaka 83fa1291fd
[3.10] bpo-13236: Flush the output stream more often in unittest (GH-29864) (GH-29929)
It can prevent some losses when output to buffered stream..
(cherry picked from commit f42a06ba27)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2021-12-11 00:36:15 +01:00
Anthony Sottile 7c99e434a9
ensure deprecation warning from assertDictContainsSubset points at actual test code (#26497) 2021-11-15 21:11:02 -06:00
Christian Clauss cfca4a6774
[3.10] Fix typos in the Lib directory (GH-28775) (GH-28804)
Fix typos in the Lib directory as identified by codespell.

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

Co-authored-by: Christian Clauss <cclauss@me.com>
2021-10-07 11:49:47 -04:00
Łukasz Langa 44396aaba9
[3.10] bpo-45238: Fix unittest.IsolatedAsyncioTestCase.debug() (GH-28449) (GH-28521)
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:42:15 +02:00
Miss Islington (bot) 753f7af22e
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:31 -07:00
Miss Islington (bot) fd74d2680e
bpo-45156: Fixes inifite loop on unittest.mock.seal() (GH-28300) (GH-28326)
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 13:00:16 +02:00
Serhiy Storchaka 462c1f0403
[3.10] bpo-25130: Add calls of gc.collect() in tests to support PyPy (GH-28005) (GH-28027)
(cherry picked from commit 2a8127cafe)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2021-09-08 17:08:57 +02:00
Miss Islington (bot) 8342c526e9
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:55:35 -07:00
Miss Islington (bot) d65fad04fa
bpo-43913: Fix bugs in cleaning up classes and modules in unittest. (GH-28006)
* 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 10:38:34 -07:00
Miss Islington (bot) 243b8de0b1
bpo-41620: TestCase.run() now always return a TestResult instance (GH-28030)
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 06:43:38 -07:00
Miss Islington (bot) d63114caf9
bpo-44955: Always call stopTestRun() for implicitly created TestResult objects (GH-27831)
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 00:55:34 -07:00
Miss Islington (bot) 8516ca500e
bpo-44911: Fixed IsolatedAsyncioTestCase from throwing an exception on leaked tasks (GH-27765)
(cherry picked from commit 2cb1a6806c)

Co-authored-by: Bar Harel <bar.harel@biocatch.com>
2021-08-16 02:54:58 -07:00
Matthew Suozzo dccdc500f9
bpo-43478: Restrict use of Mock objects as specs (GH-25326)
* Restrict using Mock objects as specs as this is always a test bug where the resulting mock is misleadingly useless.
* Skip a broken test that exposes a bug elsewhere in mock (noted in the original issue).
2021-04-09 20:45:50 -07:00
vabr-g fdb9efce6a
bpo-41877: Check for misspelled speccing arguments (GH-23737)
patch, patch.object and create_autospec silently ignore misspelled
arguments such as autospect, auto_spec and set_spec. This can lead
to tests failing to check what they are supposed to check.

This change adds a check causing a RuntimeError if the above
functions get any of the above misspellings as arguments. It also
adds a new argument, "unsafe", which can be set to True to disable
this check.

Also add "!r" to format specifiers in added error messages.
2020-12-14 10:30:09 -08:00
vabr-g 9fc571359a
bpo-41877: Improve docs for assert misspellings check in mock (GH-23729)
This is a follow-up to
4662fa9bfe.
That original commit expanded guards against misspelling assertions on
mocks. This follow-up updates the documentation and improves the error
message by pointing out the potential cause and solution.

Automerge-Triggered-By: GH:gpshead
2020-12-10 10:35:28 -08:00
idanw206 c598a04dd2
bpo-42532: Check if NonCallableMock's spec_arg is not None instead of call its __bool__ function (GH23613)
Check if NonCallableMock's spec_arg is not None instead of call its __bool__ function
2020-12-06 09:59:36 +00:00
vabr-g 4662fa9bfe
bpo-41877 Check for asert, aseert, assrt in mocks (GH-23165)
Currently, a Mock object which is not unsafe will raise an
AttributeError if an attribute with the prefix assert or assret is
accessed on it. This protects against misspellings of real assert
method calls, which lead to tests passing silently even if the tested
code does not satisfy the intended assertion.

Recently a check was done in a large code base (Google) and three
more frequent ways of misspelling assert were found causing harm:
asert, aseert, assrt. These are now added to the existing check.
2020-11-05 09:04:38 -08:00
Irit Katriel 6fdfcec5b1
bpo-41943: Fix bug where assertLogs doesn't correctly filter messages… (GH-22565)
… by level

@vsajip , @pitrou

Automerge-Triggered-By: GH:vsajip
2020-11-02 11:25:29 -08:00
Lisa Roach 8374d2ee15
bpo-39101: Fixes BaseException hang in IsolatedAsyncioTestCase. (GH-22654) 2020-10-26 09:28:17 -07:00
Hai Shi a7f5d93bb6
bpo-40275: Use new test.support helper submodules in tests (GH-21449) 2020-08-03 18:41:24 +02:00
Kit Choi 6b34d7b51e
bpo-39385: Add an assertNoLogs context manager to unittest.TestCase (GH-18067)
Co-authored-by: Rémi Lapeyre <remi.lapeyre@henki.fr>
2020-07-01 22:08:38 +01:00
kernc 46398fba4d
bpo-29620: iterate over a copy of sys.modules (GH-4800)
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.
2020-06-11 14:03:29 -04:00
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 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
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
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
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
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