Commit Graph

12483 Commits

Author SHA1 Message Date
Ran Benita 075903dafa logging: simplify log-file handling
- Instead of making it optional, always set up a handler, but possibly
  going to /dev/null. This simplifies the code by removing a lot of
  conditionals. It also can replace the NullHandler() we already add.

- Change `set_log_path` to just change the stream, instead of recreating
  one. Besides plugging a resource leak, it enables the next item.

- Remove the capturing_logs from _runtest_for, since it sufficiently
  covered by the one in pytest_runtestloop now, which wraps all other
  _runtest_for calls.

The first item alone would have had an adverse performance impact, but
the last item removes it.
2020-05-17 21:41:37 +03:00
Ran Benita e48ac692de logging: optimize catching_logs slightly
Remove usage of `@contextmanager` as it is a bit slower than
hand-rolling, and also disallows re-entry which we want to use.

Removing protections around addHandler()/removeHandler(), because
logging already checks that internally.
2020-05-17 20:42:06 +03:00
Ran Benita eceb28e4be logging: set formatter on handler creation, not in catching_logs
Conceptually it doesn't check per catching_logs (and catching_logs
doesn't restore the older one either). It is just something that is
defined for each handler once.
2020-05-17 20:42:06 +03:00
Ran Benita ce0f218793 logging: yield from _runtest_for instead of contextmanager
Avoid the slight overhead of contextmanager.
2020-05-17 20:42:06 +03:00
Ran Benita 9effbe7425 logging: inline _runtest_for_main into _runtest_for
This avoids a little bit of overhead, and makes the code a bit clearer
too.
2020-05-17 20:42:06 +03:00
Ran Benita ac6c02f1e2 logging: use item's store for private attributes
This makes things type-safe and properly private.
2020-05-17 20:42:05 +03:00
Bruno Oliveira abbd97f917
Merge pull request #7222 from nicoddemus/remove-key-error-conftest-exception 2020-05-17 11:43:43 -03:00
Bruno Oliveira c26f389c09 Refactor handling of non-top-level pytest_plugins handling
Decided to move the 'if' logic together with the error message, as this leaves
the _importconftest method cleaner.
2020-05-17 11:26:30 -03:00
Bruno Oliveira 9e1e7fcabe Use a nice string repr for ConftestImportFailure
The default message is often hard to read:

    E   _pytest.config.ConftestImportFailure: (local('D:\\projects\\pytest\\.tmp\\root\\foo\\conftest.py'), (<class 'RuntimeError'>, RuntimeError('some error',), <traceback object at 0x000001CCC3E39348>))

Using a shorter message is better:

    E   _pytest.config.ConftestImportFailure: RuntimeError: some error (from D:\projects\pytest\.tmp\root\foo\conftest.py)

And we don't really lose any information due to exception chaining.
2020-05-17 11:26:30 -03:00
Bruno Oliveira 5eaebc1900 Remove one of the tracebacks from conftest import failures
This removes the KeyError from the traceback chain when an
conftest fails to import:

    return self._conftestpath2mod[key]
    E   KeyError: WindowsPath('D:/projects/pytest/.tmp/root/foo/conftest.py')

    During handling of the above exception, another exception occurred:
    ...
        raise RuntimeError("some error")
    E   RuntimeError: some error

    During handling of the above exception, another exception occurred:
    ...
    E   _pytest.config.ConftestImportFailure: (...)

By slightly changing the code, we can remove the first chain, which is often
very confusing to users and doesn't help with anything.

Fix #7223
2020-05-17 11:26:02 -03:00
Bruno Oliveira c5b367b4f4 Package.name now contains only basname of the package
Previously it contained the entire path, which made '-k' match
against any name in the full path of the package.

