summaryrefslogtreecommitdiff
path: root/src/_pytest
AgeCommit message (Collapse)Author
2020-11-24fix typo (#8069)Jürgen Gmach
2020-11-23fix mock_timing fixture name (typo) in timing.pySimon K
2020-11-21Add unraisableexception and threadexception pluginsRan Benita
2020-11-21Add str() support to LineMatcher (#8050)Maximilian Cosmo Sitter
2020-11-21permit node to warn with any warning type, not just PytestWarning (#8052)Simon K
Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
2020-11-20pytester: always close stdin pipe in pytester.run()Ran Benita
If the user passed stdin=PIPE for some reason, they have no way to close it themselves since it is not exposed.
2020-11-19Link mentioned functions instead of using literals (#8045)Tim Hoffmann
2020-11-19Call Python 3.8 doClassCleanups (#8033)Petter Strandmark
2020-11-19Prefix contextmanagers with module name in doc examples (#8044)Tim Hoffmann
* Prefix contextmanagers with module name in doc examples * Import pytest explicitly for doctests Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
2020-11-17stop assigning nextline if its potentially not usedsymonk
2020-11-14Merge pull request #8014 from bluetech/pyc-pep552Ran Benita
assertion/rewrite: write pyc's according to PEP-552 on Python>=3.7
2020-11-14assertion/rewrite: write pyc's according to PEP-552 on Python>=3.7Ran Benita
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-13Export types of builtin fixture for type annotationsRan Benita
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-13pytester: split asserts to a separate plugin, don't rewrite pytester itselfRan Benita
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-13Merge pull request #8022 from bluetech/doctest-initRan Benita
main: fix only one doctest collected on pytest --doctest-modules __init__.py
2020-11-11Add 'node_modules' to norecursedirsAdam Johnson
Fixes #8023.
2020-11-10main: fix only one doctest collected on pytest --doctest-modules __init__.pyRan Benita
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-09Merge pull request #8006 from bluetech/export-MonkeyPatchRan Benita
Export MonkeyPatch as pytest.MonkeyPatch
2020-11-09Export MonkeyPatch as pytest.MonkeyPatchRan Benita
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-08Rename _make_plural to pluralizeBruno Oliveira
A bit shorter and a better name, IMHO.
2020-11-08Improve summary stats when using '--collect-only' (#7875)Hugo Martins
Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
2020-11-08Merge pull request #8005 from bluetech/pytest-importRan Benita
Stop importing `pytest` to avoid upcoming import cycles
2020-11-07Stop importing `pytest` to avoid upcoming import cyclesRan Benita
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-07fixtures: deprecate pytest.yield_fixture()Ran Benita
2020-11-07test_collection.py migrate from testdir to Pytester (#8003)Sanket Duthade
2020-11-06Deprecate --strict (#7985)Bruno Oliveira
Fix #7530
2020-11-01unittest: fix quadratic behavior in collection of unittests using ↵Ran Benita
setUpClass/setup_method This is similar to 50114d4731876dae; I missed that unittest does the same thing.
2020-11-01Merge pull request #7980 from bluetech/code-changesRan Benita
code: a few minor improvements
2020-10-31Merge pull request #7956 from csernazs/fix-7951Ran Benita
Fix handling recursive symlinks
2020-10-31Fix handling recursive symlinksCserna Zsolt
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-31Merge pull request #7553 from tirkarthi/namedtuple-diffRan Benita
Add support to display field names in namedtuple diffs.
2020-10-31Merge pull request #7979 from nicoddemus/metafunc-refBruno Oliveira
Add FunctionDefinition to the reference docs
2020-10-31Add support to display field names in namedtuple diffs.Karthikeyan Singaravelan
2020-10-31pathlib: fix symlinked directories not followed during collectionRan Benita
2020-10-31Add FunctionDefinition to the reference docsBruno Oliveira
Fix #7968
2020-10-31code: simplify Code constructionRan Benita
2020-10-31code: use properties for derived attributes, use slotsRan Benita
Make the objects more light weight. Remove unused properties.
2020-10-30assertion/util: remove unhelpful `type_fns` indirectionRan Benita
It doesn't serve any purpose that I am able to discern.
2020-10-30#7938 - [Plugin: Stepwise][Enhancements] Refactoring, smarter registration & ↵Simon K
--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-28Increase temp dir deletion period to 3 days (#7914)Vasilis Gerakaris
Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
2020-10-25pytester: workaround issue causing spawn to crash or hangRan Benita
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-25Merge pull request #7931 from bluetech/xunit-quadratic-2Ran Benita
fixtures: fix quadratic behavior in the number of autouse fixtures
2020-10-25fixtures: fix quadratic behavior in the number of autouse fixturesRan Benita
It turns out all autouse fixtures are kept in a global list, and thinned out for a particular node using a linear scan of the entire list each time. Change the list to a dict, and only take the nodes we need.
2020-10-25fixtures: change _getautousenames to an iteratorRan Benita
This reads better.
2020-10-25fixtures: use a faster replacement for ischildnodeRan Benita
ischildnode can be quite hot in some cases involving many fixtures. However it is always used in a way that the nodeid is constant and the baseid is iterated. So we can save work by pre-computing the parents of the nodeid and use a simple containment test. The `_getautousenames` function has the same stuff open-coded, so change it to use the new function as well.
2020-10-24make some hookspec docstrings technically correctsymonk
2020-10-23Merge pull request #7930 from bluetech/pytester-doc-fixesBruno Oliveira
pytester: minor doc fixes
2020-10-23python: fix quadratic behavior in collection of items using xunit fixturesRan Benita
Since commit 0f918b1a9dd14a2d046 pytest uses auto-generated autouse pytest fixtures for the xunit fixtures {setup,teardown}_{module,class,method,function}. All of these fixtures were given the same name. Unfortunately, pytest fixture lookup for a name works by grabbing all of the fixtures globally declared with a name and filtering to only those which match the specific node. So each xunit-using item iterates over a list (of fixturedefs) of a size of all previous same-xunit-using items, i.e. quadratic. Fixing this properly to use a better data structure is likely to take some effort, but we can avoid the immediate problem by just using a different name for each item's autouse fixture, so it only matches itself. A benchmark is added to demonstrate the issue. It is still way too slow after the fix and possibly still quadratic, but for a different reason which is another matter. Running --collect-only, before (snipped): 202533232 function calls (201902604 primitive calls) in 86.379 seconds ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 85.688 85.688 main.py:320(pytest_collection) 1 0.000 0.000 85.688 85.688 main.py:567(perform_collect) 80557/556 0.021 0.000 85.050 0.153 {method 'extend' of 'list' objects} 85001/15001 0.166 0.000 85.045 0.006 main.py:785(genitems) 10002 0.050 0.000 84.219 0.008 runner.py:455(collect_one_node) 10002 0.049 0.000 83.763 0.008 runner.py:340(pytest_make_collect_report) 10002 0.079 0.000 83.668 0.008 runner.py:298(from_call) 10002 0.019 0.000 83.541 0.008 runner.py:341(<lambda>) 5001 0.184 0.000 81.922 0.016 python.py:412(collect) 5000 0.020 0.000 81.072 0.016 python.py:842(collect) 30003 0.118 0.000 78.478 0.003 python.py:218(pytest_pycollect_makeitem) 30000 0.190 0.000 77.957 0.003 python.py:450(_genfunctions) 40001 0.081 0.000 76.664 0.002 nodes.py:183(from_parent) 30000 0.087 0.000 76.629 0.003 python.py:1595(from_parent) 40002 0.092 0.000 76.583 0.002 nodes.py:102(_create) 30000 0.305 0.000 76.404 0.003 python.py:1533(__init__) 15000 0.132 0.000 74.765 0.005 fixtures.py:1439(getfixtureinfo) 15000 0.165 0.000 73.664 0.005 fixtures.py:1492(getfixtureclosure) 15000 0.044 0.000 57.584 0.004 fixtures.py:1653(getfixturedefs) 30000 18.840 0.001 57.540 0.002 fixtures.py:1668(_matchfactories) 37507500 31.352 0.000 38.700 0.000 nodes.py:76(ischildnode) 15000 10.464 0.001 15.806 0.001 fixtures.py:1479(_getautousenames) 112930587/112910019 7.333 0.000 7.339 0.000 {built-in method builtins.len} After: 51890333 function calls (51259706 primitive calls) in 27.306 seconds ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 26.783 26.783 main.py:320(pytest_collection) 1 0.000 0.000 26.783 26.783 main.py:567(perform_collect) 80557/556 0.020 0.000 26.108 0.047 {method 'extend' of 'list' objects} 85001/15001 0.151 0.000 26.103 0.002 main.py:785(genitems) 10002 0.047 0.000 25.324 0.003 runner.py:455(collect_one_node) 10002 0.045 0.000 24.888 0.002 runner.py:340(pytest_make_collect_report) 10002 0.069 0.000 24.805 0.002 runner.py:298(from_call) 10002 0.017 0.000 24.690 0.002 runner.py:341(<lambda>) 5001 0.168 0.000 23.150 0.005 python.py:412(collect) 5000 0.019 0.000 22.223 0.004 python.py:858(collect) 30003 0.101 0.000 19.818 0.001 python.py:218(pytest_pycollect_makeitem) 30000 0.161 0.000 19.368 0.001 python.py:450(_genfunctions) 30000 0.302 0.000 18.236 0.001 python.py:1611(from_parent) 40001 0.084 0.000 18.051 0.000 nodes.py:183(from_parent) 40002 0.116 0.000 17.967 0.000 nodes.py:102(_create) 30000 0.308 0.000 17.770 0.001 python.py:1549(__init__) 15000 0.117 0.000 16.111 0.001 fixtures.py:1439(getfixtureinfo) 15000 0.134 0.000 15.135 0.001 fixtures.py:1492(getfixtureclosure) 15000 9.320 0.001 14.738 0.001 fixtures.py:1479(_getautousenames)
2020-10-23pytester: minor doc fixesRan Benita
2020-10-19cacheprovider: fix some files in packages getting lost from --lfRan Benita
--lf has an optimization where it skips collecting Modules (python files) which don't contain failing tests. The optimization works by getting the paths of all cached failed tests and skipping the collection of Modules whose path is not included in that list. In pytest, Package nodes are Module nodes with the fspath being the file `<package dir>/__init__.py`. Since it's a Module the logic above triggered for it, and because it's an `__init__.py` file which is unlikely to have any failing tests in it, it is skipped, which causes its entire directory to be skipped, including any Modules inside it with failing tests. Fix by special-casing Packages to never filter. This means entire Packages are never filtered, the Modules themselves are always checked. It is reasonable to consider an optimization which does filter entire packages bases on parent paths etc. but this wouldn't actually save any real work so is really not worth it.