Commit Graph

3325 Commits

Author SHA1 Message Date
Loïc Estève 1c9d6834fd Improve assert mod not in mods error message
[ran: tweaked message, made the formatting lazy]
2024-01-14 13:21:54 +02:00
Franck Charras a7c2549321 Fix `assert mod not in mods` crash
Fix #27806.

Co-authored-by: Loïc Estève <loic.esteve@ymail.com>
Co-authored-by: Ran Benita <ran@unusedvar.com>
Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
2024-01-13 20:19:28 +02:00
Ran Benita 06dbd3c21c doctest: remove special conftest handling
(Diff better viewed ignoring whitespace)

Since e1c66ab0ad, conftest loading is
handled at the directory level before sub-nodes are collected, so there
is no need for the doctest plugin to handle it specially.

This was probably the case even before
e1c66ab0ad, but I haven't verified this.
2024-01-13 11:18:41 +02:00
Ran Benita 5645fa45fb
Merge pull request #11801 from bluetech/node-iterchain
nodes: add `Node.iterchain()` function
2024-01-12 11:01:48 +02:00
Ran Benita 5bd5b80afd nodes: add `Node.iterparents()` function
This is a useful addition to the existing `listchain`. While `listchain`
returns top-to-bottom, `iterparents` is bottom-to-top and doesn't require
an internal full iteration + `reverse`.
2024-01-11 23:19:45 +02:00
Ran Benita bd58c09500
Merge pull request #11799 from bluetech/rm-nose-compat_co_firstlineno
python: remove support for nose's `compat_co_firstlineno`
2024-01-11 13:13:30 +02:00
Ran Benita c7d85c5dc6 python: remove support for nose's `compat_co_firstlineno`
Since we're removing nose support, let's also drop support for this
attribute.

From doing a code search on github, this seems completely unused outside
of nose, except for some projects which used to use it, but no longer
do.
2024-01-10 19:22:19 +02:00
Ran Benita 35a3863b15 config: clarify a bit of code in `_importconftest` 2024-01-09 23:49:03 +02:00
Ran Benita 368fa2c03e fixtures: remove unhelpful FixtureManager.{FixtureLookupError,FixtureLookupErrorRepr}
Couldn't find any reason for this indirection, nor any plugins which
rely on it. Seems like historically it was done to avoid some imports...
2024-01-09 23:33:07 +02:00
Ran Benita 372c17e228 fixtures: avoid FixtureDef <-> FixtureManager reference cycle
There is no need to store the FixtureManager on each FixtureDef.
2024-01-09 23:29:58 +02:00
Ran Benita 97dfc3429e
Merge pull request #11785 from bluetech/matchfactories-nodes
fixtures: match fixtures based on actual node hierarchy, not textual nodeids
2024-01-08 22:23:08 +02:00
Ran Benita 992d0f082f fixtures: match fixtures based on actual node hierarchy, not textual nodeids
Refs #11662.

--- Problem

Each fixture definition has a "visibility", the `FixtureDef.baseid`
attribute. This is nodeid-like string. When a certain `node` requests a
certain fixture name, we match node's nodeid against the fixture
definitions with this name.

The matching currently happens on the *textual* representation of the
nodeid - we split `node.nodeid` to its "parent nodeids" and then check
if the fixture's `baseid` is in there.

While this has worked so far, we really should try to avoid textual
manipulation of nodeids as much as possible. It has also caused problem
in an odd case of a `Package` in the root directory: the `Package` gets
nodeid `.`, while a `Module` in it gets nodeid `test_module.py`. And
textually, `.` is not a parent of `test_module.py`.

--- Solution

Avoid this entirely by just checking the node hierarchy itself. This is
made possible by the fact that we now have proper `Directory` nodes
(`Dir` or `Package`) for the entire hierarchy.

Also do the same for `_getautousenames` which is a similar deal.

The `iterparentnodeids` function is no longer used and is removed.
2024-01-08 21:36:51 +02:00
Ran Benita c8792bd080 python,unittest: replace obj fixture patching with `FixtureManager._register_fixture`
Instead of modifying user objects like modules and classes that we
really shouldn't be touching, use the new `_register_fixture` internal
API to do it directly.
2024-01-08 21:02:59 +02:00
Ran Benita 3234c79ee5 fixtures: add an internal API for registering a fixture
Add a function on the `FixtureManager` to register a fixture with
pytest. Currently this can only be done through `parsefactories`.

