rules_python icon indicating copy to clipboard operation
rules_python copied to clipboard

Support coverage for `py_test`

Open hwright opened this issue 7 years ago • 13 comments

bazel coverage should generate a coverage report for the python tests it runs. Ideally, it gathers the list of dependencies, and then computes coverage over that for a given test. When multiple tests are run in the same Bazel invocation, their reports should be merged into a single one.

I'm dropping this here mostly as a python-specific version of bazelbuild/bazel#1118. I know that the py_test rule is currently implemented natively in Bazel, and the implementation in rules_python just forwards there, but this is fundamentally a python issue, so this repo feels appropriate.

The general plan is to modify the python test runner within Bazel to use coverage.py to generate reports when running under coverage. I suspect it is a journey fraught with peril.

Alternatively, if it is possible to invoke coverage.py in ways other than modifying core Bazel, I'm open to suggestions.

hwright avatar Nov 20 '17 19:11 hwright

@damienmg assigning to you to redirect appropriately.

mattmoor avatar Nov 27 '17 18:11 mattmoor

fwiw, I've also been poking at this as documented here: https://groups.google.com/d/msg/bazel-discuss/LC97zBrMG84/cfVNdus-AwAJ

It's been slow going, as I'm pretty unfamiliar with Bazel internals, and haven't been able to get the ear of somebody who is.

hwright avatar Nov 27 '17 19:11 hwright

Setting P4 because this isn't a blocker for 1.0 support.

brandjon avatar Nov 09 '18 23:11 brandjon

Has this feature been implemented yet? If so, in what release version? It seems like coverage has been able to cover every other bit of our code repo except for all python files which is makes up most of our code base. Please let me know!

JoshSullivan11 avatar Jun 25 '19 19:06 JoshSullivan11

Is there a way to make bazel coverage //some_py_test not fail even if it does not collect coverage information. Currently, it fails because :lcov_merger attribute is not defined in py_test rules.

This is kind of important because we want to be able to run bazel coverage ... on a monorepo.

siddharthab avatar Aug 14 '19 20:08 siddharthab

how is it possible that coverage, python, and bazel don't work together, in the year of our lord 2020

sgammon avatar Sep 06 '20 18:09 sgammon

IIUC this problem lies in bazelbuild/bazel, and we need to work to get https://github.com/bazelbuild/bazel/issues/10660 triaged there.

thundergolfer avatar Sep 09 '20 02:09 thundergolfer

This issue has been automatically marked as stale because it has not had any activity for 180 days. It will be closed if no further activity occurs in 30 days. Collaborators can add an assignee to keep this open indefinitely. Thanks for your contributions to rules_python!

github-actions[bot] avatar Apr 28 '21 22:04 github-actions[bot]

We at Camus Energy still want this. Python doesn't scale well without good test coverage, and coverage reports are valuable leverage.

If the author of an eventual PR is ever in San Francisco, we will buy you a taco, and tell you all about how weird it is to develop software in the energy sector. (Hopefully these words of support count as activity.)

EnergySRE avatar Apr 29 '21 17:04 EnergySRE

This issue has been automatically marked as stale because it has not had any activity for 180 days. It will be closed if no further activity occurs in 30 days. Collaborators can add an assignee to keep this open indefinitely. Thanks for your contributions to rules_python!

github-actions[bot] avatar Oct 27 '21 22:10 github-actions[bot]

Boop. We still desire this 🙂

EnergySRE avatar Oct 28 '21 02:10 EnergySRE

Boop. We still desire this 🙂

Then implement it, perhaps? 🙃

mihaigalos avatar Oct 28 '21 16:10 mihaigalos

At this point upstream coveragepy has lcov support. I think the main piece that's missing is that there needs to be a way to specify a target for coveragepy in py_runtime for hermetic python toolchains.

adam-azarchs avatar Apr 29 '22 22:04 adam-azarchs

@aignas do we have more work to do on this?

chrislovecnm avatar Jul 11 '23 16:07 chrislovecnm

this issue has been open for 6 years

sgammon avatar Aug 20 '23 05:08 sgammon

I think we can safely close it now as we have docs and there are quite a few versions supporting toolchain with coverage.py enabled.

aignas avatar Aug 20 '23 08:08 aignas

@aignas I just set up a brand new project on top of rules_python, using Bazel 7 and Bzlmod, and coverage still doesn't work out of the box.

Where do you see toolchains supporting coverage?

sgammon avatar Aug 20 '23 21:08 sgammon

https://github.com/bazelbuild/rules_python/blob/main/docs/coverage.md

adam-azarchs avatar Aug 21 '23 00:08 adam-azarchs

See https://rules-python.readthedocs.io/en/latest/coverage.html

aignas avatar Nov 05 '23 13:11 aignas