Commit Graph

47 Commits

Author SHA1 Message Date
Alex Waygood 19601efa36
gh-109653: Remove unused imports in the `Lib/` directory (#109803) 2023-09-24 15:07:23 +01:00
EliseevEgor 8fc071345b
gh-106584: Fix exit code for unittest in Python 3.12 (#106588)
Co-authored-by: sunmy2019 <59365878+sunmy2019@users.noreply.github.com>
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2023-09-22 14:26:27 +02:00
Nikita Sobolev 66d1d7eb06
gh-84867: Do not load tests from TestCase and FunctionTestCase (GH-100497) 2023-09-12 16:33:30 +03:00
Nikita Sobolev 3f89b25763
gh-108927: Fix test_import + test_importlib + test_unittest problem (#108929) 2023-09-05 18:57:48 +00:00
Sangyun_LEE 0c369d6cb8
Update Lib/test/test_unittest/testmock/testmock.py: fix typo RuntimError to RuntimeError (#108847) 2023-09-03 22:19:49 +01:00
Nikita Sobolev fd9d70a94d
gh-106300: Improve errors testing in test_unittest.test_runner (GH-106737)
Use a custom exception to prevent unintentional silence of actual errors.
2023-08-16 12:20:42 +03:00
Mario Corchero 7e96370a94
gh-61215: threadingmock: Improve test suite to avoid race conditions (#106822)
threadingmock: Improve test suite to avoid race conditions

Simplify tests and split them into multiple tests to prevent assertions
from triggering race conditions.
Additionally, we rely on calling the mocks without delay to validate the
functionality of matching calls.
2023-07-17 18:57:40 +00:00
Chris Withers e6379f72cb
Remove unused branches from mock module (#106617)
* lambda has a name of __none__, but no async lambda so this branch is not needed

* _get_signature_object only returns None for bound builtins. There are no async builtins so this branch isn't needed

* Exclude  a couple of methods from coverage checking in the downstream rolling backport of mock
2023-07-11 08:52:12 +00:00
Nikita Sobolev 6e6a4cd523
gh-106300: Improve `assertRaises(Exception)` usages in tests (GH-106302) 2023-07-07 13:42:40 -07:00
Mario Corchero 56353b1002
gh-106458: Mark `testthreadingmock.py` with `@requires_working_threading` (GH-106366)
Mark `testthreadingmock.py` with `threading_helper.requires_working_threading`.

Also add longer delays to reduce the change of a race conditions on the tests that validate short timeouts.
2023-07-06 10:54:45 -07:00
Mario Corchero 2dfc7fae78
gh-61215: Rename `wait_until_any_call` to `wait_until_any_call_with` (#106414)
mock: Rename `wait_until_any_call` to `wait_until_any_call_with`

Rename the method to be more explicit that it expects the args and
kwargs to wait for.
2023-07-04 18:34:43 +01:00
Mario Corchero d65b783b69
gh-61215: New mock to wait for multi-threaded events to happen (#16094)
mock: Add `ThreadingMock` class

Add a new class that allows to wait for a call to happen by using
`Event` objects. This mock class can be used to test and validate
expectations of multithreading code.

It uses two attributes for events to distinguish calls with any argument
and calls with specific arguments.

The calls with specific arguments need a lock to prevent two calls in
parallel from creating the same event twice.

The timeout is configured at class and constructor level to allow users
to set a timeout, we considered passing it as an argument to the
function but it could collide with a function parameter. Alternatively
we also considered passing it as positional only but from an API
caller perspective it was unclear what the first number meant on the
function call, think `mock.wait_until_called(1, "arg1", "arg2")`, where
1 is the timeout.

Lastly we also considered adding the new attributes to magic mock
directly rather than having a custom mock class for multi threading
scenarios, but we preferred to have specialised class that can be
composed if necessary. Additionally, having added it to `MagicMock`
directly would have resulted in `AsyncMock` having this logic, which
would not work as expected, since when if user "waits" on a
coroutine does not have the same meaning as waiting on a standard
call.

Co-authored-by: Karthikeyan Singaravelan <tir.karthi@gmail.com>
2023-07-03 07:56:54 +01:00
Samet YASLAN 3f7c0810f6
bpo-44185: Added close() to mock_open __exit__ (#26902) 2023-06-11 19:51:21 +01:00
Thomas Grainger 9bf8d825a6
gh-94924: support `inspect.iscoroutinefunction` in `create_autospec(async_def)` (#94962)
* support inspect.iscoroutinefunction in create_autospec(async_def)

* test create_autospec with inspect.iscoroutine and inspect.iscoroutinefunction

* test when create_autospec functions check their signature
2023-06-09 13:29:09 +00:00
Victor Stinner ae319e4b43
gh-105407: Remove unused imports in tests (#105408) 2023-06-06 22:50:43 +02:00
Nikita Sobolev 219f01b185
gh-83403: Test `parent` param in `Mock.__init__` (#103630) 2023-05-30 08:36:22 +01:00
Hugo van Kemenade b1cb30ec86
gh-104835: Remove unittest's deprecated getTestCaseNames, makeSuite, findTestCases (#104836) 2023-05-24 21:16:43 +00:00
melanie witt 2e0931046d
gh-85934: Use getattr_static when adding mock spec (#22209)
Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
2023-05-23 17:10:34 -06: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