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

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

Closed
davido opened this issue Dec 5, 2020 · 2 comments
Closed
Labels
team-Rules-Java Issues for Java rules untriaged

Comments

@davido
Copy link
Contributor

davido commented Dec 5, 2020

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.

@keith
Copy link
Member

keith commented Dec 5, 2020

Worth testing --instrumentation_filter, I believe that's inferred as the package of the test

@jin jin added team-Rules-Java Issues for Java rules untriaged labels Dec 7, 2020
@davido
Copy link
Contributor Author

davido commented Jan 31, 2021

Worth testing --instrumentation_filter, I believe that's inferred as the package of the test

Thanks, that fixed it.

@davido davido closed this as completed Jan 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
team-Rules-Java Issues for Java rules untriaged
Projects
None yet
Development

No branches or pull requests

3 participants