Commit Graph

745 Commits

Author SHA1 Message Date
Ran Benita 9e164fc4fe fixtures: make FixtureRequest abstract, add TopRequest subclass
Fix #11218.
2023-08-12 11:55:08 +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
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
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 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
Ran Benita 396bfbf30b fixtures: add a test for a currently non-covered scope mismatch scenario
This test makes clear the need for the `_check_scope()` call in the
`pytest_setup_fixture` impl in fixtures.py, which otherwise seems
redundant with the one in `_compute_fixture_value`.
2023-07-18 00:01:40 +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 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
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 f4e3b4ad98 Drop Python 3.7 2023-06-30 14:55:42 -07: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
Zac Hatfield-Dodds 661b938fca Add encoding in more tests 2023-06-20 04:55:40 -07:00
nondescryptid a704605cf1 Fix encoding warnings 2023-06-20 04:55:39 -07:00
Ran Benita 4059000834 testing/python/collect: replace use of deprecated/removed `imp` module 2023-06-07 17:05:52 +03:00
Ran Benita cc23ec91d0 code: stop storing weakref to ExceptionInfo on Traceback and TracebackEntry
TracebackEntry needs the excinfo for the `__tracebackhide__ = callback`
functionality, where `callback` accepts the excinfo.

Currently it achieves this by storing a weakref to the excinfo which
created it. I think this is not great, mixing layers and bloating the
objects.