Fix #7040
2020-05-16 15:04:07 -03:00
Daniel Hahler 3d3b9511fd -k should not match session name
Fixes https://github.com/pytest-dev/pytest/issues/7040.
2020-05-16 14:15:57 -03:00
Bruno Oliveira d4dfe863c9
Merge pull request #7221 from nicoddemus/fix-release-on-comment 2020-05-16 13:46:46 -03:00
Bruno Oliveira e1becae24c Fix errors introduced by #6911
Somehow I've missed the failures while merging, totally my fault.
2020-05-16 13:42:39 -03:00
Bruno Oliveira f506682abe
Merge pull request #6919 from nicoddemus/backport-6914 2020-05-16 12:42:15 -03:00
Bruno Oliveira d9f24bca9c
Merge pull request #6935 from gdhameeja/Fix-6911 2020-05-16 12:08:23 -03:00
Bruno Oliveira 68acd10349
Merge pull request #6991 from blueyed/fix-lf-cmdline-args-upstream 2020-05-15 18:49:25 -03:00
Daniel Hahler d530d70128 Fix regressions with `--lf` plugin
Only filter with known failures, and explicitly keep paths of passed
arguments.

This also displays the "run-last-failure" status before collected files,
and does not update the cache with "--collect-only".

Fixes https://github.com/pytest-dev/pytest/issues/6968.
2020-05-15 18:35:38 -03:00
Ran Benita f0f552d60c
Merge pull request #6283 from felixn/master
Create LogCaptureHandler if necessary (closes #6240)
2020-05-15 09:13:57 +03:00
Ran Benita 2ac28f6c65
Merge pull request #7205 from lancelote/7126
Issue 7126 - "saferepr" to avoid BytesWarning when using --setup-show
2020-05-14 16:04:22 +03:00
Felix Nieuwenhuizen 4dfc461036 Create LogCaptureHandler if necessary (closes #6240) 2020-05-13 20:38:14 +02:00
Zac Hatfield-Dodds 6df0b9c41a
Merge pull request #7213 from hrichards/clarify_docs_on_nose_yield-based_tests 2020-05-13 12:38:45 +10:00
Ran Benita 07c8e0cc7f
Merge pull request #7211 from bluetech/expr-jit
mark: compile -k/-m expression once, reuse for all matches
2020-05-12 13:21:41 +03:00
Ran Benita c714f05ad7 mark: reuse compiled expression for all items in -k/-m
The previous commit made this possible, so utilize it.

Since legacy.py becomes pretty bare, I inlined it into __init__.py. I'm
not sure it's really "legacy" anyway!

Using a simple 50000 items benchmark with `--collect-only -k nomatch`:

Before (two commits ago):

   ======================== 50000 deselected in 10.31s =====================
         19129345 function calls (18275596 primitive calls) in 10.634 seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.001    0.001    2.270    2.270 __init__.py:149(pytest_collection_modifyitems)
        1    0.036    0.036    2.270    2.270 __init__.py:104(deselect_by_keyword)
    50000    0.055    0.000    2.226    0.000 legacy.py:87(matchkeyword)

After:

   ======================== 50000 deselected in 9.37s =========================
         18029363 function calls (17175972 primitive calls) in 9.701 seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    1.394    1.394 __init__.py:239(pytest_collection_modifyitems)
        1    0.057    0.057    1.393    1.393 __init__.py:162(deselect_by_keyword)

The matching itself can be optimized more but that's a different story.
2020-05-12 12:55:37 +03:00
Ran Benita 622c4ce02e mark/expression: support compiling once and reusing for multiple evaluations
In current pytest, the same expression is matched against all items. But
it is re-parsed for every match.

Add support for "compiling" an expression and reusing the result. Errors
may only occur during compilation.

This is done by parsing the expression into a Python `ast.Expression`,
then `compile()`ing it into a code object. Evaluation is then done using
`eval()`.

Note: historically we used to use `eval` directly on the user input --
this is not the case here, the expression is entirely under our control
according to our grammar, we just JIT-compile it to Python as a
(completely safe) optimization.
2020-05-12 12:53:12 +03:00
Ran Benita 952762207a
Merge pull request #7121 from bluetech/update-flake8
pre-commit: update flake8 3.7.7 -> 3.8.1
2020-05-12 12:51:46 +03:00
Ran Benita 645aaa728d python_api: reduce scope of a `except BaseException` in ApproxNumpy
I'm not sure if it can even raise at all, but catching BaseException
would swallow ctrl-C and such and is definitely inappropriate here.
2020-05-12 12:07:56 +03:00
Ran Benita 23c9856857 Remove no longer needed noqa's 2020-05-12 09:29:47 +03:00
Ran Benita 59a12e9ab3 Replace bare `except`s with `except BaseException`
Mostly I wanted to remove uses of `noqa`.

In Python 3 the two are the same.
2020-05-12 09:29:47 +03:00
Ran Benita d1534181c0 pre-commit: upgrade flake8 3.7.7 -> 3.8.1
New errors:

    testing/test_setupplan.py:104:15: E741 ambiguous variable name 'l'
    testing/test_setupplan.py:107:15: E741 ambiguous variable name 'l'
    extra/get_issues.py:48:29: E741 ambiguous variable name 'l'
    testing/test_error_diffs.py:270:32: E741 ambiguous variable name 'l'

Not so sure about it but easier to just fix.

But more importantly, is a large amount of typing-related issues there
were fixed which necessitated noqa's which can now be removed.
2020-05-12 09:29:40 +03:00
Hunter Richards 06b6d5b1d5 Combine all mentions of `yield` into "Unsupported" section 2020-05-11 15:36:08 -05:00
Ran Benita 15d5e8cd97
Merge pull request #7210 from bluetech/minus-k-deprecations
mark: deprecate a couple undocumented -k syntaxes
2020-05-11 20:57:35 +03:00
Ran Benita c4f9eaa5de mark: deprecate a couple undocumented -k syntaxes
The `-k '-expr'` syntax is an old alias to `-k 'not expr'`. It's also
not a very convenient to have syntax that start with `-` on the CLI.
Deprecate it and suggest replacing with `not`.

---

The `-k 'expr:'` syntax discards all items until the first match and
keeps all subsequent, e.g. `-k foo` with

    test_bar
    test_foo
    test_baz

results in `test_foo`, `test_baz`. That's a bit weird, so deprecate it
without a replacement. If someone complains we can reconsider or devise
a better alternative.
2020-05-11 16:07:52 +03:00
Zac Hatfield-Dodds 0ba6e1f74e
Merge pull request #7209 from pwilczynskiclearcode/master
#7138 Docs improvement: Apply indirect on particular arguments
2020-05-11 22:10:28 +10:00
Paweł Wilczyński dbfc629698 #7138 Docs improvement: Apply indirect on particular arguments 2020-05-11 13:26:16 +02:00
Pavel Karateev 8bd3f1a72b
Better test method name
Co-authored-by: Ran Benita <ran@unusedvar.com>
2020-05-10 16:59:20 +03:00
Pavel Karateev 17857b67df
Better changelog wording
Co-authored-by: Ran Benita <ran@unusedvar.com>
2020-05-10 16:58:22 +03:00
Pavel Karateev 184528d0c2 Fix tests to expected repr output 2020-05-10 13:06:36 +03:00
Pavel Karateev 7b196747dd Use saferepr for all types 2020-05-10 12:47:26 +03:00
Pavel Karateev 6b26f0f890 Rename test method and reference issue 2020-05-10 12:19:52 +03:00
Pavel Karateev feb7a5f0d1 Omit internal solution details 2020-05-10 12:11:59 +03:00
Pavel Karateev a2280d39ec #7126, use past tense in changelog 2020-05-09 14:14:23 +03:00
Pavel Karateev 903e2ab6ee Fix #7126 - saferepr for bytes params
bytes parametrize parameters cause error when --setup-show is used
and Python is called with -bb flag
2020-05-09 13:57:17 +03:00
Bruno Oliveira 5e7f1ab4bf
Merge pull request #7201 from nicoddemus/bruno-tidelift 2020-05-08 13:28:14 -03:00
Anthony Sottile abb047f71b
Add asottile as well
(went for alphabetical order, hope that's ok!)
2020-05-08 08:57:22 -07:00
Bruno Oliveira 97a0239aad Add myself to TIDELIFT 2020-05-08 12:36:40 -03:00
Bruno Oliveira 741a8b8023
Merge pull request #7195 from nicoddemus/cherry-pick-release 2020-05-08 09:09:48 -03:00
Bruno Oliveira b02b0b610b
Merge pull request #6989 from blueyed/fix-test 2020-05-08 09:04:59 -03:00
Bruno Oliveira e6151cd8d0 Cherry-pick release 5.4.2 2020-05-08 09:03:21 -03:00
Bruno Oliveira 5dd987e10f
Merge pull request #6999 from bluetech/simplify-fixture-compat 2020-05-08 08:06:35 -03:00