Skip to content

Code Coverage For Python Self Contained Executable Binary #1974

@xczhang07

Description

@xczhang07

Is your feature request related to a problem? Please describe.
I am trying to collect the code coverage for python service. The problem is I can't modify the service's source code; I only have access to its binary format, like a .par file. I've been able to use your API to get coverage from regular Python files, but not from these binaries. Is it possible to collect Python coverage data directly from a binary using coverage.py?

When I run the coverage command to collect the coverage data from a source file
-MacBook-Pro hello_world % python3 -m coverage run main.py
Hello, World!
-MacBook-Pro hello_world % python3 -m coverage report
Name Stmts Miss Cover

main.py 4 0 100%

TOTAL 4 0 100%

When I use the coverage api to collect coveragte data from its corresponding binary file
-MacBook-Pro hello_world % python3 -m coverage run hello_world_app
Hello, World!
-MacBook-Pro hello_world % python3 -m coverage report
No data to report.
-MacBook-Pro hello_world % python3 -m coverage report -m
No data to report.
-MacBook-Pro hello_world % python3 -m coverage html
No data to report.

Describe the solution you'd like
Is it possible that we are able to use the following steps to collect the coverage data from a binary file:

  1. python3 -m coverage run {python_binary_executable_file}
  2. python3 -m coverage report
  3. python3 -m coverage html

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
The source file content as:
def main():
print("Hello, World!")

if name == "main":
main()

The build file as:

BUILD file in my_hello_project/

load("@rules_python//python:defs.bzl", "py_binary")

package(
default_visibility = ["//visibility:public"],
)

py_binary(
name = "hello_world_app",
srcs = ["main.py"],
main = "main.py",
)

The module file as:

MODULE.bazel (inside bazel_python_hello_bzlmod/)

module(
name = "hello_bazel_bzlmod",
version = "0.1.0",
compatibility_level = 1,
)

Declare dependency on rules_python

Always check the latest recommended version and setup on rules_python GitHub!

bazel_dep(name = "rules_python", version = "0.30.0")

Use the python extension to register toolchains

This fetches and registers a compatible Python interpreter for Bazel to use.

Ensure you have the specified Python version installed on your system.

python = use_extension("@rules_python//python:extensions.bzl", "python")
python.toolchain(python_version = "3.11")

If you want to explicitly enable auto-detection for a specific version:

python.toolchain(python_version = "3.11", auto_detect = True)

Under the code directory, the command to build the binary as:
bazel build //hello_world:hello_world_app

The binary sample file will be attached

Thx
Xiaochong

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions