Commit graph

49 commits

Author SHA1 Message Date
Miss Islington (bot)
951ed33054
[3.12] gh-104745: Limit starting a patcher more than once without stopping it (GH-126649) (#126773)
gh-104745: Limit starting a patcher more than once without stopping it (GH-126649)

Previously, this would cause an `AttributeError` if the patch stopped more than once after this, and would also disrupt the original patched object.

---------

(cherry picked from commit 1e40c5ba47)

Co-authored-by: Red4Ru <39802734+Red4Ru@users.noreply.github.com>
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
2024-11-13 08:37:34 +00:00
Miss Islington (bot)
e9578f6424
[3.12] gh-123934: Fix MagicMock not to reset magic method return values (GH-124038) (#124232)
gh-123934: Fix `MagicMock` not to reset magic method return values (GH-124038)
(cherry picked from commit 7628f67d55)

Co-authored-by: sobolevn <mail@sobolevn.me>
2024-09-19 08:18:01 +00:00
Miss Islington (bot)
98340a0e25
[3.12] gh-121708: Improve test coverage for unittest.util (GH-121713) (GH-121737)
(cherry picked from commit 901ea411bf)

Co-authored-by: Tomas R <tomas.roun8@gmail.com>
2024-07-14 10:58:55 +02:00
Miss Islington (bot)
34ace5ba4c
[3.12] gh-120732: Fix name passing to Mock, when using kwargs to create_autospec (GH-120737) (#120761)
gh-120732: Fix `name` passing to `Mock`, when using kwargs to `create_autospec` (GH-120737)
(cherry picked from commit 1e4815692f)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2024-06-19 20:53:19 +00:00
Miss Islington (bot)
454f7e8035
[3.12] gh-120544: Add else: fail() to tests where exception is expected (GH-120545) (#120547)
gh-120544: Add `else: fail()` to tests where exception is expected (GH-120545)
(cherry picked from commit 42ebdd83bb)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2024-06-15 12:54:22 +01:00
Miss Islington (bot)
f6481925d8
[3.12] gh-65454: avoid triggering call to a PropertyMock in NonCallableMock.__setattr__ (GH-120019) (#120337)
gh-65454: avoid triggering call to a PropertyMock in NonCallableMock.__setattr__ (GH-120019)
(cherry picked from commit 9e9ee50421)

Co-authored-by: blhsing <blhsing@gmail.com>
2024-06-11 06:07:25 +00:00
Miss Islington (bot)
fa291a35eb
[3.12] gh-119600: mock: do not access attributes of original when new_callable is set (GH-119601) (#120335)
gh-119600: mock: do not access attributes of original when new_callable is set (GH-119601)

In order to patch flask.g e.g. as in GH-84982, that
proxies getattr must not be invoked. For that,
mock must not try to read from the original
object. In some cases that is unavoidable, e.g.
when doing autospec. However, patch("flask.g",
new_callable=MagicMock) should be entirely safe.
(cherry picked from commit 422c4fc855)

Co-authored-by: Robert Collins <robert.collins@cognite.com>
2024-06-11 06:01:02 +00:00
Miss Islington (bot)
23ba96e243
[3.12] gh-90848: Fixed create_autospec ignoring configure_mock style kwargs (GH-118163) (#118517)
gh-90848: Fixed create_autospec ignoring configure_mock style kwargs (GH-118163)
(cherry picked from commit b28a3339e4)

Co-authored-by: infohash <46137868+infohash@users.noreply.github.com>
2024-05-02 18:01:17 +00:00
Miss Islington (bot)
a030bae584
[3.12] gh-117860: Add tests for resolving names when import rebind names (GH-118176) (GH-118432)
Add tests for "import", pkgutil.resolve_name() and unittest.mock.path()
for cases when "import a.b as x" and "from a import b as x" give
different results.
(cherry picked from commit c0eaa232f6)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2024-04-30 14:52:44 +00:00
Miss Islington (bot)
fbe29e639d
[3.12] gh-59215: unittest: restore _top_level_dir at end of discovery (GH-15242) (GH-117508)
* gh-59215: unittest: restore _top_level_dir at end of discovery (GH-15242)
(cherry picked from commit fc5f68e58e)


Co-authored-by: Zackery Spytz <zspytz@gmail.com>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
2024-04-23 12:41:32 +00:00
infohash
ad0ff86034
[3.12] gh-75988: Fix issues with autospec ignoring wrapped object (GH-115223) (#117119)
gh-75988: Fix issues with autospec ignoring wrapped object (#115223)

* set default return value of functional types as _mock_return_value

* added test of wrapping child attributes

* added backward compatibility with explicit return

* added docs on the order of precedence

* added test to check default return_value

(cherry picked from commit 735fc2cbbc)
2024-03-22 09:47:52 +00:00
Miss Islington (bot)
6e13e50859
[3.12] gh-115274: Fix direct invocation of testmock/testpatch.py (GH-115275) (#115280)
gh-115274: Fix direct invocation of `testmock/testpatch.py` (GH-115275)
(cherry picked from commit f8e9c57067)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2024-02-11 12:14:37 +03:00
Miss Islington (bot)
0a737639dc
[3.12] gh-113267: Revert "gh-106584: Fix exit code for unittest in Python 3.12 (GH-106588)" (GH-114470) (GH-114994)
This reverts commit 8fc071345b.
(cherry picked from commit ecabff98c4)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2024-02-04 16:00:56 +00:00
Miss Islington (bot)
159e3db1f7
[3.12] GH-113661: unittest runner: Don't exit 5 if tests were skipped (GH-113856) (#113875)
GH-113661: unittest runner: Don't exit 5 if tests were skipped (GH-113856)

The intention of exiting 5 was to detect issues where the test suite
wasn't discovered at all. If we skipped tests, it was correctly
discovered.
(cherry picked from commit 3a9096c337)

Co-authored-by: Stefano Rivera <stefano@rivera.za.net>
2024-01-09 13:34:38 -08:00
Miss Islington (bot)
577a8fb72b
[3.12] gh-111019: Align expected and actual titles in test output (GH-111020) (#111024)
gh-111019: Align expected and actual titles in test output (GH-111020)

Align expected and actual titles in output from
assert_has_calls/assert_called_with for greater readability
(cherry picked from commit 77dbd95609)

Co-authored-by: James <morisja@gmail.com>
2023-10-18 08:01:18 +00:00
Miss Islington (bot)
551aa6ab94
[3.12] gh-106584: Fix exit code for unittest in Python 3.12 (GH-106588) (#109725)
gh-106584: Fix exit code for unittest in Python 3.12 (GH-106588)
(cherry picked from commit 8fc071345b)

Co-authored-by: EliseevEgor <egor.eliseev@jetbrains.com>
Co-authored-by: sunmy2019 <59365878+sunmy2019@users.noreply.github.com>
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2023-10-02 17:18:41 +02:00
Miss Islington (bot)
2fb39f73ed
[3.12] gh-84867: Do not load tests from TestCase and FunctionTestCase (GH-100497) (#109327)
gh-84867: Do not load tests from TestCase and FunctionTestCase (GH-100497)
(cherry picked from commit 66d1d7eb06)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2023-10-02 16:57:44 +02:00
Miss Islington (bot)
460043b554
[3.12] gh-108927: Fix test_import + test_importlib + test_unittest problem (GH-108929) (#108954)
gh-108927: Fix test_import + test_importlib + test_unittest problem (GH-108929)
(cherry picked from commit 3f89b25763)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2023-09-05 20:43:47 +00:00
Miss Islington (bot)
4421c65f08
[3.12] gh-106300: Improve errors testing in test_unittest.test_runner (GH-106737) (#108006)
gh-106300: Improve errors testing in test_unittest.test_runner (GH-106737)

Use a custom exception to prevent unintentional silence of actual errors.
(cherry picked from commit fd9d70a94d)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2023-08-16 12:13:35 +02:00
Miss Islington (bot)
2ade2fc148
[3.12] gh-106300: Improve assertRaises(Exception) usages in tests (GH-106302) (GH-106534)
gh-106300: Improve `assertRaises(Exception)` usages in tests (GH-106302)
(cherry picked from commit 6e6a4cd523)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2023-07-07 14:25:24 -07:00
Michael Blahay
46361bb843
gh-68968: Correcting message display issue with assertEqual (#103937) 2023-05-04 16:37:17 -06:00
Stefano Rivera
76632b836c
gh-62432: unittest runner: Exit code 5 if no tests were run (#102051)
As discussed in https://discuss.python.org/t/unittest-fail-if-zero-tests-were-discovered/21498/7

It is common for test runner misconfiguration to fail to find any tests,
This should be an error.

Fixes: #62432
2023-04-27 01:28:46 +00:00
Giampaolo Rodola
ff3303e49c
gh-48330: address review comments to PR-12271 (#103209)
address review comments to PR-12271

Signed-off-by: Giampaolo Rodola <g.rodola@gmail.com>
2023-04-17 00:19:44 +02:00
Tomas R
59e0de4903
gh-102978: Fix mock.patch function signatures for class and staticmethod decorators (#103228)
Fixes unittest.mock.patch not enforcing function signatures for methods
decorated with @classmethod or @staticmethod when patch is called with
autospec=True.
2023-04-13 08:37:57 +01:00
Russell Keith-Magee
26c65980dc
gh-103329: Add regression test for PropertyMock with side effect (#103358) 2023-04-07 20:09:00 -06:00
Irit Katriel
52bc2e7b9d
gh-48330: assert warning is emitted on unittest.TestResult with no addDuration (#103309) 2023-04-06 14:05:23 +01:00
Giampaolo Rodola
6883007a86
bpo-4080: unittest durations (#12271) 2023-04-03 00:12:51 +02:00
Alex Waygood
401d7a7f00
gh-102515: Remove unused imports in the Lib/ directory (#102516) 2023-03-08 11:45:38 +00:00
Chris Withers
4e544eafcb
fix up mock tests coverage (#100874) 2023-01-09 10:08:56 +00:00
Christian Klein
1d4d677d1c
gh-100690: Raise an AttributeError when the assert_ prefix is forgotten when using Mock (#100691)
Mock objects which are not unsafe will now raise an AttributeError when accessing an
attribute that matches the name of an assertion but without the prefix `assert_`, e.g. accessing `called_once` instead of `assert_called_once`.

This is in addition to this already happening for accessing attributes with prefixes assert, assret, asert, aseert, and assrt.
2023-01-06 18:38:50 +00:00
Christian Klein
7f1eefc6f4
gh-100739: Respect mock spec when checking for unsafe prefixes (#100740)
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2023-01-04 22:31:29 +00:00
Chris Withers
457c1f4a19
Fix mock code coverage. (#100580) 2022-12-28 12:36:26 +00:00
Chris Withers
984894a9a2
Remove skipped test in test for async mocks. (#100559)
Remove skipped test.

See discussion on https://github.com/python/cpython/pull/25326.
Fix is apparently here, but no-one is confident to review and land: https://github.com/python/cpython/pull/25347.
2022-12-28 10:55:50 +00:00
Shantanu
e4b43ebb3a
gh-100287: Fix unittest.mock.seal with AsyncMock (#100496) 2022-12-24 19:39:39 +00:00
Carl Meyer
c5726b727e
gh-83076: 3.8x speed improvement in (Async)Mock instantiation (#100252) 2022-12-23 19:41:37 +00:00
AlexTate
72ec518203
gh-98458: unittest: bugfix for infinite loop while handling chained exceptions that contain cycles (#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
2022-12-04 11:37:55 -08:00
Serhiy Storchaka
c2102136be
gh-99645: Fix a bug in handling class cleanups in unittest.TestCase (GH-99646)
Now addClassCleanup() uses separate lists for different TestCase subclasses,
and doClassCleanups() only cleans up the particular class.
2022-11-22 17:49:37 +02:00
Serhiy Storchaka
653e563d80
Add more details in test_unittest (GH-99626) 2022-11-21 13:57:30 +02:00
Nikita Sobolev
67b4d2772c
gh-98086: Now `patch.dict` can decorate async functions (#98095) 2022-11-11 08:04:30 +00:00
andrei kulakov
c6325b1c9f
gh-91803: Mock - fix error when using autospec methods with seal (#92213)
Fixes https://github.com/python/cpython/issues/91803.

Co-authored-by: Karthikeyan Singaravelan <tir.karthi@gmail.com>
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
2022-11-07 07:24:46 +00:00
Nikita Sobolev
c3648f4e4a
gh-97837: Change deprecation warning message in unittest (#97838) 2022-10-04 17:29:18 -07:00
Dennis Sweeney
569ca27293
gh-96624: Fix test_dotted_but_module_not_loaded in testpatch.py (GH-96691)
* Update test_dotted_but_module_not_loaded to reflect the move of unittest.test to test.test_unittest.
2022-09-08 23:46:13 -04:00
Serhiy Storchaka
4de06e3cc0
gh-96021: Explicitly close the IsolatedAsyncioTestCase runner in tests (GH-96135)
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 close the runner explicitly even if
currently there is no a public API for this.
2022-08-24 15:07:20 +03:00
Kumar Aditya
9d515997f9
GH-95736: fix IsolatedAsyncioTestCase to initialize Runner before calling setup functions (#95898) 2022-08-16 08:52:06 -07:00
Robert O'Shea
cd26595232
gh-95454: Replace truthy/falsy with true/false (GH-95456) 2022-07-30 00:42:21 -07:00
Serhiy Storchaka
c834c02569
Revert "bpo-45162: Revert "Remove many old deprecated unittest features"" (GH-92556)
This reverts commit b50322d203.
2022-06-26 10:18:06 +03:00
Victor Stinner
47e35625ff
gh-84623: Remove unused imports (#94132) 2022-06-22 19:14:27 +02:00
Christian Heimes
50ebd72fb0
gh-93839: Use load_package_tests() for testmock (GH-94055)
Fixes failing tests on WebAssembly platforms.

Automerge-Triggered-By: GH:tiran
2022-06-21 05:51:39 -07:00
Victor Stinner
c735d54534
gh-93839: Move Lib/unttest/test/ to Lib/test/test_unittest/ (#94043)
* Move Lib/unittest/test/ to Lib/test/test_unittest/
* Remove Lib/test/test_unittest.py
* Replace unittest.test with test.test_unittest
* Remove unittest.load_tests()
* Rewrite unittest __init__.py and __main__.py
* Update build system, CODEOWNERS, and wasm_assets.py
2022-06-21 10:27:59 +02:00