Commit Graph

2563 Commits

Author SHA1 Message Date
Ran Benita e515264eb1 Remove yet more unnecessary py.path uses 2021-03-18 10:23:48 +02:00
Ran Benita fe215bda6b
Merge pull request #8446 from bluetech/unnecessary-py-path-2
More py.path removal work
2021-03-18 10:20:59 +02:00
Kale Kundert dd3709718d
Merge pull request #8453 from encukou/rel-error-message
Fix value in error message about negative relative tolerance
2021-03-16 16:43:31 -04:00
Petr Viktorin 1f131afb07 Fix value in error message about negative relative tolerance 2021-03-16 18:54:52 +01:00
Ran Benita f0c7043138 Remove/replace some more unnecessary uses of py.path 2021-03-15 10:39:44 +02:00
Ran Benita a03ee02817 config: make `collect_ignore` accept any PathLike
The main reason is to remove a reference to `py.path`.
2021-03-15 10:39:32 +02:00
Ran Benita b26d1bb18f cacheprovider: add cache.mkdir() as a Path-returning replacement to makedir()
It is not possible to change a return type in a compatible way, so a new
method is added.
2021-03-15 10:39:31 +02:00
Ran Benita 59251e8a2a Remove/replace some unneeded usages of py.path 2021-03-14 14:12:28 +02:00
Ran Benita db539ed2b0
Merge pull request #8437 from bluetech/rm-typevar-prefix
Remove `_` prefix from TypeVars, expose ExceptionInfo
2021-03-13 19:01:29 +02:00
Pierre Mourlanne af9f27a874
Approx decimal sequence mapping (#8422) 2021-03-13 16:01:23 +02:00
Ran Benita f2d65c85f4 code: export ExceptionInfo for typing purposes
This type is most prominent in `pytest.raises` and we should allow to
refer to it by a public name.

The type is not in a perfectly "exposable" state. In particular:

- The `traceback` property with type `Traceback` which is derived from
  the `py.code` API and exposes a bunch more types transitively. This
  stuff is *not* exported and probably won't be.

- The `getrepr` method which probably should be private.

But they're already used in the wild so no point in just hiding them
now.

The __init__ API is hidden -- the public API for this are the `from_*`
classmethods.
2021-03-13 15:59:44 +02:00
Ran Benita 96ef7d678b code: fix outdated reference in ExceptionInfo.exconly docstring
No such thing as `_pytest._code.AssertionError`, it's just the built-in
`AssertionError`.
2021-03-13 14:44:50 +02:00
Ran Benita 5bbfb4e058 Remove `_` prefix from TypeVars which appear in public API
The prefixes make the API Reference docs (for e.g. `pytest.raises`,
`pytest.fixture`) uglier.

Being under `_pytest` is sufficient from a privacy perspective, so let's
drop them.
2021-03-13 14:44:50 +02:00
Ronny Pfannschmidt 77cb110258 drop usage of py.path.local calls
Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
2021-03-06 21:32:03 +01:00
Ronny Pfannschmidt 22dad53a24 implement Node.path as pathlib.Path
* reorganize lastfailed node sort

Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
2021-03-06 21:32:03 +01:00
Alexandros Tzannes 19a2f7425d
Merge pull request #8399 from atzannes/master
closes #8394

Generated fixture names for unittest/xunit/nose should start with underscore
2021-03-04 22:45:57 +02:00
Florian Bruhin c14a9adba3
Fix skip signature (#8392)
* Fix test_strict_and_skip

The `--strict` argument was removed in #2552, but the removal wasn't
actually correct - see #1472.

* Fix argument handling in pytest.mark.skip

See #8384

* Raise from None

* Fix test name
2021-03-04 11:56:21 +01:00
Simon K 22c0dace3b
change istestfunction to callable() (#8374) 2021-02-25 20:32:27 +00:00
Simon K b7f2d7ca61
Fixed an issue where `getpass.getuser()` contained illegal characters for file directories (#8365)
* retry writing pytest-of dir when invalid chars are in directory name

* add unit tests for getbasetemp() and changelog

* patch _basetemp & _given_basetemp for testing basetemp()

* Tweak changelog for #8317, tidy up comments
2021-02-25 08:28:57 +00:00
Florian Bruhin 54a154c86f Allow Class.from_parent to forward custom parameters to the constructor
Similarly to #7143, at work we have a project with a custom pytest.Class
subclass, adding an additional argument to the constructor.

All from_parent implementations in pytest accept and forward *kw, except
Class (before this change) and DoctestItem - since I'm not familiar with
doctest support, I've left the latter as-is.
2021-02-23 18:00:56 +01:00
Ran Benita 1003beaffa
Merge pull request #8323 from bluetech/setupstate-refactor-3
runner: a few more tweaks to SetupState
2021-02-08 13:56:04 +02:00
Ronny Pfannschmidt 325d701f6e
Merge pull request #8248 from RonnyPfannschmidt/mixin-mro-position
alter the PyObjMixin to carry over typing information from Node
2021-02-07 18:44:44 +01:00
Ran Benita 5822888d73 runner: add clarifying comments on why runtestprotocol re-inits the FixtureRequest 2021-02-06 20:46:08 +02:00
Ran Benita f42b68ccaa runner: rename SetupState.prepare -> setup
This is the usual terminology we use, and matches better with
`teardown_exact()` and `pytest_runtest_setup()`.
2021-02-06 20:46:08 +02:00
Ran Benita f674f6da9f runner: add a safety assert to SetupState.prepare
This ensures that the teardown for the previous item was done properly
for this (next) item, i.e. there are no leftover teardowns.
2021-02-06 20:46:08 +02:00
Pax bcfe253f5b
Type annotation for request.param (#8319) 2021-02-05 22:03:58 +02:00
Ran Benita b3115c1b06
Merge pull request #8315 from bluetech/parser-proper-deprecation
config/argparsing: use proper deprecations instead of ad-hoc DeprecationWarning
2021-02-05 00:37:38 +02:00
Ran Benita d358a060ad config/argparsing: use proper deprecations instead of ad-hoc DeprecationWarning
Proper for removing this in the next major pytest release.
2021-02-05 00:05:47 +02:00
Hong Xu 80c223474c
Type annotation polishing for symbols around Pytester.run (#8298)
* Type annotation polishing for symbols around Pytester.run

Hopefully these will help document readers understand pertinent methods
and constants better.

Following up #8294

* Use NOTSET instead of object
2021-02-04 23:44:22 +02:00
Ran Benita afea190797 Remove some no longer needed type-ignores 2021-01-29 20:40:43 +02:00
Hong Xu 07f0eb26b4
Doc: Clarify pytester.run (#8294)
Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
2021-01-29 12:54:06 -03:00
Bruno Oliveira 6a5d47a243
Merge pull request #8260 from nicoddemus/faulthandler-mode-X-8258 2021-01-28 13:03:24 -03:00
Bruno Oliveira dfe933cdb4 Remove mypy workaround after 0.800 update 2021-01-25 15:23:13 -03:00
Bruno Oliveira 33861098d9 Only re-enable fauthandler during unconfigure if it was enabled before 2021-01-25 12:28:00 -03:00
Ran Benita 48fb989a71 runner: avoid using node's store in SetupState
SetupState maintains its own state, so it can store the exception
itself, instead of using the node's store, which is better avoided when
possible.

This also reduces the lifetime of the reference-cycle-inducing exception
objects which is never a bad thing.
2021-01-24 15:05:03 +02:00
Ran Benita c30feeef8b runner: add docstring to SetupState and improve variable naming a bit 2021-01-24 14:29:54 +02:00
Ran Benita 0d19aff562 runner: schedule node.teardown() call already at setup
This is more elegant.
2021-01-24 14:08:39 +02:00
Ran Benita 1db78bec31 runner: use insertion-ordered dict instead of stack, dict pair
Since dicts are now ordered, we can use the finalizers dict itself as
the dict, simplifying the code.
2021-01-24 14:08:39 +02:00
Ran Benita 03c3a90c68 runner: replace setdefault with an unconditional set
The already-exists case is not supposed to happen.
2021-01-24 14:08:39 +02:00
Ran Benita 960ebae943 runner: enable a commented assertion in SetupState.addfinalizer
The assertion ensures that when `addfinalizer(finalizer, node)` is
called, the node is in the stack. This then would ensure that the
finalization is actually properly executed properly during the node's
teardown. Anything else indicates something is wrong.

Previous commits fixed all of the tests which previously failed this, so
can be reenabeld now.
2021-01-24 14:08:39 +02:00
Ran Benita 6db082a448 fixtures: make sure to properly setup stack for _fill_fixtures_impl
This code is weird, dead, deprecated and will be removed in pytest 7,
but for now some tests execute it, so fix it up in preparation for
some changes.
2021-01-24 14:08:39 +02:00
Ran Benita 0d4121d24b runner: collapse exception handling in SetupState.teardown_exact()
This is equivalent but simpler.
2021-01-24 14:08:39 +02:00
Ran Benita bb3d43c9a6 runner: ensure item.teardown() is called even if a finalizer raised
If one finalizer fails, all of the subsequent finalizers still run, so
the `teardown()` method should behave the same.
2021-01-24 14:08:39 +02:00
Ran Benita 14d71b2c22 runner: make sure SetupState._finalizers is always set for a node in the stack
This makes the stack <-> _finalizers correspondence clearer.
2021-01-24 14:08:39 +02:00
Ran Benita d1fcd425a3 runner: inline SetupState._pop_and_teardown()
This will enable a simplification in the next commit.
2021-01-24 14:08:39 +02:00
Ran Benita 2b14edb108 runner: express SetupState.teardown_all() in terms of teardown_exact() and remove it
Makes it easier to understand with fewer methods.
2021-01-24 14:08:39 +02:00
Ran Benita ceb4d6f6d5 runner: inline a couple of SetupState methods
Code is clearer this way.
2021-01-24 14:08:39 +02:00
Ran Benita 5f4e55fb6d runner: remove dead code in teardown_all()
When the stack is empty, the finalizers which are supposed to be
attached to nodes in the stack really ought to be empty as well. So the
code here is dead. If this doesn't happen, the assert will trigger.
2021-01-24 14:08:39 +02:00
Ran Benita 42ae8180dd runner: inline SetupState._teardown_towards()
Doesn't add much.
2021-01-24 14:08:39 +02:00
Ran Benita 410622f719 runner: reorder SetupState method to make more sense
The setup stuff happens before the teardown stuff, so put it first so
that reading the code from top to bottom makes more sense.
2021-01-24 14:08:39 +02:00
Ran Benita f7b0b1dd1f runner: use node's Store to keep private SetupState state instead of an attribute
This way it gets proper typing and decoupling.
2021-01-24 14:08:39 +02:00
Ran Benita da70f61f67 runner: complete type annotations of SetupState 2021-01-24 14:08:39 +02:00
Hong Xu d4f8e4b40c
Explain how to create binary files in the doc of `pytest.makefile`. (#8255) 2021-01-21 09:58:52 -03:00
Bruno Oliveira adc0f29b8f Always handle faulthandler stderr even if already enabled
It seems the code that would not install pytest's faulthandler support
if it was already enabled is not really needed at all, and even detrimental
when using `python -X dev -m pytest` to run Python in "dev" mode.

Also simplified the plugin by removing the hook class, now the hooks
will always be active so there's no need to delay the hook definitions anymore.

Fix #8258
2021-01-20 10:29:05 -03:00
Bruno Oliveira bda9ce4e0f
Merge pull request #8250 from daq-tools/fix-twisted-capture 2021-01-20 09:45:58 -03:00
Andreas Motl 9ba1821e91 Fix faulthandler for Twisted Logger when used with "--capture=no"
The Twisted Logger will return an invalid file descriptor since it is
not backed by an FD. So, let's also forward this to the same code path
as with `pytest-xdist`.
2021-01-18 17:51:08 +01:00
Ran Benita 7f782c72ba
Merge pull request #8242 from bluetech/deprecate-unittest-skip-collection
Deprecate raising unittest.SkipTest to skip tests during collection
2021-01-18 17:46:00 +02:00
Ronny Pfannschmidt a9e43152bc alter the PyObjMixin to carry over typing information from Node
as PyObjMixin is always supposed to be mixed in the mro
before nodes.Node the behavior doesn't change,
but all the typing information carry over to help mypy.

extracted from #8037
2021-01-17 14:36:28 +01:00
Ran Benita 25e657bfc1 Deprecate raising unittest.SkipTest to skip tests during collection
It is not very clear why this code exists -- we are not running any
unittest or nose code during collection, and really these frameworks
don't have the concept of collection at all, and just raising these
exceptions at e.g. the module level would cause an error. So unless I'm
missing something, I don't think anyone is using this.

Deprecate it so we can eventually clear up this code and keep unittest
more tightly restricted to its plugin.
2021-01-15 00:05:33 +02:00
Ran Benita 7f989203ed Improve way in which skip location is fixed up when skipped by mark
When `pytest.skip()` is called inside a test function, the skip location
should be reported as the line that made the call, however when
`pytest.skip()` is called by the `pytest.mark.skip` and similar
mechanisms, the location should be reported at the item's location,
because the exact location is some irrelevant internal code.

Currently the item-location case is implemented by the caller setting a
boolean key on the item's store and the `skipping` plugin checking it
and fixing up the location if needed. This is really roundabout IMO and
breaks encapsulation.

Instead, allow the caller to specify directly on the skip exception
whether to use the item's location or not. For now, this is entirely
private.
2021-01-14 18:07:25 +02:00
Ran Benita 3dde519f53 nose: type annotate with some resulting refactoring 2021-01-14 17:42:38 +02:00
Ran Benita 096bae6c68 unittest: add clarifying comment on unittest.SkipTest -> pytest.skip code
I was tempted to remove it, until I figured out why it was there.
2021-01-14 17:06:14 +02:00
Ran Benita addbd3161e nose,fixtures: use the public item API for adding finalizers 2021-01-14 16:53:07 +02:00
Anton 42d5545f42
unittest: cleanup unexpected success handling (#8231)
* unittest: cleanup unexpected success handling

* update comment
2021-01-13 17:02:26 -08:00
Petr Viktorin 78fb97105f Make code.FormattedExcinfo.get_source more defensive
When line_index was a large negative number, get_source failed
on `source.lines[line_index]`.
Use the same dummy Source as with a large positive line_index.
2021-01-06 13:33:33 +01:00
Ran Benita 5336ba28cc
Merge pull request #8218 from bluetech/reports2
Misc small code improvements
2021-01-04 22:47:32 +02:00
bengartner 8e00df4c4b
Add dot prefix if file makefile extension is invalid for pathlib (#8222) 2021-01-04 15:58:11 +02:00
Ran Benita 2ff88098a7 python: inline a simple method
I don't think it adds much value!
2021-01-01 23:23:08 +02:00
Ran Benita 8ee6d0a866 Always use getfixturemarker() to access _pytestfixturefunction
Keep knowledge of how the marker is stored encapsulated in one place.
2021-01-01 23:22:10 +02:00
Ran Benita ade253c790 fixtures: type annotate FixtureRequest.keywords 2021-01-01 23:18:17 +02:00
Ran Benita 6c575ad8c8 fixtures: simplify FixtureRequest._get_fixturestack() 2021-01-01 23:15:57 +02:00
Ran Benita 73c4105230 reports: improve a type annotation 2021-01-01 15:22:05 +02:00
Ran Benita 7d306e9e86 reports: BaseReport.{passed,failed,skipped} more friendly to mypy
Not smart enough to understand the previous code.
2021-01-01 15:22:05 +02:00
Anton 48c9a96a03
Fix failing staticmethod tests if they are inherited (#8205)
* Fix failing staticmethod tests if they are inherited

* add comments, set default=None
2020-12-30 19:00:37 -08:00
Ran Benita 7585221d55
Merge pull request #8195 from christophebedard/add-missing-space-version-option-help-message
Add missing space in '--version' help message
2020-12-30 11:57:02 +02:00
pre-commit-ci[bot] ee03e31831
[pre-commit.ci] pre-commit autoupdate (#8201)
* [pre-commit.ci] pre-commit autoupdate

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

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

* manual fixes after configuration update

* [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>
Co-authored-by: Anthony Sottile <asottile@umich.edu>
2020-12-30 11:56:09 +02:00
Christophe Bedard bf9b59b3c8 Add missing space in '--version' help message 2020-12-28 10:50:06 -05:00
Ran Benita 96ea867fec runner: export pytest.CallInfo for typing purposes
The type cannot be constructed directly, but is exported for use in type
annotations, since it is reachable through existing public API.

This also documents `from_call` as public, because at least
pytest-forked uses it, so we must treat it as public already anyway.
2020-12-26 21:38:37 +02:00
Ran Benita bd76042344 python: export pytest.Metafunc for typing purposes
The type cannot be constructed directly, but is exported for use in type
annotations, since it is reachable through existing public API.
2020-12-26 21:05:02 +02:00
Ran Benita 6d3a66d947 nodes: avoid needing to expose NodeKeywords for typing
It adds no value over exporting just the ABC so do that to reduce the
API surface.
2020-12-26 19:54:07 +02:00
Ran Benita d8d2df7e6f
Merge pull request #8174 from bluetech/py-to-pathlib-5
More py.path -> pathlib conversions
2020-12-26 15:01:07 +02:00
Ran Benita ca4effc822 Convert most of the collection code from py.path to pathlib 2020-12-22 21:09:36 +02:00
Ran Benita a218413008 pathlib: missing type annotation for fnmatch_ex 2020-12-22 21:08:25 +02:00
Ran Benita 92ba96b061 code: convert from py.path to pathlib 2020-12-22 21:08:25 +02:00
Ran Benita 6aa4d1c7ab mark: export pytest.MarkGenerator for typing purposes
The type cannot be constructed directly, but is exported for use in type
annotations, since it is reachable through existing public API.
2020-12-20 15:59:20 +02:00
Ran Benita 69c302479e mark: export pytest.MarkDecorator for typing purposes
The type cannot be constructed directly, but is exported for use in type
annotations, since it is reachable through existing public API.
2020-12-20 15:59:06 +02:00
Ran Benita 2ec372df8b mark: export pytest.Mark for typing purposes
The type cannot be constructed directly, but is exported for use in type
annotations, since it is reachable through existing public API.
2020-12-20 15:58:49 +02:00
Ran Benita 042d12fae6 doctest: use Path instead of py.path where possible 2020-12-19 14:52:40 +02:00
Ran Benita 2c05a7babb config: let main() accept any os.PathLike instead of just py.path.local
Really it ought to only take the List[str], but for backward
compatibility, at least get rid of the explicit py.path.local check.
2020-12-19 14:52:40 +02:00
Ran Benita 73586be08f terminal: remove unused union arm in WarningReport.fslocation 2020-12-19 13:33:34 +02:00
antonblr 196b173c8a address comments 2020-12-18 12:36:20 -08:00
antonblr 15156e94c4 tests: Migrate to pytester - final update 2020-12-18 11:02:38 -08:00
Ran Benita d46ecbc18b terminal: fix "(<Skipped instance>)" skip reason in test status line 2020-12-15 22:24:25 +02:00
Jakob van Santen 9ccbf5b899
python_api: handle array-like args in approx() (#8137) 2020-12-15 08:49:29 -03:00
Ran Benita 592b32bd69 hookspec: add pathlib.Path alternatives to py.path.local parameters in hooks
As part of the ongoing migration for py.path to pathlib, make sure all
hooks which take a py.path.local also take an equivalent pathlib.Path.
2020-12-15 00:34:23 +02:00
Ran Benita 2cb34a99cb Some py.path.local -> pathlib.Path 2020-12-15 00:29:13 +02:00
antonblr 4ed9a38519 tests: Migrate testing/python to pytester fixture 2020-12-13 07:42:12 -08:00
Bruno Oliveira 7e2e6630ad
Merge pull request #8123 from nicoddemus/import-mismatch-unc
Compare also paths on Windows when considering ImportPathMismatchError
2020-12-13 10:35:11 -03:00
Ran Benita 37b154b1ec
Merge pull request #8122 from bluetech/py-to-pathlib-3
Some py.path.local -> pathlib.Path
2020-12-13 10:54:19 +02:00
Ran Benita 3302ff9949 terminal: when the skip/xfail is empty, don't show it as "()"
Avoid showing a line like

    x.py::test_4 XPASS ()   [100%]

which looks funny.
2020-12-12 22:09:00 +02:00
Pedro Algarvio b16c091253 Add `pytest_markeval_namespace` hook.
Add a new hook , `pytest_markeval_namespace` which should return a dictionary.
This dictionary will be used to augment the "global" variables available to evaluate skipif/xfail/xpass markers.

Pseudo example

``conftest.py``:

.. code-block:: python
   def pytest_markeval_namespace():
       return {"color": "red"}
``test_func.py``:

.. code-block:: python
   @pytest.mark.skipif("color == 'blue'", reason="Color is not red")
   def test_func():
       assert False
2020-12-12 17:41:37 +02:00
Ran Benita ed658d6829 Some py.path.local -> pathlib.Path
- Some conftest related functions
- _confcutdir
- Allow arbitrary os.PathLike[str] in gethookproxy.
2020-12-12 17:33:28 +02:00
Bruno Oliveira 572dfcd160 Compare also paths on Windows when considering ImportPathMismatchError
On Windows, os.path.samefile returns false for paths mounted in UNC paths which
point to the same location.

I couldn't reproduce the actual case reported, but looking at the code it seems
this commit should fix the issue.

Fix #7678
Fix #8076
2020-12-12 08:54:49 -03:00
Katarzyna 612f157dbd Show reason for skipped test in verbose mode 2020-12-09 09:43:47 +02:00
Ran Benita 1a1bb61340
Merge pull request #8038 from matthewhughes934/note-tmpdir-lifetime
Docs: Note lifetime of temporary directories
2020-12-05 21:54:04 +02:00
Ran Benita e398c93884
Merge pull request #8055 from bluetech/unraisable
Add unraisableexception and threadexception plugins
2020-12-05 21:52:17 +02:00
Ran Benita 760a73c08c
Merge pull request #8017 from bluetech/typing-public-fixtures
Export types of builtin fixtures for type annotations
2020-12-05 21:51:20 +02:00
Matthew Hughes 6a256606c6 Docs: Note lifetime of temporary directories
Explanation: The default handling of these lifetimes is done in
`tmpdir.TempPathFactory.getbasetemp`, which passes `keep=3` to
`pathlib.make_numbered_dir_with_cleanup`.

GH Issue: #8036
2020-11-28 21:18:16 +00:00
Ran Benita 70823da7ed
Merge pull request #8064 from symonk/fix-typo-in-mock-timing
fix mock_timing fixture name (typo) in timing.py
2020-11-25 12:29:34 +02:00
Jürgen Gmach d27806295a
fix typo (#8069) 2020-11-24 22:27:34 +01:00
Simon K b310872300
fix mock_timing fixture name (typo) in timing.py 2020-11-23 20:45:12 +00:00
Ran Benita d50df85e26 Add unraisableexception and threadexception plugins 2020-11-21 21:11:48 +02:00
Maximilian Cosmo Sitter 0cef530d10
Add str() support to LineMatcher (#8050) 2020-11-21 20:45:20 +02:00
Simon K 52fef811c2
permit node to warn with any warning type, not just PytestWarning (#8052)
Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
2020-11-21 10:49:17 -03:00
Ran Benita 148e3c582a pytester: always close stdin pipe in pytester.run()
If the user passed stdin=PIPE for some reason, they have no way to close
it themselves since it is not exposed.
2020-11-20 17:55:30 +02:00
Tim Hoffmann afd53ede6f
Link mentioned functions instead of using literals (#8045) 2020-11-19 15:44:59 +01:00
Petter Strandmark eda681af2b
Call Python 3.8 doClassCleanups (#8033) 2020-11-19 12:07:15 +02:00
Tim Hoffmann b7ba76653d
Prefix contextmanagers with module name in doc examples (#8044)
* Prefix contextmanagers with module name in doc examples

* Import pytest explicitly for doctests

Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
2020-11-19 12:06:24 +02:00
symonk 3b677f79f4 stop assigning nextline if its potentially not used 2020-11-17 11:30:09 +00:00
Ran Benita 825b81ba52
Merge pull request #8014 from bluetech/pyc-pep552
assertion/rewrite: write pyc's according to PEP-552 on Python>=3.7
2020-11-14 23:38:45 +02:00
Ran Benita 1d532da49e assertion/rewrite: write pyc's according to PEP-552 on Python>=3.7
Python 3.7 changes the pyc format by adding a flags byte. Even though it
is not necessary for us to match it, it is nice to be able to read pyc
files we emit for debugging the rewriter.

Update our custom pyc files to use that format. We write flags==0
meaning we still use the mtime+size format rather the newer hash format.
2020-11-14 23:20:12 +02:00
Ran Benita f1e6fdcddb Export types of builtin fixture for type annotations
In order to allow users to type annotate fixtures they request, the
types need to be imported from the `pytest` namespace. They are/were
always available to import from the `_pytest` namespace, but that is
not guaranteed to be stable.

These types are only exported for the purpose of typing. Specifically,
the following are *not* public:

- Construction (`__init__`)
- Subclassing
- staticmethods and classmethods

We try to combat them being used anyway by:

- Marking the classes as `@final` when possible (already done).

- Not documenting private stuff in the API Reference.

- Using `_`-prefixed names or marking as `:meta private:` for private
  stuff.

- Adding a keyword-only `_ispytest=False` to private constructors,
  warning if False, and changing pytest itself to pass True. In the
  future it will (hopefully) become a hard error.

Hopefully that will be enough.
2020-11-13 11:25:09 +02:00
Ran Benita b050578882 pytester: split asserts to a separate plugin, don't rewrite pytester itself
An upcoming commit wants to import from `_pytest.pytester` in the public
`pytest` module. This means that `_pytest.pytester` would start to get
imported during import time, which it hasn't up to now -- it was
imported by the plugin loader (if requested). When a plugin is loaded,
it is subjected to assertion rewriting, but only if the module isn't
imported yet, it issues a warning "Module already imported so cannot be
rewritten" and skips the rewriting. So we'd end up with the pytester
plugin not being rewritten, but it wants to be.

Absent better ideas, the solution here is to split the pytester
assertions to their own plugin (which will always only be imported by
the plugin loader) and exclude pytester itself from plugin rewriting.
2020-11-13 11:25:09 +02:00
Ran Benita 66311ff702
Merge pull request #8022 from bluetech/doctest-init
main: fix only one doctest collected on pytest --doctest-modules __init__.py
2020-11-13 10:46:46 +02:00
Adam Johnson 39b2706f6a Add 'node_modules' to norecursedirs
Fixes #8023.
2020-11-11 01:52:18 +00:00
Ran Benita 265cc2cfec main: fix only one doctest collected on pytest --doctest-modules __init__.py
When --doctest-modules is used, an `__init__.py` file is not a `Package`
but a `DoctestModule`, but some collection code assumed that
`__init__.py` implies a `Package`. That code caused only a single test
to be collected in the scenario in the subject.

Tighten up this check to explicitly check for `Package`. There are
better solutions, but for another time.

Report & test by Nick Gates <nickgatzgates@gmail.com>.
2020-11-10 22:50:46 +02:00
Ran Benita e986d84466
Merge pull request #8006 from bluetech/export-MonkeyPatch
Export MonkeyPatch as pytest.MonkeyPatch
2020-11-09 11:45:38 +02:00
Ran Benita 6f13d1b03b Export MonkeyPatch as pytest.MonkeyPatch
We want to export `pytest.MonkeyPatch` for the purpose of
type-annotating the `monkeypatch` fixture. For other fixtures we export
in this way, we also make direct construction of them (e.g.
`MonkeyPatch()`) private. But unlike the others, `MonkeyPatch` is also
widely used directly already, mostly because the `monkeypatch` fixture
only works in `function` scope (issue #363), but also in other cases. So
making it private will be annoying and we don't offer a decent
replacement yet.

So, let's just make direct construction public & documented.
2020-11-09 11:28:15 +02:00
Bruno Oliveira c7f8ad17f5 Rename _make_plural to pluralize
A bit shorter and a better name, IMHO.
2020-11-08 12:42:52 -03:00
Hugo Martins 5b2e5e8a40
Improve summary stats when using '--collect-only' (#7875)
Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
2020-11-08 11:45:10 -03:00
Ran Benita 29f2f4e854
Merge pull request #8005 from bluetech/pytest-import
Stop importing `pytest` to avoid upcoming import cycles
2020-11-08 14:15:19 +02:00
Ran Benita 1cbb0c3554 Stop importing `pytest` to avoid upcoming import cycles
Don't import `pytest` from within some `_pytest` modules since an
upcoming commit will import from them into `pytest`.

It would have been nice not to have to do it, so that internal plugins
look more like external plugins, but with the existing layout this seems
unavoidable.
2020-11-07 18:08:30 +02:00
Ran Benita 4c0513bc18 fixtures: deprecate pytest.yield_fixture() 2020-11-07 17:06:40 +02:00
Sanket Duthade 3bcd316f07
test_collection.py migrate from testdir to Pytester (#8003) 2020-11-07 16:56:00 +02:00
Bruno Oliveira 30287b49cd
Deprecate --strict (#7985)
Fix #7530
2020-11-06 09:48:20 +01:00
Ran Benita 489f6f4499 unittest: fix quadratic behavior in collection of unittests using setUpClass/setup_method
This is similar to 50114d4731876dae; I missed that unittest does the
same thing.
2020-11-01 15:17:42 +02:00
Ran Benita a95da7a425
Merge pull request #7980 from bluetech/code-changes
code: a few minor improvements
2020-11-01 09:51:39 +02:00
Ran Benita 7fb0ea3f68
Merge pull request #7956 from csernazs/fix-7951
Fix handling recursive symlinks
2020-10-31 18:59:50 +02:00
Cserna Zsolt 8a38e7a6e8 Fix handling recursive symlinks
When pytest was run on a directory containing a recursive symlink it failed
with ELOOP as the library was not able to determine the type of the
direntry:

src/_pytest/main.py:685: in collect
    if not direntry.is_file():
E   OSError: [Errno 40] Too many levels of symbolic links: '/home/florian/proj/pytest/tests/recursive'

This is fixed by handling ELOOP and other errors in the visit function in
pathlib.py, so the entries whose is_file() call raises an OSError with the
pre-defined list of error numbers will be exluded from the result.

The _ignore_errors function was copied from Lib/pathlib.py of cpython 3.9.

Fixes #7951
2020-10-31 17:40:56 +01:00
Ran Benita 1c18fb8ccc
Merge pull request #7553 from tirkarthi/namedtuple-diff
Add support to display field names in namedtuple diffs.
2020-10-31 15:02:31 +02:00
Bruno Oliveira 2753859ff0
Merge pull request #7979 from nicoddemus/metafunc-ref
Add FunctionDefinition to the reference docs
2020-10-31 10:00:48 -03:00
Karthikeyan Singaravelan 9a0f4e57ee Add support to display field names in namedtuple diffs. 2020-10-31 14:41:53 +02:00
Ran Benita 6cdae8ed40 pathlib: fix symlinked directories not followed during collection 2020-10-31 14:22:15 +02:00
Bruno Oliveira 569c091769 Add FunctionDefinition to the reference docs
Fix #7968
2020-10-31 08:45:34 -03:00
Ran Benita 531416cc5a code: simplify Code construction 2020-10-31 12:40:25 +02:00
Ran Benita a1df458e85 code: use properties for derived attributes, use slots
Make the objects more light weight.

Remove unused properties.
2020-10-31 12:40:25 +02:00
Ran Benita 5913cd20ec assertion/util: remove unhelpful `type_fns` indirection
It doesn't serve any purpose that I am able to discern.
2020-10-30 21:15:48 +02:00
Simon K 6cddeb8cb3
#7938 - [Plugin: Stepwise][Enhancements] Refactoring, smarter registration & --sw-skip functionality (#7939)
* adding --sw-skip shorthand for stepwise skip

* be explicit rather than implicit with default args for stepwise

* add constant for sw cache dir; only register plugin if necessary rather check check activity always;

* use str format; remove unused args in hooks

* assert cache upfront, allow stepwise to have a reference to the cache

* type hinting lf, skip, move literal strings into module constants

* convert parametrized option into a list

* add a sessionfinish hook for stepwise to keep backwards behaviour the same

* add changelog for #7938

* Improve performance of stepwise modifyitems & address PR feedback

* add test for stepwise deselected based on performance enhancements

* Apply suggestions from code review

* delete from items, account for edge case where failed_index = 0

Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
2020-10-30 19:13:06 +00:00
Vasilis Gerakaris a431310c0a
Increase temp dir deletion period to 3 days (#7914)
Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
2020-10-28 08:23:35 -03:00
Ran Benita ca82214444 pytester: workaround issue causing spawn to crash or hang
In pytester tests, pytest stashes & restores the sys.modules for each
test. So if the test imports a new module, it is initialized anew each
time.

Turns out the readline module isn't multi-init safe, which causes
pytester.spawn to crash or hang. So preserve it as a workaround.
2020-10-25 16:31:47 +02:00