Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tox: guard against parallel pytest coverage execution #15

Closed
wants to merge 2 commits into from

Conversation

davvid
Copy link

@davvid davvid commented Apr 11, 2020

An usual problem users have is that pytest-cov will erase the
previous coverage data by default, thus if you run tox with
multiple environments you’ll get incomplete coverage at the end.

To prevent this problem you need to use --cov-append. It’s still
recommended to clean the previous coverage data to have
consistent output.

Cf. https://pytest-cov.readthedocs.io/en/latest/tox.html

@davvid
Copy link
Author

davvid commented Apr 11, 2020

The tox docs are only mention "incomplete coverage", but in practice it's possible to trigger test errors. I've observed the following without these changes:

INTERNALERROR> coverage.misc.CoverageException:
INTERNALERROR> Couldn't use data file '.../.coverage': no such table: meta
...
ERROR: InvocationError for command pytest (exited with code 3)

@davvid davvid force-pushed the tox-parallel-coverage branch 3 times, most recently from ea4e5bc to a377a7c Compare April 11, 2020 11:04
An usual problem users have is that pytest-cov will erase the
previous coverage data by default, thus if you run tox with
multiple environments you’ll get incomplete coverage at the end.

To prevent this problem you need to use --cov-append. It’s still
recommended to clean the previous coverage data to have
consistent output.

Cf. https://pytest-cov.readthedocs.io/en/latest/tox.html
pytest-cov has issues when run in parallel when using coverage 5,
as noted by the pytest-cov authors.

Use coverage 4 until pytest-cov is ready for coverage 5, or a
better solution is documented.

pytest-dev/pytest-cov@d3daf76

pytest-dev/pytest-cov#386
Copy link
Owner

@jaraco jaraco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this. I'd very much like to support evaluating coverage across runs of different Python. I've historically suppressed or ignored coverage on files that had selective execution.

I have some reservations, but I'm happy to explore options.

@@ -1,5 +1,5 @@
[tox]
envlist = python
envlist = clean,python
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My biggest reservation comes from this change, which

(a) adds an extra tox environment to the default argument,
(b) doesn't run under explicit environment invocations such as 'tox -e py38' or 'tox -e python' (which I have configured in my environment),
(c) corrects for lingering state at the beginning, and
(d) even with all of this, the coverage details aren't going to be readily appended in CI environments or other environments that provide isolation between runs.

I think I'd prefer to keep a simple 'tox' environment to simply run the tests, but add more complicated configurations to wrapper scripts or CI invocations or user-local settings.

Do you have a place where you're already using this approach? Does coverage recommend this approach?

@@ -14,9 +14,17 @@ deps =
pip_version = pip
commands =
pytest {posargs}
depends =
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this feature was added in tox 3.7, so that would imply a minversion bump to 3.7.

@@ -23,6 +23,7 @@ setup_requires = setuptools_scm[toml] >= 3.4.1
[options.extras_require]
testing =
# upstream
coverage < 5
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I'd prefer this to appear at the end of the list - that's where I typically add them.

@@ -23,6 +23,7 @@ setup_requires = setuptools_scm[toml] >= 3.4.1
[options.extras_require]
testing =
# upstream
coverage < 5
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More importantly, what's this line about? Is it to add coverage? To limit coverage? I do try to avoid pinning dependencies without a reason to pin (and a comment to explain why). Looking quickly at coverage, this is a downgrade from what's currently used. Is it necessary to support this feature? If so, there should be a comment explaining that.

@@ -1,5 +1,6 @@
[run]
omit = .tox/*
parallel = true
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change generally applicable?

@jaraco jaraco force-pushed the master branch 2 times, most recently from c4e3b4d to fc0162b Compare May 23, 2020 15:57
@davvid
Copy link
Author

davvid commented Jun 22, 2020

Thanks for the review notes. I agree, this isn't ready yet.

The coverage < 5 tweaks are workarounds mentioned for stuff related to pytest-dev/pytest-cov#386 and pytest-dev/pytest-cov#398 (comment) which could be helpful if we add pre-canned coverage environments.

That said, the recent pytest release notes mention changes that may have already addressed these issues, so it's worth trying newer versions these days. https://github.com/pytest-dev/pytest-cov/blob/master/CHANGELOG.rst#290-2020-05-22

As you noted, this would be best mentioned in a code comment in addition to the commit message so that it doesn't get lost in this random github thread. I have to revisit the coverage < 5 specs.

pytest.ini contains --cov, and that precludes someone from running tox tests in parallel without also having parallel = True in .coveragerc. The original changes were prompted by parallel tox runs stomping over the shared data file, which resulted in failed test runs that were confusing to diagnose.

In order to provide a simple tox test environment where users won't stumble into these edge cases then what do you think about this?

  1. Remove --cov from pytest.ini so that the simple tox run doesn't have to worry about coverage. Users that want parallel coverage can inject --cov using {args} via the pytest {args} template.
  2. Add parallel = True to .coveragerc, which current seems to be needed for parallel execution.
  3. Add a separate coverage environment for doing coverage, which won't have to concern itself with parallel tox execution, and can handle the clean/run/combine/collect steps into a single run.
  4. It might also make sense to provide an explicit coverage-clean and coverage-combine environments so that a user can choose to run the clean and collect/combine steps independently. For example, if they ran a parallel run themselves by injecting --cov then this would be helpful for them.

The most useful feature for me is parallel tox running of tests; coverage is secondary and not as important in my perspective, but 1. does not shut the door to that use case.

Supporting all the various test dependency permutations with parallel tox running and coverage can be tricky, which is why it seems like punting and making them explicit separate steps seems helpful.

Of course, this is something that would be best documented a bit, so perhaps the best first commit/patch would be to come up with a patch against skeleton.md to propose the workflow.

What do you think?

@jaraco jaraco force-pushed the master branch 3 times, most recently from 58cca73 to 1311cec Compare October 24, 2020 02:47
@jaraco jaraco closed this Dec 8, 2020
@jaraco jaraco deleted the branch jaraco:master December 8, 2020 17:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants