Commit Graph

1933 Commits

Author SHA1 Message Date
Bruno Oliveira 5dd987e10f
Merge pull request #6999 from bluetech/simplify-fixture-compat 2020-05-08 08:06:35 -03:00
Bruno Oliveira 9657798c12
Merge pull request #7158 from CarycaKatarzyna/issue_7076 2020-05-08 07:56:03 -03:00
Ran Benita 857b5c9ccd
Merge pull request #7185 from bluetech/sigpipe
Handle SIGPIPE/BrokenPipeError in pytest's CLI
2020-05-08 13:32:34 +03:00
Ran Benita 73448f265d Handle EPIPE/BrokenPipeError in pytest's CLI
Running `pytest | head -1` and similar causes an annoying error to be
printed to stderr:

    Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
    BrokenPipeError: [Errno 32] Broken pipe

(or possibly even a propagating exception in older/other Python versions).

The standard UNIX behavior is to handle the EPIPE silently. To
recommended method to do this in Python is described here:
https://docs.python.org/3/library/signal.html#note-on-sigpipe

It is not appropriate to apply this recommendation to `pytest.main()`,
which is used programmatically for in-process runs. Hence, change
pytest's entrypoint to a new `pytest.console_main()` function, to be
used exclusively by pytest's CLI, and add the SIGPIPE code there.

Fixes #4375.
2020-05-08 12:51:02 +03:00
Anthony Sottile b238845d0f Fix _is_setup_py for files encoded differently than locale 2020-05-07 13:14:58 -07:00
Ran Benita 65963d2066 warnings: speed up work done in catch_warnings_for_item()
When setting up the warnings capture, filter strings (with the general
form `action:message:category:module:line`) are collected from the
cmdline, ini and item and applied. This happens for every test and other
cases.

To apply a string it needs to be parsed into a tuple, and it turns out
this is slow. Since we already vendor the parsing code from Python's
warnings.py, we can speed it up by caching the result. After splitting
the parsing part from the applying part, the parsing is pure and is
straightforward to cache.

An alternative is to parse ahead of time and reuse the result, however
the caching solution turns out cleaner and more general in this case.

On this benchmark:

    import pytest
    @pytest.mark.parametrize("x", range(5000))
    def test_foo(x): pass

Before:

============================ 5000 passed in 14.11s =============================
         14365646 function calls (13450775 primitive calls) in 14.536 seconds

After:

============================ 5000 passed in 13.61s =============================
         13290372 function calls (12375498 primitive calls) in 14.034 seconds
2020-05-07 12:19:13 +03:00
Ran Benita 81da5dac48
Merge pull request #7122 from bluetech/matcher-parser
Stop using Python's eval() for -m and -k
2020-05-07 09:23:29 +03:00
Bruno Oliveira 6c2d358918
Merge pull request #7135 from pytest-dev/terminalwriter 2020-05-06 18:26:44 -03:00
Bruno Oliveira 4d439760ad
Merge pull request #7168 from nicoddemus/saferepr-getattr-fail-7145 2020-05-06 17:57:38 -03:00
Ran Benita d16ae0bbdc
Merge pull request #7171 from bluetech/code-import-cycles
code: fix import cycles between code.py and source.py
2020-05-06 18:15:32 +03:00
Ran Benita 89eee90b5f python: optimize PythonCollector.collect 2020-05-06 12:05:04 +03:00
Ran Benita b90f34569f nodes: micro-optimize Node attribute access 2020-05-06 11:58:30 +03:00
Ran Benita 5702c86f4c nodes: micro-optimize hash(node)
Turns out it's called alot, and saving the function call makes it
faster.
2020-05-06 11:58:30 +03:00
Ran Benita fcc473ab1c Use dict instead of OrderedDict on Python 3.7
OrderedDict is quite a bit heavier than just a dict.
2020-05-06 11:58:28 +03:00
Ran Benita 69143fe5b0 code: fix import cycles between code.py and source.py
These two files were really intertwined. Make it so code.py depends on
source.py without a reverse dependency.

No functional changes.
2020-05-06 11:01:32 +03:00
Bruno Oliveira d0022b5a13 'saferepr' handles classes with broken __getattribute__
Fix #7145
2020-05-05 19:22:39 -03:00
Ran Benita 94400a68b4 terminal: fix non-deterministic warning summary order in Python 3.5
In Python 3.5, collections.Counter() does not preserve insertion order.
2020-05-05 23:08:44 +03:00
Ran Benita 4b91617002
Merge pull request #7020 from blueyed/fix-warnings-summary
Fix warnings summary
2020-05-05 21:58:57 +03:00
Ran Benita b4be6cd4b7
Merge pull request #6765 from blueyed/capture-refactor-1
Refactor Capture classes: move/rename CaptureIO classes
2020-05-05 21:46:18 +03:00
Daniel Hahler 8cae78a18b Fix warnings summary
- replace "tests with warnings" with just warnings: they a) might not
  come from a test in the first place, and b) a single test might have
  multiple warnings.
- fix usage of (unused) argument to `collapsed_location_report`

Co-authored-by: Ran Benita <ran@unusedvar.com>
2020-05-05 21:40:34 +03:00
Ran Benita d7d627b1e8
Merge pull request #7064 from blueyed/fix-_printcollecteditems-doc-upstream
Fix/improve printing of docs for collected items
2020-05-05 21:31:21 +03:00
Daniel Hahler 7647d1c836 Move Capture classes from compat to capture and improve naming
Move {Passthrough,CaptureIO} to capture module, and rename Passthrough
-> Tee to match the existing terminology.

Co-authored-by: Ran Benita <ran@unusedvar.com>
2020-05-05 21:24:59 +03:00
Ran Benita 46d768503e
Merge pull request #6839 from blueyed/typing-get_dirs_from_args-parseconfig
typing: get_dirs_from_args, parseconfig
2020-05-05 21:11:26 +03:00
Ran Benita 85732e2b08
Merge pull request #6993 from blueyed/doc-pytest_collection-upstream
[WIP] doc: pytest_collection: has to set `session.items`
2020-05-05 21:05:56 +03:00
Bruno Oliveira 16a44823eb Use reportinfo() instead of location in skipping message 2020-05-04 19:50:40 -03:00
Katarzyna 402ee6fb9d Relative path to invocationdir instead rootdir. 2020-05-03 22:56:38 +02:00
Bruno Oliveira 80e5098408
Merge pull request #7155 from kerizane/make_numbered_dir 2020-05-03 09:48:36 -03:00
Keri Volans 678440e46d 7018: Use internal version of make_numbered_dir 2020-05-03 10:45:06 +01:00
Bruno Oliveira 5c2e96c0e6 Fix cleanup functions not being invoked on test failures
Also delay calling tearDown() when --pdb is given, so users still have
access to the instance variables (which are usually cleaned up during tearDown())
when debugging.

Fix #6947
2020-05-02 15:26:55 -03:00
Bruno Oliveira 2b51ed46d5
Merge pull request #7153 from nicoddemus/xunit-warning-update 2020-05-02 13:31:34 -03:00
Bruno Oliveira 095a195d71 Improve docs about junit_family warning message
From discussion in #6178
2020-05-02 13:01:15 -03:00
Bruno Oliveira 0b78983197
Merge pull request #7141 from bluetech/rm-nonzero 2020-05-02 10:00:35 -03:00
Bruno Oliveira e3643751bc
Merge pull request #7144 from nicoddemus/async-testcase-7110 2020-05-01 16:44:10 -03:00
Bruno Oliveira fd2f172258 Let unittest frameworks deal with async functions
Instead of trying to handle unittest-async functions in pytest_pyfunc_call,
let the unittest framework handle them instead.

This lets us remove the hack in pytest_pyfunc_call, with the upside that
we should support any unittest-async based framework.

Also included 'asynctest' as test dependency for py37-twisted, and renamed
'twisted' to 'unittestextras' to better reflect that we install 'twisted' and
'asynctest' now.

This also fixes the problem of cleanUp functions not being properly called
for async functions.

Fix #7110
Fix #6924
2020-05-01 15:10:28 -03:00
Bruno Oliveira 7f5978c34c Allow File.from_parent to forward custom parameters to the constructor 2020-05-01 11:00:52 -03:00
Ran Benita a718ad6363 Stop using Python's eval() for -m and -k
Previously, the expressions given to the `-m` and `-k` options were
evaluated with `eval`. This causes a few issues:

- Python keywords cannot be used.

- Constants like numbers, None, True, False are not handled correctly.

- Various syntax like numeric operators and `X if Y else Z` is supported
  unintentionally.

- `eval()` is somewhat dangerous for arbitrary input.

- Can fail in many ways so requires `except Exception`.

The format we want to support is quite simple, so change to a custom
parser. This fixes the issues above, and gives us full control of the
format, so can be documented comprehensively and even be extended in the
future if we wish.
2020-05-01 12:59:06 +03:00
Ran Benita 409ffcef17 Remove a couple Python 2 __nonzero__ definitions
It's called __bool__ in Python 3.
2020-05-01 11:41:21 +03:00
Ran Benita d8558e87c5 terminalwriter: clean up markup function a bit 2020-04-30 16:44:03 +03:00
Ran Benita 414a87a53f config/argparsing: use our own get_terminal_width() 2020-04-30 16:44:03 +03:00
Ran Benita d5584c7207 terminalwriter: compute width_of_current_line lazily
Currently this property is computed eagerly, which means
get_line_width() is computed on everything written, but that is a slow
function.

Compute it lazily, so that get_line_width() only runs when needed.
2020-04-30 16:44:03 +03:00
Ran Benita dd32c72ff0 terminalwriter: remove unused property chars_on_current_line 2020-04-30 16:44:03 +03:00
Ran Benita 1bc4170e63 terminalwriter: don't flush implicitly; add explicit flushes
Flushing on every write is somewhat expensive.

Rely on line buffering instead (if line buffering for stdout is
disabled, there must be some reason...), and add explicit flushes when
not outputting lines.

This is how regular `print()` e.g. work so should be familiar.
2020-04-30 16:44:03 +03:00
Ran Benita d9b43647b7 terminalwriter: inline function _update_chars_on_current_line 2020-04-30 16:44:03 +03:00
Ran Benita 8e04d35a33 terminalwriter: remove unneeded hasattr use 2020-04-30 16:44:03 +03:00
Ran Benita e8fc5f99fa terminalwriter: add type annotations 2020-04-30 16:44:03 +03:00
Ran Benita f6564a548a terminalwriter: remove win32 specific code in favor of relying on colorama
On Windows we already depend on colorama, which takes care of all of
this custom code on its own.
2020-04-30 16:44:03 +03:00
Ran Benita 8d2d1c40f8 terminalwriter: inline function _escaped
Doesn't add much.
2020-04-30 16:44:03 +03:00
Ran Benita 66ee755649 terminalwriter: remove TerminalWriter's stringio argument
Had a mark indicating it should be removed, and I agree, it's better to
just use the `file` argument.
2020-04-30 16:44:02 +03:00
Ran Benita 94a57d2353 io: combine _io.TerminalWriter and _io.terminalwriter.TerminalWriter
Previously it extended an external type but now it come move to the type
itself.
2020-04-30 16:44:02 +03:00
Ran Benita dac05ccd9a terminalwriter: remove support for passing callable as file in TerminalWriter
Not used.
2020-04-30 16:44:02 +03:00
Ran Benita 0528307ebf terminalwriter: remove unused function TerminalWriter.reline 2020-04-30 16:44:02 +03:00
Ran Benita a6819726cd terminalwriter: remove unused function ansi_print 2020-04-30 16:44:02 +03:00
Ran Benita b6cc90e0af terminalwriter: remove support for writing bytes directly
It is not used and slows things down.
2020-04-30 16:44:02 +03:00
Ran Benita 9a59970cad terminalwriter: optimize get_line_width() a bit
This function is called a lot when printing a lot of text, and is very
slow -- this speeds it up a bit.
2020-04-30 16:44:02 +03:00
Ran Benita 6c1b6a09b8 terminalwriter: simplify get_terminal_width()
The shutil.get_terminal_size() handles everything this did already.
2020-04-30 16:44:02 +03:00
Ran Benita c749e44efc terminalwriter: remove custom win32 screen width code
Python 3 does this on its own so we can use the shared code:
bcf2b59fb5
2020-04-30 16:44:02 +03:00
Ran Benita 1d596b27a7 terminalwriter: move Win32ConsoleWriter definition under win32 conditional
This way non-Windows platforms skip it. It also uses things defined
inside the `if`.
2020-04-30 16:44:02 +03:00
Ran Benita 5e2d820308 terminalwriter: fix lints 2020-04-30 16:44:02 +03:00
Ran Benita 3014d9a3f7 terminalwriter: auto-format 2020-04-30 16:43:55 +03:00
Ran Benita 276405a039 terminalwriter: vendor TerminalWriter from py
Straight copy from py 1.8.1. Doesn't pass linting yet.
2020-04-30 16:43:54 +03:00
Ran Benita c72a1b2933 config: replace usage of py.io.dupfile
As part of the effort to remove uses of `py`.
2020-04-29 18:04:37 +03:00
Ran Benita e5d6e58625
Merge pull request #7130 from bluetech/cacheprovider-nf-set
cacheprovider: speed up NFPlugin when --nf is not enabled
2020-04-29 17:28:20 +03:00
Ran Benita 4a42afdc2f cacheprovider: speed up NFPlugin when --nf is not enabled
The code used an O(n^2) loop. Replace list with set to make it O(n).