My aim is to eventually make something like this available to plugins,
as it's a pretty common need.
2024-01-08 21:02:59 +02:00
Ran Benita 851b72f289
Merge pull request #11776 from bluetech/unittest-xunit-inline
unittest: inline `_make_xunit_fixture`
2024-01-08 21:02:24 +02:00
Ran Benita 1d7349d18c
Merge pull request #11774 from bluetech/fspath-cleanups
Small `fspath` cleanups
2024-01-08 21:01:56 +02:00
Fabian Sturm 13eacdad8a
Add summary for xfails with -rxX option (#11574)
Co-authored-by: Brian Okken <1568356+okken@users.noreply.github.com>
2024-01-05 09:59:19 -03:00
Ran Benita a616adf3ae unittest: inline `_make_xunit_fixture`
The indirection makes things harder to follow in this case IMO.
2024-01-05 14:37:03 +02:00
Ran Benita 685e52ec30 nodes: fix attribute name `fspath` -> `path` in `get_fslocation_from_item` 2024-01-04 22:32:34 +02:00
Ran Benita 7b4ab8134e fixtures: remove unnecessary `fspath` workaround 2024-01-04 22:32:34 +02:00
Clément Robert ac96256272 Fix a mistake in pytest.warns' docstring (expect_warning accepts tuples, not any sequence) 2024-01-04 11:51:12 +01:00
Ben Brown 12b9bd5801
Fix teardown error reporting when `--maxfail=1` (#11721)
Co-authored-by: Ran Benita <ran@unusedvar.com>
2024-01-03 19:39:24 +02:00
Ran Benita f017df443a
Merge pull request #11757 from bluetech/rm-removed-in-8
Remove pytest 8 deprecations
2024-01-03 16:53:10 +02:00
Ran Benita 215f4d1fab Remove `PytestRemovedIn8Warning`
Per our deprecation policy.
2024-01-03 14:29:45 +02:00
Ran Benita 6c89f9261c Remove deprecated py.path (`fspath`) node constructor arguments 2024-01-03 14:29:45 +02:00
Ran Benita a98f02d423 Remove deprecated py.path hook arguments 2024-01-03 14:29:42 +02:00
Marc Bresson effc2b0529
Clarified `markers` ini property. Fix #11738 (#11739) 2024-01-03 14:20:54 +02:00
Ran Benita cb5a42c836 terminalwriter: fix crash trying to highlight empty source
For quick checking I don't know how we can reach here with an empty
source, so test just checks the function directly.

Fix #11758.
2024-01-02 19:37:24 +02:00
Ran Benita 0f18a7fe5e Remove deprecated nose support 2024-01-02 12:20:47 +02:00
Ran Benita 0591569b4b Remove deprecated pytest.{exit,fail,skip}(msg=...) argument 2024-01-02 12:20:47 +02:00
Ran Benita 477959ef7d Remove deprecated `pytest.Instance` backward compat 2024-01-02 12:20:47 +02:00
Ran Benita 4147c92b21 Remove deprecated `pytest.warns(None)` 2024-01-02 12:20:47 +02:00
Ran Benita 10fbb2325f Remove deprecated `Parser.addoption` backward compatibilities 2024-01-02 12:20:47 +02:00
Ran Benita 1f8b39ed32 Remove deprecated `--strict` option 2024-01-02 12:20:47 +02:00
Ran Benita f4e7b0d6e0 Remove deprecated `pytest_cmdline_preparse` hook 2024-01-02 12:20:47 +02:00
Ran Benita f13724e2e3 Remove deprecated {FSCollector,Package}.{gethookproxy,isinitpath} 2024-01-02 12:20:47 +02:00
Ran Benita d220880924
nodes: fix tracebacks from collection errors are not getting pruned (#11711)
Fix #11710.
2023-12-31 10:14:23 +02:00
Ran Benita a71a95b54c Change "Marks applied to fixtures" removal from 8 to 9
The deprecation has only been added in 8.0, so can't be removed in 8.
That will have to wait for 9.
2023-12-30 22:14:40 +02:00
Ran Benita acd445a3f3
Merge pull request #11646 from bluetech/pkg-collect
Rework Session and Package collection
2023-12-30 12:51:48 +02:00
Michał Górny 52db918a27
Fix handling empty values of NO_COLOR and FORCE_COLOR (#11712)
* Fix handling empty values of NO_COLOR and FORCE_COLOR

Fix handling NO_COLOR and FORCE_COLOR environment variables to correctly
be ignored when they are set to an empty value, as defined
in the specification:

> Command-line software which adds ANSI color to its output by default
> should check for a NO_COLOR environment variable that, when present
> *and not an empty string* (regardless of its value), prevents
> the addition of ANSI color.

(emphasis mine, https://no-color.org/)

The same is true of FORCE_COLOR, https://force-color.org/.

* Streamline testing for FORCE_COLOR and NO_COLOR

Streamline the tests for FORCE_COLOR and NO_COLOR variables, and cover
all possible cases (unset, set to empty, set to "1").  Combine the two
assert functions into one taking boolean parameters.  Mock file.isatty
in all circumstances to ensure that the environment variables take
precedence over the fallback value resulting from isatty check (or that
the fallback is actually used, in the case of both FORCE_COLOR
and NO_COLOR being unset).
2023-12-23 10:12:13 +02:00
Ran Benita 54a0ee02ea
Merge pull request #11732 from bluetech/typing-tweaks
Some minor typing tweaks
2023-12-22 13:00:52 +02:00
Benjamin Schubert 88ae27da08
Add syntactic highlights to the error explanations (#11661)
* Put a 'reset' color in front of the highlighting

When doing the highlighting, some lexers will not set the initial color
explicitly, which may lead to the red from the errors being propagated
to the start of the expression

* Add syntactic highlighting to the error explanations

This updates the various error reporting to highlight python code when
displayed, to increase readability and make it easier to understand
2023-12-21 17:11:56 +00:00
Ran Benita 75f292d9df Some minor typing tweaks 2023-12-19 23:29:27 +02:00
Benjamin Schubert 283a746dad pprint: Remove conversion to int, we only accept those 2023-12-18 08:35:57 +00:00
Benjamin Schubert 64b5b665cf pprint: Remove the option to add underscore for numbers
This is never used, we can remove this. If we wanted instead, we could
always enable it
2023-12-18 08:35:57 +00:00
Benjamin Schubert 6aa35f772f pprint: Remove the option to sort dictionaries, we always do it 2023-12-18 08:35:57 +00:00
Benjamin Schubert 03b24e5b30 pprint: Remove the `format` method, it's not used outside of pprint
Let's reduce the API surface for the bundled PrettyPrinter to what we
really need and use
2023-12-18 08:35:57 +00:00
Arthur Richard 047ba83dab
Improve pytest.exit docs (#11698)
Fixes #11695
2023-12-14 08:14:36 -03:00
Ran Benita e1c66ab0ad Different fix for conftest loading
--- Current main

In current main (before pervious commit), calls to gethookproxy/ihook
are the trigger for loading non-initial conftests. This basically means
that conftest loading is done almost as a random side-effect,
uncontrolled and very non-obvious. And it also dashes any hope of making
gethookproxy faster (gethookproxy shows up prominently in pytest
profiles).

I've wanted to improve this for a while, #11268 was the latest step
towards that.

--- PR before change

In this PR, I ran into a problem.

Previously, Session and Package did all of the directory traversals
inside of their collect, which loaded the conftests as a side effect. If
the conftest loading failed, it will occur inside of the collect() and
cause it to be reported as a failure.

Now I've changed things such that Session.collect and Package.collect no
longer recurse, but just collect their immediate descendants, and
genitems does the recursive expansion work.

The problem though is that genitems() doesn't run inside of specific
collector's collect context. So when it loads a conftest, and the
conftest loading fails, the exception isn't handled by any CollectReport
and causes an internal error instead.

The way I've fixed this problem is by loading the conftests eagerly in a
pytest_collect_directory post-wrapper, but only during genitems to make
sure the directory is actually selected.

This solution in turn caused the conftests to be collected too early;
specifically, the plugins are loaded during the parent's collect(), one
after the other as the directory entries are collected. So when the
ihook is hoisted out of the loop, new plugins are loaded inside the
loop, and due to the way the hook proxy works, they are added to the
ihook even though they're supposed to be scoped to the child collectors.
So no hoisting.

--- PR after change

Now I've come up with a better solution: since now the collection tree
actually reflects the filesystem tree, what we really want is to load
the conftest of a directory right before we run its collect(). A
conftest can affect a directory's collect() (e.g. with a
pytest_ignore_collect hookimpl), but it cannot affect how the directory
node itself is collected. So I just moved the conftest loading to be
done right before calling collect, but still inside the CollectReport
context.

This allows the hoisting, and also removes conftest loading from
gethookproxy since it's no longer necessary. And it will probably enable
further cleanups. So I'm happy with it.
2023-12-10 17:01:39 +02:00
Ran Benita 385796ba49 Rework Session and Package collection
Fix #7777.
2023-12-10 17:01:39 +02:00
Ran Benita f411c8d6d7 main: add `with_parents` parameter to `isinitpath`
Will be used in upcoming commit.
2023-12-10 16:32:54 +02:00
Ran Benita c1339628d6
Merge pull request #11684 from bluetech/docs-nitpicky
docs: enable Sphinx nitpicky mode
2023-12-10 09:42:30 +02:00
Ran Benita 397769c45e
Merge pull request #11677 from bluetech/nodes-abc
nodes,python: mark abstract node classes as ABCs
2023-12-10 09:41:46 +02:00
Ran Benita 0ae02e2165 nodes,python: mark abstract node classes as ABCs
Fixes #11676
2023-12-07 16:41:07 +02:00
Ran Benita ee91d095f6 doc: fix more broken Sphinx references 2023-12-07 11:50:02 +02:00
Ran Benita 4918883336 python_api: remove unused function `_non_numeric_type_error` 2023-12-07 11:03:52 +02:00
Ran Benita d1675646f2
Merge pull request #11678 from pytest-dev/doc-nitpicks
doc: fix some broken Sphinx references
2023-12-07 09:40:52 +02:00
Ran Benita 9056db4de5 doc: fix some broken Sphinx references 2023-12-07 00:30:35 +02:00
Benjamin Schubert a536f49d91
Separate the various parts of the error report with newlines (#11659)
Previously the error report would have all sections glued together:

- The assertion representation
- The error explanation
- The full diff

This makes it hard to see at a glance where which one starts and ends.

One of the representation (dataclasses, tuples, attrs) does display a
newlines at the start already.

Let's add a newlines before the error explanation and before the full
diff, so we get an easier to read report.

This has one disadvantage: we get one line less in the least verbose
mode, where the output gets truncated.
2023-12-06 09:25:00 +00:00
Ran Benita ad1bccdead pytester: avoid EncodingWarning from `locale.getpreferredencoding`
When running `tox -e py-lsof` I get a deluge of this warning:

```
src/pytest/.tox/py-lsof-numpy-pexpect/lib/python3.11/site-packages/_pytest/pytester.py:130: EncodingWarning: UTF-8 Mode affects locale.getpreferredencoding(). Consider locale.getencoding() instead.
```

Use `locale.getencoding` instead.
2023-11-28 17:40:58 +02:00
Benjamin Schubert 88c3546006 pprint: use a set instead of a dict for the context
This is really what the context is doing, we don't need to use a dict
for it
2023-11-27 22:40:09 +00:00
Benjamin Schubert 50607297f4 pprint: Remove tracking of whether an object is readable
This information is not used anywhere
2023-11-27 22:40:09 +00:00
Benjamin Schubert 767f08cecd pprint: Remove tracking of whether the object is recursive
This information is not used anywhere, we can simplify by just not
tracking it
2023-11-27 22:40:09 +00:00
Benjamin Schubert e5a448cd5f pprint: Type annotate the module
This will make it easier to refactor
2023-11-27 22:40:09 +00:00
Benjamin Schubert 64e72b79f6 pprint: Remove unused arguments on PrettyPrinter 2023-11-27 22:40:09 +00:00
Benjamin Schubert 2d1710e0e9
Improve the full diff by having more consistent indentation in the PrettyPrinter (#11571)
The normal default pretty printer is not great when objects are nested
and it can get hard to read the diff.

Instead, provide a pretty printer that behaves more like when json get
indented, which allows for smaller, more meaningful differences, at
the expense of a slightly longer diff.

This does not touch the other places where the pretty printer is used,
and only updated the full diff one.
2023-11-27 16:47:18 +02:00
Jens Tröger acab13fcc9
Add new filtering() method to LogCaptureFixture class (#11625)
Fixes #11610
2023-11-24 09:38:34 -03:00
Simon Blanchard a42530a09d
Fix for operation on closed file in faulthandler teardown (#11584) 2023-11-22 22:05:00 +02:00
Benjamin Schubert 19934b2b0c Merge the AlwaysDispathPrettyPrinter into the now vendored PrettyPrinter
We don't need to keep the separation anymore, and this will make it
easier to extend
2023-11-20 13:00:00 +00:00
Benjamin Schubert 2953120003 Fix typing information for the pprint module
There is more type information that could be added. We can add those
later to make it easier, this is jsut the minimum to allow linting to
pass
2023-11-20 13:00:00 +00:00
Benjamin Schubert 5fae5ef73e Apply project-wide formatting standard to the pprint module (black) 2023-11-20 13:00:00 +00:00
Benjamin Schubert 66f2f20eff Run pyupgrade on the pprint module 2023-11-20 13:00:00 +00:00
Benjamin Schubert 2322668344 Remove unneeded pprint interfaces
There are parts of the original pprint module that we won't need, let's
limit the surface and remove the unnecessary code
2023-11-20 13:00:00 +00:00
Benjamin Schubert eb6ad08e5d Vendor in the pprint module to allow further modifications
We already have the AlwaysDispatchingPrettyPrinter override of the
default pretty printer. In order to make more in depth changes, we
need to copy the upstream version in, as it doesn't lend itself well to
being extended.

This does a verbatime copy, adding provenance information at the top.
2023-11-20 13:00:00 +00:00
Patrick Lannigan 9dc1fc4523
Add verbosity_assertions and config.get_verbosity
Fixes #11387
2023-11-19 11:56:29 -03:00
Avasam 80442ae2f2
Use `False`, instead of `None` as default for `_XfailMarkDecorator`'s `condition` param and update doc (#11600) 2023-11-17 10:42:05 +02:00
Bruno Oliveira 970d11cf34 Explicitly export error and path from py
Required by mypy 1.7.0
2023-11-14 07:29:40 -03:00
Sharad Nair 7c7bdf4574
Sanitize ini-options default handling #11282 (#11594)
Fixes #11282
2023-11-11 13:08:18 -03:00
Bruno Oliveira 1e02797d15
Improve pytest.raises docs (#11578) 2023-11-04 10:24:57 +00:00
Bruno Oliveira 7156a97f9a Fix crash using empty string for parametrized value more than once
Fixes #11563.
2023-10-27 17:26:12 -03:00
Benjamin Schubert fbe3e29a55
Color the full diff that pytest shows as a diff (#11530)
Related to #11520
2023-10-24 08:42:21 -03:00
pre-commit-ci[bot] 40e9abd66b
[pre-commit.ci] pre-commit autoupdate (#11510)
* [pre-commit.ci] pre-commit autoupdate

updates:
- [github.com/pre-commit/mirrors-mypy: v1.5.1 → v1.6.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.5.1...v1.6.0)

* Ignore two typing errors after updating to mypy 1.6.0

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Bruno Oliveira <bruno@soliv.dev>
2023-10-23 08:05:40 -03:00
ryanpudd 3ab70cd561
Use hyphenated cmdline options in docs (#11490)
Fix #11091
2023-10-10 21:16:24 +00:00
Tanya Agarwal af9b1dcc24
Duplicated parameters in parametrize marker (#11489)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Zac Hatfield-Dodds <zac.hatfield.dodds@gmail.com>
2023-10-08 10:26:31 -07:00
Reagan Lee 9bbfe995ee
Add more comprehensive set assertion rewrites (#11469)
Fixes #10617
2023-10-02 18:37:52 -03:00
Ran Benita 81192ca85f pytester: use monkeypatch.chdir() for dir changing
The current method as the following problem, described by Sadra
Barikbin:

The tests that request both `pytester` and `monkeypatch` and use
`monkeypatch.chdir` without context, relying on `monkeypatch`'s teardown
to restore cwd. This doesn't work because the following sequence of
actions take place:

- `monkeypatch` is set up.
- `pytester` is set up. It saves the original cwd and changes it to a
  new one dedicated to the test function.
- Test function calls `monkeypatch.chdir()` without context.
  `monkeypatch` saves cwd, which is not the original one, before
  changing it.
- `pytester` is torn down. It restores the cwd to the original one.
- `monkeypatch` is torn down. It restores cwd to what it has saved.

The solution here is to have pytester use `monkeypatch.chdir()` itself,
then everything is handled correctly.
2023-09-25 11:31:09 +03:00
Ran Benita 4ae102c003
Merge pull request #11446 from bluetech/pluggy-typing
Improve pluggy-related typing
2023-09-24 15:42:58 +03:00
Warren Markham d2b214220f
Add class docstring to NodeMeta (#11427) 2023-09-20 09:08:07 -03:00
Simon Blanchard a38ad254ef
Handle ValueError raised during faulthandler teardown code (#11453)
Fixes #11439
2023-09-20 09:06:43 -03:00
Sharad Nair 9a58e6283d
Fixes issue #11314 - log_file_format does not default to log_format (#11444)
* Fixes issue #11314 -

* Incorporated review comments for issue #11314

* Update changelog/11314.improvement.rst

Co-authored-by: Bruno Oliveira <bruno@soliv.dev>

---------

Co-authored-by: Zac Hatfield-Dodds <zac.hatfield.dodds@gmail.com>
Co-authored-by: Bruno Oliveira <bruno@soliv.dev>
2023-09-18 16:50:04 +00:00
Chris Mahoney 8062743f6b
Change deprecated_call to handle FutureWarning (#11448)
Fixes #11447
2023-09-18 09:34:05 -03:00
Mihail Milushev a47fcb4873 code review: kwarg-only `match`, replace `recursive` with `depth` 2023-09-17 22:28:32 +01:00
Mihail Milushev ab8f5ce7f4 Add new `ExceptionInfo.group_contains` assertion helper method
Tests if a captured exception group contains an expected exception.
Will raise `AssertionError` if the wrapped exception is not an exception group.
Supports recursive search into nested exception groups.
2023-09-17 22:28:32 +01:00
Ran Benita f43a8db618 Improve pluggy-related typing 2023-09-17 21:32:55 +03:00
Bruno Oliveira e2acc1a99b
Fix --import-mode=importlib when root contains `__init__.py` file (#11420)
We cannot have an empty module name when importing a `__init__.py` file that
is at the rootdir.

Fixes #11417
2023-09-10 09:57:40 -03:00
Warren Markham 71f265f1f3
Refactor: use division operator to join paths (#11413)
Starting with `resolve_package_path` and its associated tests,
this refactoring seeks to make path concatenation more
readable and consistent within tests/functions.

As discussed in #11413:

- code is free to use either `/` and `joinpath`
- consistency within a function is more important than consistency across the codebase
- it is nice to use `/` when it is more readable
- it is nice to use `joinpath` when there is little context
- be mindful that `joinpath` may be clearer when joining multiple segments
2023-09-09 09:16:22 -03:00
Marc Mueller 7259e8db98
Fix assert rewriting with assignment expressions (#11414)
Fixes #11239
2023-09-09 09:09:31 -03:00
Ran Benita 6ad9499c9c doctest: some missing type annotations 2023-09-08 15:59:17 +03:00
Ran Benita 2ed2e9208d doctest: remove unnecessary Optionals 2023-09-08 15:59:17 +03:00
Ran Benita ab63ebb3dc doctest: inline `_setup_fixtures`, make more similar to `Function`
There used to be two callers to `_setup_fixtures()`, now there's only
one, so inline it and make `DoctestItem` more similar to `Function`.

(Eventually we may want to generalize `TopRequest` from taking
`Function` directly to some "fixture-supporting item", removing the
remaining `type: ignore` here and allowing plugins to do it in a stable
manner).
2023-09-08 15:59:17 +03:00
Ran Benita b3a981d385 fixtures: remove `getfixtureinfo(funcargs)` in favor of None `func`
Since we already broke plugins using this (private) interface in this
version (pytest-play, pytest-wdl), might as well do a cleanup.
2023-09-08 15:59:17 +03:00
Sadra Barikbin 48b0395648 fixtures: clean up getfixtureclosure()
Some code cleanups - no functional changes.
2023-09-08 15:53:49 +03:00
Ran Benita 9c11275553 fixtures: change getfixtureclosure(ignore_args) to a set
Only used for containment checks so a Set is more appropriate than a
list.
2023-09-08 15:53:49 +03:00
Bruno Oliveira 5936a79fdb
Use _pytest.pathlib.safe_exists in get_dirs_from_args (#11407)
Related to #11394
2023-09-07 15:44:47 -03:00
Bruno Oliveira 28ccf476b9
Fix crash when passing a very long cmdline argument (#11404)
Fixes #11394
2023-09-07 12:49:25 -03:00
Fraser Stark 333e4eba6b
Change PytestReturnNotNoneWarning to return a normal warning (#11211)
Fixes #10465
2023-09-07 15:11:59 +00:00
Ronny Pfannschmidt e787d2ed48
Merge pull request #11317 from tjsmart/fix-issue-11237
Fix doctest collection of `functools.cached_property` objects.
2023-09-07 14:39:02 +02:00
Ran Benita faa8f2ea08
Merge pull request #11393 from pytest-dev/fixtures-tweaks
Fixtures tweaks
2023-09-06 09:15:28 +03:00
Bruno Oliveira 194a782e38
Fix import_path for packages (#11390)
For packages, `import_path` receives the path to the package's `__init__.py` file, however module names (as they live in `sys.modules`) should not include the `__init__` part.

For example, `app/core/__init__.py` should be imported as `app.core`, not as `app.core.__init__`.

Fix #11306
2023-09-05 19:42:40 -03:00
Ran Benita bc71561ad9 python: avoid an Any 2023-09-05 22:32:46 +03:00
Ran Benita 574e0f45d9 fixtures: avoid using the mildly expensive `fixturenames` property
Avoid creating a list copy + 2 sets + a linear search through the list
(in the common case).
2023-09-05 22:15:37 +03:00
Ran Benita b8906b29a7 fixtures: require `item.fixturenames` to exist in `_fillfixtures`
I could find 2 plugins that would be broken by this (pytest-play and
pytest-wdl), but they will be better served by just copying
`_fillfixtures` instead of use the private function.
2023-09-05 22:15:37 +03:00
Ran Benita d2b5177dd6 fixtures: avoid some redundant work in `_fillfixtures` 2023-09-05 22:15:37 +03:00
Ran Benita 65c01f531b fixtures: use the item `fixturenames` in `request.fixturenames`
`_pyfuncitem.fixturenames` is just an alias for
`_pyfuncitem._fixtureinfo.names_closure` (at least in core pytest), so
let's do the less abstraction-breaking thing.
2023-09-05 22:15:37 +03:00
Ran Benita 82bd63d318 doctest: add `fixturenames` field to `DoctestItem`
The field is used in `_fillfixtures`, in preference to
`request.fixturenames`, which also includes already-computed which is
not needed.
2023-09-05 22:15:37 +03:00
Ran Benita d4872f5df7 fixtures: tiny code cleanup 2023-09-05 22:15:37 +03:00
Ran Benita 8d815ca55b python: type some CallSpec2 fields as immutable
Knowing that a field is immutable makes it easier to understand the
code.
2023-09-05 22:15:37 +03:00
Israel Fruchter 917ce9aa01
Fix user_properties not saved to XML if fixture errors during teardown
Move handling of user_properties to `finalize()`.

Previously if a fixture failed during teardown, `pytest_runtest_logreport` would not be called with "teardown", resulting in the user properties not being saved on the JUnit XML file.

Fixes: #11367
2023-09-03 14:33:54 -03:00
Sean Patrick Malloy 76ba7db6ce
Improve docs for last-failed-no-failures
Improve documentation for last-failed-no-failures to make the different options and the functionality more clear.

Closes #11354
2023-08-29 00:14:45 +00:00
Warren Markham ff23347f1f
Fix platform-dependent type-check errors (#11345)
Use more explicit `sys.platform` checks, instead of the previous check using `in`, which mypy understands.

Fixes #11343
2023-08-27 09:40:24 -03:00
Ran Benita 77f7f59b2a Fixes for typed pluggy
Since version 1.3 pluggy added typing, which requires some fixes to
please mypy.
2023-08-26 22:13:24 +03:00
Jon Parise 7500fe44b2
Correct the spelling of ArgSource.INVOCATION_DIR (#11333)
Config.ArgsSource.INCOVATION_DIR remains as a backwards compatibility
alias.
2023-08-23 09:21:17 +00:00
Ran Benita 43d1398fc7
Merge pull request #11334 from WarrenTheRabbit/fix-docstring-spelling-in-pytester-1
doc: fix docstring spelling in pytester
2023-08-22 14:44:06 +03:00
Zac Hatfield-Dodds 03832fa31e
Merge pull request #11326 from DetachHead/DetachHead-patch-1
use `if not TYPE_CHECKING` on `pytest.__getattr__` to prevent type checkers from using it
2023-08-22 03:53:46 -07:00
WarrenTheRabbit 3f446b68fd
doc: fix docstring spelling in pytester 2023-08-22 14:36:39 +10:00
Zac Hatfield-Dodds 050f402816
Merge pull request #11308 from reaganjlee/iter-reporting
Improve reporting from __iter__ exceptions
2023-08-21 16:27:42 -07:00
pre-commit-ci[bot] dd5ae0c3b8 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2023-08-21 08:03:06 +00:00
DetachHead fc653d0d12
use `if not TYPE_CHECKING` on `pytest.__getattr__` to prevent type checkers from using it 2023-08-21 18:00:28 +10:00
Tyler Smart a357c7abc8 Ignore dip in branch coverage (since py3.13+ isn't tested in CI) 2023-08-20 20:55:30 -06:00
Reagan Lee 61133ba83d un-iterable fix 2023-08-20 14:46:09 -07:00
Reagan Lee 049eec8474 Revert iter raises checks
This reverts commit e938580257.

Revert "improve error msg and test"

This reverts commit c0cf822ca1.

Revert "error msg"

This reverts commit ec1053cc16.

Revert "changelog"

This reverts commit d2dc8a70b5.

Revert "simplify code / take out user-gen typeerror case"

This reverts commit b9cb87d862.
2023-08-20 14:06:42 -07:00
Tyler Smart 7a625481da PR suggestions 2023-08-19 22:20:40 -06:00
Tyler Smart ebd571bb18 Move _from_module override to pre-existsing DocTestFinder subclass 2023-08-19 12:04:59 -06:00
Tyler Smart d4fb6ac9f7 Fix doctest collection of `functools.cached_property` objects. 2023-08-16 00:55:16 -06:00
Ran Benita 15fadd8c5c
Merge pull request #11219 from bluetech/fixtures2
fixtures: make FixtureRequest abstract, add TopRequest subclass
2023-08-15 21:52:46 +03:00
Ran Benita 1827d8d5f9 doc: fix a few `pytest.mark.xfail` nits
Refs #9027, #10094.
2023-08-12 18:30:58 +03:00
Ran Benita 9e164fc4fe fixtures: make FixtureRequest abstract, add TopRequest subclass
Fix #11218.
2023-08-12 11:55:08 +03:00
Reagan Lee c0cf822ca1 improve error msg and test 2023-08-11 10:33:34 -07:00
Reagan Lee ec1053cc16 error msg 2023-08-11 10:03:19 -07:00
Reagan Lee b9cb87d862 simplify code / take out user-gen typeerror case 2023-08-10 17:43:01 -07:00
Reagan Lee e938580257 check for user-generated exceptions 2023-08-10 17:37:48 -07:00
Ran Benita 3ad3fc6b8f python: use clearer terminology for `_resolve_arg_value_types` 2023-08-09 20:36:24 +03:00
Sadra Barikbin 09b78737a5
Move `fixtures.py::add_funcarg_pseudo_fixture_def` to `Metafunc.parametrize` (#11220)
To remove fixtures.py::add_funcargs_pseudo_fixture_def and add its logic
i.e. registering funcargs as params and making corresponding fixturedefs,
right to Metafunc.parametrize in which parametrization takes place.

To remove funcargs from metafunc attributes as we populate metafunc
params and make pseudo fixturedefs simultaneously and there's no need to
keep funcargs separately.
2023-08-09 17:13:45 +00:00
Ran Benita b2186e2455
Merge pull request #11268 from bluetech/conftest-load
config: split `_getconftestmodules` and `_loadconftestmodules`
2023-08-08 14:44:29 +03:00
Bruno Oliveira 24c9aa6c30
Merge pull request #11291 from calestyo/improve-duplicate-values-documentation
Improve duplicate values documentation
2023-08-07 08:14:39 -03:00
Christoph Anton Mitterer 1cc58ed67f improve exception message on duplicate parametrization
Signed-off-by: Christoph Anton Mitterer <mail@christoph.anton.mitterer.name>
2023-08-06 17:43:37 +02:00
Christoph Anton Mitterer 84a342e27c doc: parametrize() can be called multiple times only on different args
Signed-off-by: Christoph Anton Mitterer <mail@christoph.anton.mitterer.name>
2023-08-06 17:39:31 +02:00
Sadra Barikbin e8a8a5f320
python: fix scope assignment for indirect parameter sets (#11277)
Previously, when assigning a scope for a fully-indirect parameter set,
when there are multiple fixturedefs for a param (i.e. same-name fixture
chain), the highest scope was used, but it should be the lowest scope,
since that's the effective scope of the fixture.
2023-08-06 13:59:54 +00:00
Sadra Barikbin 4797deab99
Add `FixtureArgKey` class to represent fixture deps in `fixtures.py` (#11231) 2023-08-02 14:28:31 +03:00
pre-commit-ci[bot] c9163402e0
[pre-commit.ci] pre-commit autoupdate (#11269)
* [pre-commit.ci] pre-commit autoupdate

updates:
- [github.com/PyCQA/flake8: 6.0.0 → 6.1.0](https://github.com/PyCQA/flake8/compare/6.0.0...6.1.0)
- [github.com/asottile/pyupgrade: v3.9.0 → v3.10.1](https://github.com/asottile/pyupgrade/compare/v3.9.0...v3.10.1)

* Use is instead of type comparison with equal to appease the linter

---------

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>
2023-08-01 23:52:09 +02:00
Ran Benita 01ac13a77d config: split _getconftestmodules and _loadconftestmodules
Previously, the `_getconftestmodules` function was used both to load
conftest modules for a path (during `pytest_load_initial_conftests`),
and to retrieve conftest modules for a path (during hook dispatch and
for fetching `collect_ignore`). This made things muddy - it is usually
nicer to have clear separation between "command" and "query" functions,
when they occur in separate phases.

So split into "load" and "get".

Currently, `gethookproxy` still loads conftest itself. I hope to change
this in the future.
2023-08-01 09:46:17 +03:00
Ran Benita a21fb87a90 python: change `Package` to no longer be a `Module`/`File`
Fix #11137.
2023-07-28 22:49:24 +03:00
Ran Benita 71e627aa8f fixtures: fix crash when `parametrize(scope="package")` is used without a Package
There as handling for `scope="class"` without a class, but not for
`scope="package"` without a package. It would fail the assert.
2023-07-28 16:13:02 +03:00
Sadra Barikbin e8aa906e06 fixtures: move _get_direct_parametrize_args to a standalone function
So it can be used independently of the FixtureManager.
2023-07-27 09:38:42 +03:00
Sadra Barikbin 12054a4972 config: avoid list[], set[], dict[]
Should wait with this until Python 3.8 is dropped.
2023-07-27 09:33:34 +03:00
Ran Benita 13e2b00258 config: don't pass the entire Config to `determine_setup()`
Seems better to make the function a bit more pure, and avoids the
circular import.
2023-07-24 00:00:34 +03:00
Ran Benita 4e42421ebf config: fix an incorrect type
Was a mistake in d97d44a97a.
2023-07-24 00:00:34 +03:00
bowugit 0b4a557087
Extend pytest.raises to support Exception having __repr__ method and initialized with kwargs (#11073)
Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
2023-07-20 13:29:29 -03:00
Isaac Virshup 1de0923e83
Have pytest.raises match against exception `__notes__` (#11227)
The doctest is skipped because add_note is only available in 3.11,

Closes #11223
2023-07-18 08:39:39 -03:00
Ran Benita 02ba39bfcd
Merge pull request #11215 from bluetech/fixtures-tweaks2
fixtures: minor tweaks
2023-07-17 17:30:24 +03:00
Ran Benita c5262b0c42 fixtures: show test as skip location if skipped from an xunit setup function
Fix #11216.
2023-07-16 23:26:04 +03:00
Ran Benita ff6e110161 fixtures: remove inaccurate comment
unittest TestCases can also use `usefixtures`. Just remove this comment,
it's not very helpful.
2023-07-16 23:19:33 +03:00
Ran Benita 0e0ed2af95 fixtures: some code comments and minor improvements 2023-07-16 23:19:33 +03:00
Ran Benita a3fbf24389 unittest: hide access to _request
Let the parent class (Function) do this. Tiny bit better abstraction.
There seems to be no reason for the `hasattr` bit.
2023-07-16 23:19:33 +03:00
Ran Benita 04e0db7e48 Use typing.Literal without TYPE_CHECKING checks
Literal was added in Python 3.8 which we now require so can use it
freely.
2023-07-16 01:11:21 +03:00
Ran Benita 32f480814c
Merge pull request #11209 from bluetech/fixtures-doc-comments
fixtures: some tweaks & improvements
2023-07-15 19:40:48 +03:00
Ran Benita 40ed678885 fixtures: remove two unused functions
Not used since 310b67b227.
2023-07-15 10:06:06 +03:00
Ran Benita fb55615d5e Export `FixtureDef`
FixtureDef is used in the `pytest_fixture_setup` hook so needs to be
public. However since its current internals are quite dubious (and not
all marked with `_` prefix) I've added an explicit note that only
documented fields/methods are considered public.

Refs #7469.
2023-07-15 10:06:06 +03:00
Ran Benita 9d0ddb4625 fixtures: change `FixtureDef.cached_result[2]` from exception triplet to exception
Fix #11208.
2023-07-15 10:06:06 +03:00
Ran Benita 01f38aca44 fixtures: expand comments and annotations on fixture internals 2023-07-15 10:06:06 +03:00
Ran Benita b41acaea12 Switch to new-style pluggy hook wrappers
Fix #11122.
2023-07-14 22:47:48 +03:00
Ran Benita ecfab4dc8b fixtures: fix a typing ignore TODO
From understanding the code better I see this is the correct fix.
The fixturedefs can be None if `request.getfixturevalue("doesnotexist")`
is used.

In practice there is no change in behavior because this mapping is used
as `self._arg2fixturedefs.get(argname, None)` which ends up the same.
2023-07-11 00:19:29 +03:00
Ran Benita 2c80de532f fixtures: replace a `startswith("conftest.py")` with `== "conftest.py"`
I can't imagine why we would want to test for a prefix here.
2023-07-10 23:27:11 +03:00
Ran Benita 7967b2e710 fixtures: change a lambda to partial
It makes for a more debuggable repr. Before:

    <function FixtureRequest._schedule_finalizers.<locals>.<lambda> at 0x7fe4ae32d440>

After:

    functools.partial(<bound method FixtureDef.finish of <FixtureDef argname='hello_package' scope='package' baseid=''>>, request=<SubRequest 'hello_package' for <Function test_hello>>)
2023-07-10 23:27:11 +03:00
Ran Benita f1c9570a0e A few more Python>=3.8 simplifications 2023-07-10 23:20:11 +03:00
antosikv 99ab8ae884
Clarify docs for pytest.main default behavior (#11187)
Add explicit mentions of the default behavior to both API reference and
how-to section about ways to invoke pytest.

Co-authored-by: Ran Benita <ran@unusedvar.com>
2023-07-09 12:30:33 -03:00
Bruno Oliveira fd7a4d2429
Merge pull request #11106 from bluetech/docs-ref-collection-objs
reference: improve the node types docs a bit
2023-07-08 15:41:22 -03:00
Kenny Y d4265448a5
Update src/_pytest/python_api.py
Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
2023-07-08 08:41:42 -04:00
Kenny Y c5b13099e6
Fix error assertion handling in approx when None in dict comparison
Dict comparsion in the ApproxMapping class did not check if values were None before attempting to subtract for max_abs_diff stat, which was throwing an TypeError instead of being handled by pytest error assertion. Check for None has been added before these calculations, so that None will properly show as Obtained/Expected in pytest assert message
2023-07-07 14:42:59 -04:00
Zac Hatfield-Dodds 084d756ae6
Merge pull request #11143 from tushar-deepsource/patch-1 2023-07-04 10:47:13 -07:00
Zac Hatfield-Dodds cb732f7f49
Merge branch 'main' into patch-1 2023-07-04 10:17:06 -07:00
Zac Hatfield-Dodds 7775e494b1 Further tweaks from code review 2023-07-04 10:00:29 -07:00
Lesnek c4876c7106 chore(CR): Add changes from code review 2023-07-04 11:49:11 +02:00
Lesnek 7b7bd304aa fix(warnings-recorder): Add handling of rest 2023-07-04 10:30:19 +02:00
Lesnek 8ac3c645fa fix(warnings-recorder): Match also subclass of warning in pop 2023-07-04 10:30:19 +02:00
Ronny Pfannschmidt 561f1a993b fix #10447 - consider marks in reverse mro order to give base classes priority 2023-07-01 22:27:18 +02:00
Bruno Oliveira b77d0deaf5
Fix duplicated imports with importlib mode and doctest-modules (#11148)
The initial implementation (in #7246) introduced the `importlib` mode, which
never added the imported module to `sys.modules`, so it included a test
to ensure calling `import_path` twice would yield different modules.

Not adding modules to `sys.modules` proved problematic, so we began to add the imported module to `sys.modules`
in #7870, but failed to realize that given we are now changing `sys.modules`, we might
as well avoid importing it more than once.

Then #10088 came along, passing `importlib` also when importing application modules
(as opposed to only test modules before), which caused problems due to imports
having side-effects and the expectation being that they are imported only once.

With this PR, `import_path` returns the module immediately if already in
`sys.modules`.

Fix #10811
Fix #10341
2023-07-01 15:37:46 +00:00
akhilramkee 2f7415cfbc
Add child modules as attributes of parent modules. (#10338)
Failing to add child modules as attributes of parent module will prevent them from being accessible through parent module.

Fix #10337
2023-07-01 15:12:41 +00:00
Cheukting 15524f34d2 capture warning when exception is raised (fix #9036) 2023-06-30 16:04:37 -07:00
Zac Hatfield-Dodds 2d48171e88 Tweak docs on review 2023-06-30 15:29:03 -07:00
Zac Hatfield-Dodds 7022fb455d Update tests for re-emitted warnings 2023-06-30 15:29:03 -07:00
Zac Hatfield-Dodds a1b37022af Refactor warns() exit logic 2023-06-30 15:29:02 -07:00
Reagan Lee 9279ea2882 Emit unmatched warnings from pytest.warns() 2023-06-30 15:29:02 -07:00
Zac Hatfield-Dodds f617bab0a2 Update pre-commit config 2023-06-30 15:07:07 -07:00
Zac Hatfield-Dodds f4e3b4ad98 Drop Python 3.7 2023-06-30 14:55:42 -07:00
Tushar Sadhwani 4ebb2b94c2
fix code for py37 2023-06-26 12:19:42 +05:30
Tushar Sadhwani 9859c110cc
Ensure the docstring is a string 2023-06-26 12:14:41 +05:30
Thomas Grainger 45f1a462d5
Apply suggestions from code review
Co-authored-by: Zac Hatfield-Dodds <zac.hatfield.dodds@gmail.com>
2023-06-25 16:08:53 +01:00
Thomas Grainger 4cd95eeabf
Merge branch 'main' into warn-when-a-mark-is-applied-to-a-fixture 2023-06-25 16:08:11 +01:00
Ran Benita c8b1790ee7 python: change `pytest pkg/__init__.py` to only collect the `__init__.py` Module
Previously it would collect the entire package, but this is not what
users expect.

Refs #3749
Fixes #8976
Fixes #9263
Fixes #9313
2023-06-23 20:42:50 +03:00