您可以创建调用 python 文件的 py_test 调用,它会自动扭曲对pylint 或pytest --pylint 的调用。为了在工作区中获得更多可重用的东西,请在 py_test 周围创建一个宏。我在Experimentations on Bazel: Python (3), linter & pytest中解释了详细的解决方案,并附有源代码链接。
在tools/pytest/pytest_wrapper.py中创建python工具(包装调用pytest,或者只调用pylint)
import sys
import pytest
# if using 'bazel test ...'
if __name__ == "__main__":
sys.exit(pytest.main(sys.argv[1:]))
在tools/pytest/defs.bzl中创建宏
"""Wrap pytest"""
load("@rules_python//python:defs.bzl", "py_test")
load("@my_python_deps//:requirements.bzl", "requirement")
def pytest_test(name, srcs, deps = [], args = [], data = [], **kwargs):
"""
Call pytest
"""
py_test(
name = name,
srcs = [
"//tools/pytest:pytest_wrapper.py",
] + srcs,
main = "//tools/pytest:pytest_wrapper.py",
args = [
"--capture=no",
"--black",
"--pylint",
"--pylint-rcfile=$(location //tools/pytest:.pylintrc)",
# "--mypy",
] + args + ["$(location :%s)" % x for x in srcs],
python_version = "PY3",
srcs_version = "PY3",
deps = deps + [
requirement("pytest"),
requirement("pytest-black"),
requirement("pytest-pylint"),
# requirement("pytest-mypy"),
],
data = [
"//tools/pytest:.pylintrc",
] + data,
**kwargs
)
公开tools/pytest/BUILD.bazel的部分资源
exports_files([
"pytest_wrapper.py",
".pylintrc",
])
从你的包中调用它BUILD.bazel
load("//tools/pytest:defs.bzl", "pytest_test")
...
pytest_test(
name = "test",
srcs = glob(["*.py"]),
deps = [
...
],
)
然后调用bazel test //... pylint, pytest, back,... 是测试流程的一部分