Skip to content

To run coverage with python, setting PYTHON_COVERAGE in every test env is required #14436

@bradb423

Description

@bradb423

Description of the problem / feature request:

Currently, to obtain coverage for projects in python, the location macro is used to point to the entry point of a python coverage tool. This has to be repeated for each test in a given project. Perhaps an environment variable could be created e.g: PYTHON_COVERAGE_TARGET, to make the process simpler.

Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

For an example project with the following python files:

import sys


def main() -> int:
    return 0


if __name__ == "__main__":
    sys.exit(main())
import unittest

from main import main


class TestMain(unittest.TestCase):
    def test_main(self):
        self.assertEqual(main(), 0)


if __name__ == "__main__":
    unittest.main()

To achieve coverage for the files above, one can use the location macro to point to a specific coverage tool. This is shown below in the BUILD file:

alias(
    name = "python_coverage_tools",
    actual = entry_point("coverage"),
    visibility = ["//src:__subpackages__"],
)

py_test(
    name = "test",
    srcs = ["test.py"],
    env = {
        "PYTHON_COVERAGE": "$(location :python_coverage_tools)",
    },
    deps = [
        ":main",
        ":python_coverage_tools",
    ],
)

The problem for this is that for additional python test files, the PYTHON_COVERAGE must be set each time. While this process could be automated, it may be easier to use an environment variable that would allow us to use --test_env flag instead of the location macro each time.

What operating system are you running Bazel on?

Debian 10(Buster)

Bazel Version

bazel version 4.2.1 and above

@c-mita

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3We're not considering working on this, but happy to review a PR. (No assignee)team-Rules-PythonNative rules for Python

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions