Commit Graph

816 Commits

Author SHA1 Message Date
Stefan Zimmermann 61c204a035
Fix handling of positional-only parameters in test methods (#13377)
Fixes #13376
2025-04-18 21:18:36 +00:00
Callum Scott 9a7dbd8a39
Add title and change spelling for use of approx with non-numerics (#13343)
* Added title to draw attention to the easily-missed information on non-numeric types for approx.
* Changed spelling of all cases of "nonnumeric" to "non-numeric" within docstrings for clarity and consistency with .rst files.

Related to #13218
2025-04-03 09:53:43 -03:00
Bahram Farahmand 0c454979cc
Fix `approx` when mixing numpy.bool and bool (#13338)
Fix #13047

---------

Co-authored-by: Bruno Oliveira <bruno@pytest.org>
2025-04-02 07:31:53 -03:00
John Litborn 134b759e5d
raisesgroup followups (#13279)
* raisesgroup followups
* renames src/_pytest/raises_group.py to src/_pytest/raises.py
* moves pytest.raises from src/_pytest/python_api.py to src/_pytest/raises.py
* adds several newsfragments that should've been bundled with #13192
* add more detailed error message if you try to do RaisesGroup((ValueError, TypeError))
* mess around with ValueError vs TypeError on invalid expected exception
* revert change in behaviour if raises has a type mismatch
* add check example to raises, fix test after behaviour revert
* made args to AbstractMatcher, RaisesExc and RaisesGroup pos/kw-only

---------

Co-authored-by: Ran Benita <ran@unusedvar.com>
Co-authored-by: Bruno Oliveira <bruno@soliv.dev>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2025-03-29 13:08:12 +00:00
Ronny Pfannschmidt d149ab353a
assorted code updates and f string migrations (#12017)
* chore: setuponly - migrate to f string

* chore: MockTiming - move impl to _pytest.timing

* chore: fixture tests: migrate to f-strings

* chore: skipping tests: migrate to f-strings

* chore: junitxml tests: add more type annotations

* chore: junitxml tests: introduce more typesafe helpers

* add changelog

* test junitxml: split the DomNode type to express the Document/Element difference

* fixup: undo accidential name-mangling in the junitxml test helpers

* fixup: junitxml logging test - restore validation for expected output nodes

* fixup: have fake config .getini use correct types

* Update changelog/12017.improvement.rst

Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>

* use contrib category for changelog

* use datetime.fromisoformat instead of strptime

* post rebase ruff fixes

* add extra coverage for junit test helpers

---------

Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
2025-03-11 09:14:36 +01:00
Anton Zhilin ada9977b2a
Prevent parametrize with scope from breaking fixture dependencies (#13249)
* Prevent parametrize with scope from breaking fixture dependencies

* Add myself to AUTHORS

* Fix the regression test

* Assert fixture value inside the test

---------

Co-authored-by: Bruno Oliveira <bruno@pytest.org>
2025-03-09 21:35:11 +01:00
John Litborn a98d093a71
improve raisesgroup code coverage (#13275)
* improve raisesgroup code coverage

* fix coverage of a weird branch in python_api, explicitify match in other test

* remove comment
2025-03-07 11:04:46 -08:00
jakkdl 5186f3677e
Merge remote-tracking branch 'origin/main' into raisesgroup 2025-03-03 12:28:16 +01:00
Anton Zhilin 611bb13108
Allow hiding a parameter set from test name (#13229)
Fixes #13228

---------

Co-authored-by: 🇺🇦 Sviatoslav Sydorenko (Святослав Сидоренко) <wk.cvs.github@sydorenko.org.ua>
2025-03-01 10:06:40 -03:00
jakkdl 753df941da
harmonize stringify_exception, various comments 2025-02-21 13:01:57 +01:00
jakkdl 09d06fe99d
fix tests 2025-02-20 17:14:43 +01:00
jakkdl 2c8cd64d5f
make pytest.raises use RaisesExc... which made me notice about a million small things differing between raises and RaisesExc, and some random other stuff... 2025-02-20 16:38:02 +01:00
jakkdl 4d2c70911b
fix imports after file rename 2025-02-18 15:16:36 +01:00
jakkdl c011e9b6f3 fix test, fix references in docstrings 2025-02-06 17:12:56 +01:00
jakkdl e73c4111d2 fix test on py<311 2025-02-06 15:52:56 +01:00
jakkdl e1e1874cda rename AbstractMatcher -> AbstractRaises, Matcher->RaisesExc. Add docs on RaisesGroup&RaisesExc. Add warnings to group_contains. Remove group_contains example from getting-started page 2025-02-06 15:39:25 +01:00
jakkdl 4737c8cf95 add AbstractMatcher support to xfail 2025-02-04 15:33:06 +01:00
jakkdl c93130c1b9 add RaisesGroup & Matcher 2025-02-04 13:31:23 +01:00
Shaygan Hooshyari ecde993e17
fixtures: replace fixture representation with a class (#12473)
During the sprint we discussed fixing the above issue and thought maybe it's a better idea to add a better representation to fixtures. To do this without patching the object more, this PR refactors fixtures to have a class with attributes.

The main rational behind that is:

- Now we have a type for fixtures makes it easier to check for fixture types and no need to perform cast on objects. Removed monkey patching.

- They are no longer functions that carry a class within them.

- When you decorate a function with a fixture it's not possible to call it anymore. So it makes more sense for it to not be a function or method.

Fixes #11525

---------

Co-authored-by: Sviatoslav Sydorenko (Святослав Сидоренко) <wk.cvs.github@sydorenko.org.ua>
Co-authored-by: Bruno Oliveira <bruno@soliv.dev>
Co-authored-by: Ran Benita <ran@unusedvar.com>
2024-12-07 08:51:26 -03:00
Daara b2dce69664
Warn `pytest.mark.usefixtures()` is used without arguments (#13008)
Closes #12426
Closes #12439

---------

Co-authored-by: Jiajun Xu <jiajunxu2@gmail.com>
Co-authored-by: Bruno Oliveira <bruno@pytest.org>
2024-12-01 08:08:33 -03:00
Pierre Sassoulas 17c5bbbdae [Fix UP031] Manually, keeping some required %r specifiers
Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
2024-11-30 14:57:44 +01:00
Marcelo Duarte Trevisani d65d991776
approx: do not raise error in mixed sequence/collections
Previously `approx` would show an internal error when passed a collection with numbers and non-numbers mixed:

```
E         (pytest_assertion plugin: representation of details failed: /tmp/test_a/.venv/lib/python3.12/site-packages/_pytest/python_api.py:343: TypeError: unsupported operand type(s) for -: 'str' and 'str'.
E          Probably an object has a faulty __repr__.)
```

The problem was that it was unconditionally computing the diff for every element. 

This change introduces a try/except catch around the code related to computing the diff, and ignores it instead of letting the error be raised.

Fixes #13010

Co-authored-by: Bruno Oliveira <bruno@pytest.org>
2024-11-30 12:31:52 +00:00
Jakob van Santen a16e8eac8c
approx: use exact comparison for bool
Fixes #9353
2024-11-29 14:30:48 -03:00
Pierre Sassoulas a4cb74e864 Upgrade doc and CI after dropping python 3.8 2024-11-25 20:11:18 +01:00
jakkdl 922d6841b0 avoid test failing if the working path contains the string 'error' 2024-10-23 17:12:04 +02:00
moajo a14c718867
Fix `disable_test_id_escaping_and_forfeit_all_rights_to_community_support` option when using `pytest.param(..., id="...")`
Fixes #9037

---------

Co-authored-by: Ronny Pfannschmidt <opensource@ronnypfannschmidt.de>
Co-authored-by: Bruno Oliveira <bruno@pytest.org>
2024-10-09 20:52:26 +00:00
Christopher Head 23256dd967
Prohibit pytest.mark.usefixtures in pytest.param (#12828)
Mitigates #4112 a bit.

Co-authored-by: Bruno Oliveira <bruno@soliv.dev>
2024-10-09 16:13:56 -03:00
Anthony Sottile c6a5290322
Do not discover properties when iterating fixtures (#12781)
Resolves #12446
2024-09-08 14:45:05 +00:00
Christian Clauss 09e386e3e0
Fix typos and introduce codespell pre-commit hook (#12769) 2024-09-03 20:26:49 +00:00
Sadra Barikbin 6a3ac51ee2
Revert the unintended change in tests reordering from #11220 (#12542)
In #11220, an unintended change in reordering was introduced by changing the way indices were assigned to direct params. This PR reverts that change and reduces #11220 changes to just refactors.

After this PR we could safely decide on the solutions discussed in #12008, i.e. #12082 or the one initially introduced in #11220 .

Fixes #12008

Co-authored-by: Bruno Oliveira <bruno@soliv.dev>
Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
2024-08-11 06:36:32 +02:00
fazeelghafoor 5a01583687
Improve approx repr for better readability (#12665)
Adjust `ApproxScalar.__repr__` to format tolerances in decimal form for smaller ranges.

Closes #6985

---------

Co-authored-by: Bruno Oliveira <bruno@soliv.dev>
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
2024-08-01 19:26:50 -03:00
Nicolas Simonds d489247505
Fix caching of parameterized fixtures (#12600)
The fix for Issue #6541 caused regression where cache hits became cache misses, unexpectedly.  

Fixes #6962

---------

Co-authored-by: Nicolas Simonds <nisimond@cisco.com>
Co-authored-by: Bruno Oliveira <bruno@pytest.org>
Co-authored-by: Ran Benita <ran@unusedvar.com>
2024-07-17 10:24:03 -03:00
Pierre Sassoulas 92ec5c349d [python 3.13] Mark functool.partial test to be skipped
Closes #12552
2024-07-04 11:46:19 +02:00
Virendra Patil 49bb5c89a6
Improved handling of invalid regex pattern in pytest.raises (#12526) 2024-07-02 21:58:08 +03:00
Ronny Pfannschmidt 9295f9ffff RFC: from __future__ import annotations + migrate 2024-06-20 11:03:03 +02:00
Ronny Pfannschmidt 76f3f3da00 fix #11797: be more lenient on SequenceLike approx
this needs a validation as it allows partially implemented sequences
2024-06-18 17:01:24 +02:00
Ran Benita e89d23b247 fixtures: fix catastrophic performance problem in `reorder_items`
Fix #12355.

In the issue, it was reported that the `reorder_items` has quadratic (or
worse...) behavior with certain simple parametrizations. After some
debugging I found that the problem happens because the "Fix
items_by_argkey order" loop keeps adding the same item to the deque,
and it reaches epic sizes which causes the slowdown.

I don't claim to understand how the `reorder_items` algorithm works, but
if as far as I understand, if an item already exists in the deque, the
correct thing to do is to move it to the front. Since a deque doesn't
have such an (efficient) operation, this switches to `OrderedDict` which
can efficiently append from both sides, deduplicate and move to front.
2024-06-04 10:15:50 +03:00
Ran Benita fdf3aa3fc3
Merge pull request #12329 from pytest-dev/fix-package-scope-reorder
fixtures: fix non-working package-scope parametrization reordering
2024-05-16 10:30:34 +03:00
Josh Soref 8d00811822
Spelling and minor changes (#12122) 2024-05-15 13:49:34 -03:00
Ran Benita 1acf56d033 fixtures: fix non-working package-scope parametrization reordering
The `.parent` was incorrectly removed in
a21fb87a90, but it was actually correct
(well, it assumes that Module.path.parent == Package.path, which I'm not
sure is always correct, but that's a different matter). Restore it.

Fix #12328.
2024-05-15 17:29:43 +03:00
Ran Benita 37489d3c6c python,unittest: don't collect abstract classes
Fix #12275.
2024-05-13 13:00:47 +03:00
Pierre Sassoulas 4788165e69 [ruff UP031] Fix to use format specifiers instead of percent format 2024-04-30 18:06:26 +02:00
Ran Benita 0b91d5e3e8 fixtures: fix tracebacks for higher-scoped failed fixtures getting longer and longer
Fix #12204.
2024-04-28 13:06:32 +03:00
poulami-sau 5cffef7f07
Fixed Bug Regarding Attribute Error in pytest.approx For Types Implicitly Convertible to Numpy Arrays (#12232)
* added test case in testing/python/approx.py based on test case provided by reporter in issue #12114
* test cases pass for pytest testing/python/approx.py
* expanded the type annotation to include objects which may cast to a array and renamed other_side to other_side_as_array and asserted that it is not none
2024-04-23 10:45:33 +02:00
John Litborn e64efd8653
Don't reregister subfixture finalizer in requested fixture if value is cached (#12136) 2024-03-31 15:02:09 +03:00
John Litborn 70c11582aa
Don't add fixture finalizer if the value is cached (#11833)
Fixes #1489
2024-03-16 23:45:56 +02:00
jakkdl bec0e9caf8 Add tests to ensure setup&finalization for scoped fixtures only run once. 2024-03-15 12:26:28 +01:00
pre-commit-ci[bot] c0532dda18
[pre-commit.ci] pre-commit autoupdate (#12115)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
Co-authored-by: Ran Benita <ran@unusedvar.com>
2024-03-13 15:30:18 +02:00
Ran Benita 0dc0360351 python: fix instance handling in static and class method tests
and also fixes a regression in pytest 8.0.0 where `setup_method` crashes
if the class has static or class method tests.

It is allowed to have a test class with static/class methods which
request non-static/class method fixtures (including `setup_method`
xunit-fixture). I take it as a given that we need to support this
somewhat odd scenario (stdlib unittest also supports it).

This raises a question -- when a staticmethod test requests a bound
fixture, what is that fixture's `self`?

stdlib unittest says - a fresh instance for the test.

Previously, pytest said - some instance that is shared by all
static/class methods. This is definitely broken since it breaks test
isolation.

Change pytest to behave like stdlib unittest here.

In practice, this means stopping to rely on `self.obj.__self__` to get
to the instance from the test function's binding. This doesn't work
because staticmethods are not bound to anything.

Instead, keep the instance explicitly and use that.

BTW, I think this will allow us to change `Class`'s fixture collection
(`parsefactories`) to happen on the class itself instead of a class
instance, allowing us to avoid one class instantiation. But needs more
work.

Fixes #12065.
2024-03-09 19:35:54 +02:00
Ran Benita 71671f60b5 fixtures: improve fixture scope mismatch message
- Separate the requesting from the requested.

- Avoid the term "factory", I think most people don't distinguish
  between "fixture" and "fixture function" (i.e. "factory") and would
  find the term "factory" unfamiliar.
2024-03-09 00:02:06 +02:00