For backward compatibility the filesystem cache still remains a list.

On this test:

    import pytest
    @pytest.mark.parametrize("x", range(5000))
    def test_foo(x): pass

run with `pytest --collect-only`:

Before: 0m1.251s
After:  0m0.921s
2020-04-29 13:02:38 +03:00
Ran Benita 9bf3efb8e9
Merge pull request #7120 from bluetech/update-dev
pre-commit: update some tool revisions
2020-04-25 23:07:15 +03:00
Ran Benita 3cd97d50f9 pre-commit: update pyupgrade 1.18.0 -> 2.2.1 2020-04-24 21:57:38 +03:00
Ran Benita 38c9d59ddc pre-commit: update blacken-docs 1.0.0 -> 1.6.0 2020-04-24 21:53:37 +03:00
Ran Benita 289e6c1d36
Update src/_pytest/deprecated.py
Co-Authored-By: Ronny Pfannschmidt <opensource@ronnypfannschmidt.de>
2020-04-24 21:37:08 +03:00
Ran Benita 907e29a47b fixtures: deprecate pytest._fillfuncargs function
This function is exposed and kept alive for the oejskit plugin which is
abandoned and no longer works with recent plugins, so let's prepare to
completely remove it.
2020-04-24 18:54:46 +03:00
Ran Benita 7d5f5a8785
Merge pull request #7050 from blueyed/fix-doc
doc: internal: remove references to old "newinterpret" module
2020-04-17 17:11:31 +03:00
Ran Benita 731c340be6
Merge pull request #7058 from blueyed/doc-store
doc: minor fixes for Store
2020-04-17 17:10:35 +03:00
Katarzyna Król 7789b51acb
Issue 4677 - always relative path in skip report (#6953) 2020-04-17 08:28:36 +03:00
Simon K de6c28ed1f
Improve error handling around yieldctx fixtures which do not yield a value (#7083) 2020-04-15 12:17:13 +03:00
Ran Benita 9ddf38369d
Merge pull request #7069 from bluetech/faulthandler-doc-windows
Remove note saying faulthandler_timeout is not available on Windows
2020-04-14 10:24:28 +03:00
Simon K f479cbce10
Document pytester fixtures so --fixtures makes more sense (#7080) 2020-04-13 20:58:50 +03:00
Ran Benita c08cff3770
Merge pull request #7082 from symonk/4583-better-ux-with-eval-fails
Gracefully handle eval() failure(s) for marker expressions
2020-04-13 17:03:37 +03:00
symonk 251e8f212e refactor mark tests, widen catching and make error msg more concise 2020-04-13 14:25:01 +01:00
symonk 87edc09dea Gracefully handle eval() failure(s) for marker expressions 2020-04-13 13:25:06 +01:00
Andreas Maier c9386ada29 Squash: Resolved 2nd round of review comments 2020-04-12 13:19:45 +02:00
Andreas Maier b2582b0314 Squash: Applied review comments 2020-04-12 12:12:00 +02:00
Andreas Maier 869c089887 Fixes #7077: Added & improved docs for repr_failure() in Node & Collector 2020-04-12 09:28:40 +02:00
Ran Benita b553ce54c8 Remove note saying faulthandler_timeout is not available on Windows
I think it is available in all Python versions we support, but at least
since Python 3.7 the docs[0] say:

    Changed in version 3.7: This function is now always available.

so let's just remove the note.

[0] https://docs.python.org/3/library/faulthandler.html#faulthandler.dump_traceback_later
2020-04-10 14:46:15 +03:00
Ronny Pfannschmidt 244c8e4a13
Merge pull request #6680 from RonnyPfannschmidt/fix-6294-more-docs-for-fromparent
doc: more docs for from_parent
2020-04-10 13:10:40 +02:00
Ronny Pfannschmidt 19c243f0fa
Merge pull request #6285 from earonesty/patch-1
Add _pytest.fixtures.FixtureLookupError to top level import
2020-04-10 07:58:58 +02:00
Ronny Pfannschmidt c5d636a1b2
Merge pull request #6992 from pytest-dev/revert-6767-tmpdir-cleanup-upstream
Revert "tmpdir: clean up indirection via config for factories"
2020-04-10 07:49:52 +02:00
Ronny Pfannschmidt 1eb2b45db5 Revert "tmpdir: clean up indirection via config for factories (#6767)"
This reverts commit 8a1633c3b4.

+ add changelog
2020-04-10 07:18:54 +02:00
Daniel Hahler 0b5d2ff526 Fix/improve printing of docs for collected items 2020-04-09 21:49:30 +02:00
Daniel Hahler accea46fa1 doc: minor fixes for Store 2020-04-09 17:08:47 +02:00
Daniel Hahler 413ca8a4d0
faulthandler: trylast=True (#7025)
It should happen as late as possible before the test runs.

Ref: https://github.com/pytest-dev/pytest/issues/7022
2020-04-09 08:53:35 +02:00
Daniel Hahler a98a62723e revisit 2020-04-09 01:57:46 +02:00
Daniel Hahler ad4c1071d9 doc: internal: remove references to old "newinterpret" module
This has been merged into the (only) assertrewrite mode.
2020-04-09 01:40:23 +02:00
Ran Benita 4344c61731 Slightly improve Mark and MarkDecorator documentation
Mostly I wanted to get rid of mentions of "MarkItem" which is something
that no longer exists, but I improved a little beyond that and annotated
some simple types.
2020-04-07 22:14:47 +03:00
Daniel Hahler ce806001b0
Fix doc for `numbered` arg with `TempPathFactory.mktemp` (#7014) 2020-04-07 11:44:54 +02:00
Ran Benita 259e5d0610
Merge pull request #7034 from bluetech/fix-testdurations-flaky
Fix flaky TestDurations test
2020-04-07 09:39:02 +03:00
Daniel Hahler 1fd14685c5
doc: document inversed lines with terminal report hooks (#7016)
It was surprising that `tryfirst=True` would not result in lines being
added to the beginning with `pytest_report_header`.
This is due to lines being reversed, and therefore the same applies to
`pytest_report_collectionfinish`.
2020-04-07 08:08:28 +02:00
Ran Benita c3e6e2e8c8 Fix flaky TestDurations test
TestDurations tests the `--durations=N` functionality which reports N
slowest tests, with durations <= 0.005s not shown by default.

The test relies on real time.sleep() (in addition to the code which uses
time.perf_counter()) which makes it flaky and inconsistent between
platforms.

Instead of trying to tweak it more, make it use fake time instead. The
way it is done is a little hacky but seems to work.
2020-04-06 23:49:46 +03:00
Ran Benita 1ce30fd38f Document the pytest_report_teststatus hook better and test uncovered functionality
This hook has some functionality to provide explicit markup for the test
status. It seemed unused and wasn't tested, so I was tempted to remove
it, but I found that the pytest-rerunfailures plugin uses it, so
document it and add a test instead.
2020-04-04 17:27:59 +03:00
Ran Benita 4a324ce920 Remove unused defaultfuncargprefixmarker
Unused since 1e80a9cb34.
2020-04-04 14:34:40 +03:00
Daniel Hahler 20f6331afd
Fix TerminalRepr instances to be hashable (#6988)
pytest-xdist assumes `ExceptionChainRepr` is hashable.

Fixes https://github.com/pytest-dev/pytest/issues/6925.
Fixes https://github.com/pytest-dev/pytest-xdist/issues/515.
2020-04-03 00:56:53 +02:00
Ran Benita 03451c397f Simplify positional arguments compatibility code in pytest.fixture()
The dynamic scope feature added in 10bf6aac76
necessitated some wrangling of arguments in pytest.fixture(). In
particular, it deprecated positional arguments in favor of keyword-only
arguments, while keeping backward compatibility.

The way it did this avoided some code duplication but ended up being
quite hard to follow and to annotate with types.

Replace it with some straightforward code, which is not very DRY but is
simple and easy to remove when the time comes.
2020-04-02 14:52:58 +03:00
Daniel Hahler 354602abe6
Update src/_pytest/hookspec.py
Co-Authored-By: Bruno Oliveira <nicoddemus@gmail.com>
2020-04-02 12:01:43 +02:00