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>
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>
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>
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>
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>
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>
* 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>
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)
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>
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>
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>
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>
Fixes unittest.mock.patch not enforcing function signatures for methods
decorated with @classmethod or @staticmethod when patch is called with
autospec=True.
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.
* 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
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.