Commit Graph

4017 Commits

Author SHA1 Message Date
Sorin Sbarnea b031a7cecf
Smoke tests for assorted plugins (#7721)
Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
Co-authored-by: Thomas Grainger <tagrain@gmail.com>
Co-authored-by: Kyle Altendorf <sda@fstab.net>
2020-09-19 15:56:52 -03:00
Florian Bruhin 89305e7b09
Improve output for missing config keys (#7572)
Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
2020-09-19 12:57:29 -03:00
Bruno Oliveira 9bfd14a443
Merge pull request #7749 from bluetech/fix-get_source-crash 2020-09-19 10:38:32 -03:00
Sorin Sbarnea da21fc5883
Improve output for missing required plugins/unknown config keys (#7723)
Co-authored-by: Florian Bruhin <me@the-compiler.org>
2020-09-16 12:13:17 +02:00
Ran Benita d18cb961cf assertion/rewrite: fix internal error on collection error due to decorated function
For decorated functions, the lineno of the FunctionDef AST node points
to the `def` line, not to the first decorator line. On the other hand,
in code objects, the `co_firstlineno` points to the first decorator
line.

Assertion rewriting inserts some imports to code it rewrites. The
imports are inserted at the lineno of the first statement in the AST. In
turn, the code object compiled from the rewritten AST uses the lineno of
the first statement (which is the first inserted import).

This means that given a module like this,

```py
@foo
@bar
def baz(): pass
```

the lineno of the code object without assertion rewriting
(`--assertion=plain`) is 1, but with assertion rewriting it is 3.

And *this* causes some issues for the exception repr when e.g. the
decorator line is invalid and raises during collection. The code becomes
confused and crashes with

INTERNALERROR>   File "_pytest/_code/code.py", line 638, in get_source
INTERNALERROR>     lines.append(space_prefix + source.lines[line_index].strip())
INTERNALERROR> IndexError: list index out of range

Fix it by special casing decorators. Maybe there are other cases like
this but off hand I can't think of another Python construct where the
lineno of the item would be after its first line, and this is the only
such issue we have had reported.
2020-09-12 23:05:08 +03:00
Bruno Oliveira 634cde9506
Merge pull request #7745 from asottile/exec_globals_type_problem
Fix INTERNALERROR when accessing locals / globals with faulty `exec`
2020-09-12 08:09:48 -03:00
Anthony Sottile 96a17b1683 Fix INTERNALERROR when accessing locals / globals with faulty `exec` 2020-09-11 18:13:48 -07:00
Bruno Oliveira c00fe960ba Allow ovewriting a parametrized fixture while reusing the parent fixture's value
Fix #1953
2020-09-11 16:53:34 -03:00
Ran Benita 885d969484
Merge pull request #7685 from bluetech/py-to-pathlib-2
config: start migrating Config.{rootdir,inifile} from py.path.local to pathlib
2020-09-04 18:42:52 +03:00
Bruno Oliveira 0d0b798663
Merge pull request #7708 from nicoddemus/repr-line-7707
Fix handle of exceptions in ReprEntry with tb=line
2020-09-04 12:06:11 -03:00
Ran Benita 62e249a1f9 Replace some usages of config.{rootdir,inifile} with config.{rootpath,inipath} 2020-09-04 18:05:42 +03:00
Bruno Oliveira 19e99ab413
Integrate warnings filtering directly into Config (#7700)
Warnings are a central part of Python, so much that Python itself has
command-line and environtment variables to handle warnings.

By moving the concept of warning handling into Config, it becomes natural to
filter warnings issued as early as possible, even before the "_pytest.warnings"
plugin is given a chance to spring into action. This also avoids the weird
coupling between config and the warnings plugin that was required before.

Fix #6681
Fix #2891
Fix #7620
Fix #7626
Close #7649

Co-authored-by: Ran Benita <ran@unusedvar.com>
2020-09-04 11:57:15 -03:00
Bruno Oliveira 9f672c85c5 Fix handle of exceptions in ReprEntry with tb=line
Fix #7707
2020-09-03 07:44:45 -03:00
Ran Benita daca174c98 python: small optimization in PyCollector.collect()
Inline `_makeitem()` so that `self.ihook` (which is moderately
expensive) can be called only once.

Note: the removed test "test_makeitem_non_underscore" comes from an old
behavior of skipping names that start with `_` which has since been
generalized, making the test no longer relevant.
2020-08-27 10:18:37 +03:00
Ran Benita 98891a5947 python: skip pytest_pycollect_makeitem work on certain names
When a Python object (module/class/instance) is collected, for each name
in `obj.__dict__` (and up its MRO) the pytest_pycollect_makeitem hook is
called for potentially creating a node for it.

These Python objects have a bunch of builtin attributes that are
extremely unlikely to be collected. But due to their pervasiveness,
dispatching the hook for them ends up being mildly expensive and also
pollutes PYTEST_DEBUG=1 output and such.

Let's just ignore these attributes.

On the pandas test suite commit 04e9e0afd476b1b8bed930e47bf60e,
collect only, irrelevant lines snipped, about 5% improvement:

Before:

```
         51195095 function calls (48844352 primitive calls) in 39.089 seconds

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
226602/54    0.145    0.000   38.940    0.721 manager.py:90(_hookexec)
    72227    0.285    0.000   20.146    0.000 python.py:424(_makeitem)
    72227    0.171    0.000   16.678    0.000 python.py:218(pytest_pycollect_makeitem)
```

After:

```
          48410921 function calls (46240870 primitive calls) in 36.950 seconds

    ncalls  tottime  percall  cumtime  percall filename:lineno(function)
 181429/54    0.113    0.000   36.777    0.681 manager.py:90(_hookexec)
     27054    0.130    0.000   17.755    0.001 python.py:465(_makeitem)
     27054    0.121    0.000   16.219    0.001 python.py:218(pytest_pycollect_makeitem)
```
2020-08-26 17:43:57 +03:00
Ran Benita a267a622eb python: fix empty parametrize() leading to "NotSetType.token" id
In ff8b7884e8 NOTSET was changed to a
singleton enum, which ended up unexpectedly triggering a code path in ID
generation which checks for `isinstance(Enum)`.

Add an explicit case for it, which is not too bad anyway.
2020-08-25 22:01:43 +03:00
Ran Benita bb38ae9c52
Merge pull request #7651 from bluetech/capture-safe-disable
capture: fix disabled()/global_and_fixture_disabled() enabling capturing when it was disabled
2020-08-24 12:11:09 +03:00
Ran Benita b47b488e3d testing: fix flaky test when executed slowly
The 0-1 was a bit too optimistic: CI got "no tests ran in 3.98s".
2020-08-23 12:32:30 +03:00
Ran Benita 143e3ab846
Merge pull request #7673 from bluetech/logging-fix-handler-restore
logging: fix handler level restored incorrectly if caplog.set_level is called more than once
2020-08-23 12:06:09 +03:00
Ran Benita 837687c21a
Merge pull request #7668 from bluetech/dir-match-msg
main: improve message on `pytest path/to/a/directory::mytest`
2020-08-23 12:05:22 +03:00
Ran Benita b1354608cc logging: fix handler level restored incorrectly if caplog.set_level is called more than once 2020-08-22 17:46:23 +03:00
Maximilian Cosmo Sitter 75af2bfa06
Reintroduce warnings postponed in 6.0 (#7637) 2020-08-22 11:17:50 -03:00
Ran Benita 5e39cd5e71 main: improve message on `pytest path/to/a/directory::mytest`
The path part of a `<path>::part1::part2` style collection argument must
be a file, not a directory.

Previously this crashed with an uncool assert "invalid arg".
2020-08-22 11:52:54 +03:00
Bruno Oliveira 372a094005 PytestDeprecationWarning no longer a hard error 2020-08-19 08:14:28 -03:00
Bruno Oliveira ef946d557c Remove resultlog plugin 2020-08-19 08:14:28 -03:00
Bruno Oliveira 52b0cc4f19 Remove broken pytest_collect_directory hook 2020-08-19 08:14:25 -03:00
Bruno Oliveira 457d351941 Remove deprecated TerminalReporter.writer property 2020-08-19 08:13:34 -03:00
Bruno Oliveira 6ecbd008c4 Change junit_family default to xunit2 2020-08-19 08:13:06 -03:00
Bruno Oliveira 73e06373dc Hard failure when constructing Node subclasses 2020-08-19 08:13:03 -03:00
Bruno Oliveira c747dc5248 Drop support for positional arguments in @pytest.fixture 2020-08-19 08:11:42 -03:00
Bruno Oliveira 98530184a5 Remove funcargnames compatibility property 2020-08-19 08:11:39 -03:00
Ran Benita c98525bd21
Merge pull request #7648 from bluetech/pylint-abc2
Only define gethookproxy, isinitpath on Session
2020-08-18 15:45:11 +03:00
Bruno Oliveira afa4760cb8
Merge pull request #7643 from nicoddemus/issue-7628 2020-08-17 16:58:56 -03:00
Ran Benita 0d5a65091d capture: fix disabled()/global_and_fixture_disabled() enabling capturing when it was disabled
The `CaptureManager.global_and_fixture_disabled()` context manager (and
`CaptureFixture.disabled()` which calls it) did `suspend(); ...;
resume()` but if the capturing was already suspended, the `resume()`
would resume it when it shouldn't.

This caused caused some messages to be swallowed when `--log-cli` is
used because it uses `global_and_fixture_disabled` when capturing is not
necessarily resumed.
2020-08-16 23:21:45 +03:00
Bruno Oliveira b426bb3443 Refactor Session._parsearg into a separate function for testing 2020-08-15 13:23:55 -03:00
Bruno Oliveira 2213016e40 Fix Module.name from full path without drive letter
Fix #7628
2020-08-15 09:39:14 -03:00
Ran Benita eddd993cf4 Only define gethookproxy, isinitpath on Session
This fixes an issue where pylint complains about missing implementations
of abstract methods in subclasses of `File` which only override
`collect()` (as they should).

It is also cleaner and makes sense, these methods really don't need to
be overridden.

The previous methods defined directly on `FSCollector` and `Package` are
deprecated, to be removed in pytest 7.

See commits e2934c3f8c and
f10ab021e2 for reference.
2020-08-15 13:40:16 +03:00
Bruno Oliveira 36c8bb492e get_dirs_from_args handles paths with invalid syntax
Fix #7638
2020-08-12 17:20:09 -03:00
Ran Benita acc9310c17 capture: add type annotations to CaptureFixture
It now has a str/bytes type parameter.
2020-08-10 18:14:47 +03:00
Ran Benita 8a66f0a96d capture: overcome a mypy limitation by making CaptureResult a regular class
See the code comment for the rationale.
2020-08-10 18:14:47 +03:00
Ran Benita f8c4e038fd Replace some usages of py.path.local 2020-08-06 18:46:17 +03:00
Ran Benita 70f3ad1c1f config/findpaths: convert from py.path.local to pathlib 2020-08-06 18:46:17 +03:00
Ran Benita e0d0951945 pathlib: add analogues to py.path.local's bestrelpath and common
An equivalent for these py.path.local functions is needed for some
upcoming py.path -> pathlib conversions.
2020-08-06 18:16:04 +03:00
Bruno Oliveira 67cb7ef673 Fix test_plain_unittest_does_not_support_async on pypy3
Fix #7624
2020-08-05 15:24:08 -03:00
Bruno Oliveira 44cd8a3a86
Demonstrate that plain unittest does not support async tests (#7607)
Co-authored-by: Ran Benita <ran@unusedvar.com>
2020-08-04 19:37:41 -03:00
Ran Benita fbf251f11d Improve typing of reports' longrepr field 2020-08-04 22:52:24 +03:00
Ran Benita 62ddf7a0e5 resultlog: add missing type annotations 2020-08-04 22:45:46 +03:00
Ran Benita 9ab14c6d9c typing: set warn_unreachable
This makes mypy raise an error whenever it detects code which is
statically unreachable, e.g.

    x: int
    if isinstance(x, str):
        ... # Statement is unreachable  [unreachable]

This is really neat and finds quite a few logic and typing bugs.

Sometimes the code is intentionally unreachable in terms of types, e.g.
raising TypeError when a function is given an argument with a wrong
type. In these cases a `type: ignore[unreachable]` is needed, but I
think it's a nice code hint.
2020-08-04 09:59:46 +03:00
Ran Benita b8471aa527 testing: fix some docstring issues
In preparation for enforcing some docstring lints.
2020-08-03 10:10:43 +03:00
Ran Benita be656dd4e4 typing: set disallow_any_generics
This prevents referring to a generic type without filling in its generic
type parameters.

The FixtureDef typing might need some more refining in the future.
2020-08-01 20:39:15 +03:00