|
| 1 | +# ******************************************************************************* |
| 2 | +# Copyright (c) 2026 Contributors to the Eclipse Foundation |
| 3 | +# |
| 4 | +# See the NOTICE file(s) distributed with this work for additional |
| 5 | +# information regarding copyright ownership. |
| 6 | +# |
| 7 | +# This program and the accompanying materials are made available under the |
| 8 | +# terms of the Apache License Version 2.0 which is available at |
| 9 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# SPDX-License-Identifier: Apache-2.0 |
| 12 | +# ******************************************************************************* |
| 13 | + |
| 14 | +"""Lightweight macro for running unit tests via pytest without ITF plugin infrastructure.""" |
| 15 | + |
| 16 | +load("@rules_python//python:defs.bzl", "py_test") |
| 17 | + |
| 18 | +def py_itf_unittest(name, srcs, deps = [], data = [], env = {}, pytest_config = None, **kwargs): |
| 19 | + """Thin py_test wrapper for unit tests that do not need ITF plugin machinery. |
| 20 | +
|
| 21 | + Unlike py_itf_test, this macro creates a direct py_test with no launcher |
| 22 | + script or plugin infrastructure, so Bazel coverage works out of the box. |
| 23 | +
|
| 24 | + Args: |
| 25 | + name: Target name. |
| 26 | + srcs: Python test source files. |
| 27 | + deps: Additional Python dependencies. |
| 28 | + data: Data files available at runtime. |
| 29 | + env: Environment variables for the test. |
| 30 | + pytest_config: Optional pytest config file. Defaults to @score_itf//:pytest.ini. |
| 31 | + **kwargs: Forwarded to py_test (e.g. size, timeout, tags). |
| 32 | + """ |
| 33 | + pytest_bootstrap = Label("@score_itf//:main.py") |
| 34 | + if not pytest_config: |
| 35 | + pytest_config = Label("@score_itf//:pytest.ini") |
| 36 | + |
| 37 | + py_test( |
| 38 | + name = name, |
| 39 | + srcs = [pytest_bootstrap] + srcs, |
| 40 | + main = pytest_bootstrap, |
| 41 | + args = [ |
| 42 | + "-c $(location %s)" % pytest_config, |
| 43 | + "-p no:cacheprovider", |
| 44 | + "--show-capture=no", |
| 45 | + "--junitxml=$$XML_OUTPUT_FILE", |
| 46 | + ] + ["$(location %s)" % x for x in srcs], |
| 47 | + deps = ["@score_itf//:itf", "@itf_pip//pytest_mock"] + deps, |
| 48 | + data = [pytest_config] + data, |
| 49 | + env = {"PYTHONDONOTWRITEBYTECODE": "1"} | env, |
| 50 | + **kwargs |
| 51 | + ) |
0 commit comments