xref: /aosp_15_r20/external/bazelbuild-rules_testing/lib/unit_test.bzl (revision d605057434dcabba796c020773aab68d9790ff9f)
1*d6050574SRomain Jobredeaux# Copyright 2022 The Bazel Authors. All rights reserved.
2*d6050574SRomain Jobredeaux#
3*d6050574SRomain Jobredeaux# Licensed under the Apache License, Version 2.0 (the "License");
4*d6050574SRomain Jobredeaux# you may not use this file except in compliance with the License.
5*d6050574SRomain Jobredeaux# You may obtain a copy of the License at
6*d6050574SRomain Jobredeaux#
7*d6050574SRomain Jobredeaux#    http://www.apache.org/licenses/LICENSE-2.0
8*d6050574SRomain Jobredeaux#
9*d6050574SRomain Jobredeaux# Unless required by applicable law or agreed to in writing, software
10*d6050574SRomain Jobredeaux# distributed under the License is distributed on an "AS IS" BASIS,
11*d6050574SRomain Jobredeaux# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*d6050574SRomain Jobredeaux# See the License for the specific language governing permissions and
13*d6050574SRomain Jobredeaux# limitations under the License.
14*d6050574SRomain Jobredeaux"""# Unit test
15*d6050574SRomain Jobredeaux
16*d6050574SRomain JobredeauxSupport for testing generic Starlark code, i.e. code that doesn't require
17*d6050574SRomain Jobredeauxthe analysis phase or instantiate rules.
18*d6050574SRomain Jobredeaux"""
19*d6050574SRomain Jobredeaux
20*d6050574SRomain Jobredeaux# We have to load the private impl to avoid a circular dependency
21*d6050574SRomain Jobredeauxload("//lib/private:analysis_test.bzl", "analysis_test")
22*d6050574SRomain Jobredeaux
23*d6050574SRomain Jobredeaux_TARGET = Label("//lib:_stub_target_for_unit_tests")
24*d6050574SRomain Jobredeaux
25*d6050574SRomain Jobredeauxdef unit_test(name, impl, attrs = {}):
26*d6050574SRomain Jobredeaux    """Creates a test for generic Starlark code (i.e. non-rule/macro specific).
27*d6050574SRomain Jobredeaux
28*d6050574SRomain Jobredeaux    Unless you need custom attributes passed to the test, you probably don't need
29*d6050574SRomain Jobredeaux    this and can, instead, pass your test function directly to `test_suite.tests`.
30*d6050574SRomain Jobredeaux
31*d6050574SRomain Jobredeaux    See also: analysis_test, for testing analysis time behavior, such as rules.
32*d6050574SRomain Jobredeaux
33*d6050574SRomain Jobredeaux    Args:
34*d6050574SRomain Jobredeaux        name: (str) the name of the test
35*d6050574SRomain Jobredeaux        impl: (callable) the function implementing the test's asserts. It takes
36*d6050574SRomain Jobredeaux            a single position arg, `env`, which is information about the
37*d6050574SRomain Jobredeaux            test environment (see analysis_test docs).
38*d6050574SRomain Jobredeaux        attrs: (dict of str to str) additional attributes to make available to
39*d6050574SRomain Jobredeaux            the test.
40*d6050574SRomain Jobredeaux    """
41*d6050574SRomain Jobredeaux    analysis_test(
42*d6050574SRomain Jobredeaux        name = name,
43*d6050574SRomain Jobredeaux        impl = lambda env, target: impl(env),
44*d6050574SRomain Jobredeaux        target = _TARGET,
45*d6050574SRomain Jobredeaux        attrs = attrs,
46*d6050574SRomain Jobredeaux    )
47