Instead, have `ishidden` (and transitively, `Traceback.filter()`) take
the excinfo as a parameter.
2023-04-28 11:47:45 +03:00
Jay ca40380e99
Add check for zero denominator in approx (#10624)
Closes #10533
2023-01-24 07:07:42 -03:00
q0w bd7919e03d
Initialize args and args_source during Config.__init__
Closes #10626

Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
2023-01-21 08:19:54 -03:00
Ran Benita 310b67b227
Drop attrs dependency, use dataclasses instead (#10669)
Since pytest now requires Python>=3.7, we can use the stdlib attrs
clone, dataclasses, instead of the OG package.

attrs is still somewhat nicer than dataclasses and has some extra
functionality, but for pytest usage there's not really a justification
IMO to impose the extra dependency on users when a standard alternative
exists.
2023-01-20 11:13:36 +02:00
Marko Pacak 9fbd67dd4b
Class methods can now be discovered as tests (#10552)
Fix #10525
2022-12-02 15:53:04 +00:00
Anthony Sottile 1b47de7d7f update does_not_raise docs now that pytest is 3.7+ only 2022-06-29 13:13:15 -04:00
Zach OBrien 96412d19ab
Fix representation of tuples in approx
Closes #9917
2022-06-14 06:54:32 -03:00
Zac Hatfield-Dodds 29462b1277 type-ignore in error-message test 2022-05-10 23:24:13 -07:00
Babak Keyvani 31a9c5c667 improve `pytest.raises` - cont'd
a few more iterations on error message and related tests.
2022-05-09 21:15:02 -04:00
Babak Keyvani 3444d35c54 improve `pytest.raises` 2022-04-30 17:56:36 -06:00
Ronny Pfannschmidt e9dd3dffab
Enhance errors for exception/warnings matching (#8508)
Co-authored-by: Florian Bruhin <me@the-compiler.org>
2022-03-20 23:32:39 -03:00
Kian Eliasi 3297bb24a9
Remove unnecessary numpy import (#9798)
Fix #9726
2022-03-21 00:01:59 +00:00
Bruno Oliveira 5f3d94c47e
Disallow unordered sequences in pytest.approx (#9709)
Fix #9692
2022-02-24 10:16:35 -03:00
Ran Benita c3aa4647c7 python: unify code to generate ID from value
In the following

    @pytest.mark.parametrize(..., ids=[val])

the ID values are only allowed to be `str`, `float`, `int` or `bool`.

In the following

    @pytest.mark.parametrize(..., [val])

    @pytest.mark.parametrize(..., [pytest.param(..., id=val])

a different code path is used, which also allows `bytes`, `complex`,
`re.Pattern`, `Enum` and anything with a `__name__`.

In the interest of consistency, use the latter code path for all cases.
2022-02-12 19:22:37 +02:00
pre-commit-ci[bot] 9d2ffe207b [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2022-01-31 22:20:16 +00:00
Tobias Deiminger b21b008118 Refactor idmaker functions into class IdMaker
This commit only refactors, it does not change or add functionality yet. Public
API is retained. Reason or refactoring:

User provided parameter IDs (e.g. Metafunc.parametrize(ids=...)) had so far
only been used to calculate a unique test ID for each test invocation. That
test ID was a joined string where each parameter contributed some partial ID.

We're soon going to reuse functionality to generate parameter keys for
reorder_items and FixtureDef cache. We will be interested in the partial
IDs, and only if they originate from explicit user information. Refactoring
makes logic and data accessible for reuse, and increases cohesion in general.
2022-01-26 15:42:34 +02:00
Hugo van Kemenade 61417b2551 Drop support for EOL Python 3.6 2021-12-30 13:25:34 +02:00
Ran Benita 0b0e2d2dbb Remove deprecated `_fillfuncargs` function 2021-12-07 22:27:35 +02:00
Bruno Oliveira a335ade1f5
Rename pathlib hook parameters (#9363)
* Rename pytest_ignore_collect fspath parameter to collection_path

* Rename pytest_collect_file fspath parameter to file_path

* Rename pytest_pycollect_makemodule fspath parameter to module_path

* Rename pytest_report_header startpath parameter to start_path

* Rename pytest_report_collectionfinish startpath parameter to start_path

* Update docs with the renamed parameters

* Use pytest-flakes fork temporarily to prove it works

* Use pytest-flakes 4.0.5
2021-12-03 13:14:09 +01:00
Ran Benita 062d91ab47
python: remove the `Instance` collector node 2021-11-13 14:03:44 +02:00
Florian Bruhin 5d87a27434
Elide pytest-internal paths for --fixtures (#9281)
* Elide pytest-internal paths for --fixtures

Fixes #8822

* Make flake8 happy
2021-11-10 15:16:41 +01:00
Ran Benita 7c0011374c Move FixtureRequest.fspath to legacypath plugin 2021-10-28 21:51:51 +03:00
Ran Benita 259cff59f5 Remove {pytester,testdir}.Session aliases
This causes `Session` documentation to be rendered again in full under
`pytester` and `testdir` in the API Reference. I tried but couldn't get
sphinx to hide it.

Since it's a pretty odd thing to have (should just use
`pytest.Session`), and I couldn't find any plugin which uses this, let's
just remove it.
2021-10-23 10:25:29 +03:00
Ran Benita 7eee5c1634 Change `Node.reportinfo()` return value from `py.path` to `str|os.PathLike[str]`
`reportinfo()` is the last remaining py.path-only code path in pytest,
i.e. the last piece holding back py.path deprecation. The problem with
it is that plugins/users use it from both sides -- implementing it
(returning the value) and using it (using the return value). Dealing
with implementers is easy enough -- allow to return `os.PathLike[str]`.
But for callers who expect strictly `py.path` this will break and
there's not really a good way to provide backward compat for this.

From analyzing a corpus of 680 pytest plugins, the vast majority of
`reportinfo` appearances are implementations, and the few callers don't
actually access the path part of the return tuple.

As for test suites that might access `reportinfo` (e.g. using
`request.node.reportinfo()` or other ways), that is much harder to
survey, but from the ones I searched, I only found case
(`pytest_teamcity`, but even then it uses `str(fspath)` so is unlikely
to be affected in practice). They are better served with using
`node.location` or `node.path` directly.

Therefore, just break it and change the return type to
`str|os.PathLike[str]`.

Refs #7259.
2021-10-09 15:02:03 +03:00
Ran Benita 5fc3e35afb
Merge pull request #9144 from bluetech/py36_order_by_definition
py36+ tests are definition ordered [v2]
2021-10-01 16:17:37 +03:00
Florian Bruhin 112204cf8d
Fix non-sensical error message (#9077)
* Fix non-sensical error message

Introduced in 12de92cd2b / #7698

* Add a test

* Put the unit back into unittest

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2021-09-24 06:15:53 +02:00
Kale Kundert af42e7154a
Prevent approx from being used without a comparison (#9061)
Some of the top search-engine hits for pytest.approx use the function without actually comparing it to anything.

This PR will cause these tests to fail by implementing approx.__bool__() to raise an AssertionError that briefly explains how to correctly use approx.
2021-08-30 18:19:31 +00:00
Bruno Oliveira a83b359cf0
Refactor internal scope handling by introducing Scope enum
PR #8913
2021-08-01 06:11:56 -03:00
Bruno Oliveira 2c4c57e135 Revert fspath deprecation
It is not clear yet how we should proceed with this deprecation
because `pytest.Item.reportinfo` is public API and returns a `py.path` object,
and is not clear how plugins and our examples should handle that.

Reverting just the deprecation aspect of #8251 so we can get a 7.0.0 release out.

We will reintroduce the deprecation later once we have a clear path moving forward with replacing `reportinfo`.

Closes #8445
Closes #8821
2021-07-13 08:24:39 -03:00
Graeme Smecher 5c04674e96 Generate useful parameterization IDs for complex() numbers. 2021-07-12 12:27:24 -07:00
Miro Hrončok 5165bf97c6
Revert "Adjust enum reprs for Python 3.10" (#8896)
This reverts commit 710446420c.

The change was reverted in Python 3.10.0b4:

https://mail.python.org/archives/list/python-dev@python.org/message/LSTMFAPSPD3BGZ4D6HQFODXZVB3PLYKF/
2021-07-12 14:32:27 +00:00
Rahul Kumaresan c516dba69a
add feature to view fixture source location in invocations with --fixtures-per-test option (#8626)
* add feature to view fixture source location in invocations with --fixtures-per-test option

* remove unrelated changes to show_fixtures_per_test::test_doctest_items

* eshew the extraneous else in _show_fixtures_per_test.write_fixture

* enable the accommodation of multi-line docstring with --fixtures-per-test option

* add feature to view fixture source location in invocations with --fixtures

* add colour encoding to fixture location paths

* add changelog for #8606 fixing

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2021-05-14 14:38:55 +02:00
Florian Bruhin e354c5c919 Fix warning filters used in tests 2021-05-04 17:25:02 +02:00
Florian Bruhin 710446420c Adjust enum reprs for Python 3.10
Potential fix for #8546
2021-05-04 17:24:50 +02:00