generated from bazel-contrib/rules-template
-
-
Notifications
You must be signed in to change notification settings - Fork 35
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
py_pytest_main
and coverage
#303
Labels
question
This issue is a question. Close the loop with documentation?
Comments
mattem
added
question
This issue is a question. Close the loop with documentation?
and removed
untriaged
Requires traige
labels
Apr 29, 2024
i don't know if this is the best solution, but here's what i currently do:
load("@aspect_rules_py//py:defs.bzl", _py_pytest_main = "py_pytest_main")
load("@pip//:requirements.bzl", "requirement")
def py_pytest_main(name):
_py_pytest_main(
name = name,
deps = [
requirement("pytest"),
requirement("pytest-asyncio"),
requirement("pytest-cov"),
],
args = [
# report on all but passed tests
# docs: https://docs.pytest.org/en/stable/how-to/output.html#producing-a-detailed-summary-report
"-ra",
# set verbositylevel
# docs: https://docs.pytest.org/en/stable/how-to/output.html#verbosity
"-vv",
# enable capturing of specific warnings
# docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html#controlling-warnings
# docs: https://docs.pytest.org/en/stable/reference/reference.html#pytest.PytestUnhandledCoroutineWarning
"-W error::pytest.PytestUnhandledCoroutineWarning",
# enable automatic async test discovery
# docs: https://pytest-asyncio.readthedocs.io/en/latest/concepts.html#auto-mode
"--asyncio-mode=auto",
# set package for which coverage should be collected
# docs: https://pytest-cov.readthedocs.io/en/latest/config.html#referenc
"--cov={}".format(native.package_name().replace("/", ".")),
# enable branch coverage
"--cov-branch",
# NOTE normally we would have to set this.
# but i have no clue how to get the output path in here.
# thus i do it in a custom template
# "--cov-report=lcov:$COVERAGE_OUTPUT_FILE",
],
template = "//:pytest.py.tmpl",
visibility = [":__pkg__"],
)
# Copyright 2022 Aspect Build Systems, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# adapted from: https://github.com/aspect-build/rules_py/blob/main/py/private/pytest.py.tmpl
import sys
import os
import pytest
if __name__ == "__main__":
# Change to the directory where we need to run the test or execute a no-op
$$CHDIR$$
os.environ["ENV"] = "testing"
args = [
"--verbose",
"--ignore=external/",
# Avoid loading of the plugin "cacheprovider".
"-p",
"no:cacheprovider",
]
junit_xml_out = os.environ.get("XML_OUTPUT_FILE")
if junit_xml_out is not None:
args.append(f"--junitxml={junit_xml_out}")
# customization starts here
coverage_lcov_out = os.environ.get("COVERAGE_OUTPUT_FILE")
if coverage_lcov_out is not None:
args.append(f"--cov-report=lcov:{coverage_lcov_out}")
# customization ends here
test_filter = os.environ.get("TESTBRIDGE_TEST_ONLY")
if test_filter is not None:
args.append(f"-k={test_filter}")
user_args = [$$FLAGS$$]
if len(user_args) > 0:
args.extend(user_args)
cli_args = sys.argv[1:]
if len(cli_args) > 0:
args.extend(cli_args)
exit_code = pytest.main(args)
if exit_code != 0:
print("Pytest exit code: " + str(exit_code), file=sys.stderr)
print("Ran pytest.main with " + str(args), file=sys.stderr)
sys.exit(exit_code) |
I'm trying to use your workaround, but I run into this error:
Does this ring a bell to you? |
@qdii not really, sorry. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi ! I am trying to use
py_pytest_main
alongsidepytest-cov
to generate an HTML coverage report.The
py_pytest_main
is working as expected, the only problem I have is that the HTML report is generated inside the bazel sandbox and I don't know how to have bazel copy it tobazel-testlogs/*
or somewhere easily accessible so that I don't have to run my tests with--sandbox_debug
and then having to manually find it usingfind -L ~/.cache/bazel -name "htmlcov" -type d
Is there a simple way of achieving that that I am missing?
My bazel test invocation is simply:
This is my BUILD file:
My versions:
rules_py
0.7.1rules_python
0.29.0bazel
7.0.2The text was updated successfully, but these errors were encountered: