Skip to content

No test coverage is produced when test and source located in different packages #12636

Closed
@davido

Description

@davido

I'm seeing this behaviour on Bazel@HEAD (11d85c0)

Bazel coverage doesn't produce any coverage results if source and test rules located in different packages.

Consider these two reproducers:

Same package, works as expected

https://github.com/davido/bazel_coverage_src_and_test_same_package

Different packages, no coverage is produced

https://github.com/davido/bazel_coverage_src_and_test_different_package

In the latter case, the source rule is located in the root BUILD file:

BUILD

java_library(
    name = "collatz-lib",
    srcs = glob(["src/main/**/*.java"]),
    visibility = ["//visibility:public"],
)

and the test rule is located in src/test/java/com/example/BUILD file:

src/test/java/com/example/BUILD

java_test(
    name = "test",
    srcs = ["TestCollatz.java"],
    test_class = "com.example.TestCollatz",
    deps = ["//:collatz-lib"],
)

No coverage data is produced in this case:

  $ bazel coverage --test_output=all src/test/java/com/example:test
  [...]
  WARNING: There was no coverage found.
================================================================================
Target //src/test/java/com/example:test up-to-date:
  bazel-bin/src/test/java/com/example/test.jar
  bazel-bin/src/test/java/com/example/test
INFO: Elapsed time: 20.090s, Critical Path: 6.83s
INFO: 21 processes: 10 internal, 8 linux-sandbox, 3 worker.
INFO: Build completed successfully, 21 total actions
//src/test/java/com/example:test                                         PASSED in 0.8s

Executed 1 out of 1 test: 1 test passes.
INFO: Build completed successfully, 21 total actions

Workaround is to move test rule also to root BUILD file (like it is in the Bazel coverage test itself), or to move source rule to src/main/java/com/example package, see first reproducer repository.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions