|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Unit tests validating pytest marker propagation through decorator stacks.""" |
| 16 | + |
| 17 | +import functools |
| 18 | +import unittest |
| 19 | + |
| 20 | +from absl.testing import parameterized |
| 21 | +import jax |
| 22 | +import pytest |
| 23 | + |
| 24 | + |
| 25 | +def dummy_decorator(func): |
| 26 | + """Standard transparent wrapper decorator preserving function metadata.""" |
| 27 | + |
| 28 | + @functools.wraps(func) |
| 29 | + def wrapper(*args, **kwargs): |
| 30 | + return func(*args, **kwargs) |
| 31 | + |
| 32 | + return wrapper |
| 33 | + |
| 34 | + |
| 35 | +class MarkerPropagationTest(parameterized.TestCase): |
| 36 | + """Validates that pytest markers propagate correctly through decorator stacks.""" |
| 37 | + |
| 38 | + @pytest.mark.cpu_only |
| 39 | + @parameterized.named_parameters( |
| 40 | + {"testcase_name": "default", "unused": None}, |
| 41 | + ) |
| 42 | + def test_parameterized_cpu_only_marker_propagation(self, unused): |
| 43 | + """Verifies cpu_only marker above @parameterized propagates to generated methods.""" |
| 44 | + has_tpu = any(d.platform == "tpu" for d in jax.devices()) |
| 45 | + has_gpu = any(d.platform == "gpu" for d in jax.devices()) |
| 46 | + assert not has_tpu, "cpu_only parameterized test accidentally executed on TPU hardware" |
| 47 | + assert not has_gpu, "cpu_only parameterized test accidentally executed on GPU hardware" |
| 48 | + |
| 49 | + @pytest.mark.cpu_only |
| 50 | + @dummy_decorator |
| 51 | + def test_standard_decorator_cpu_only_marker_propagation(self): |
| 52 | + """Verifies cpu_only marker above standard decorators propagates correctly.""" |
| 53 | + has_tpu = any(d.platform == "tpu" for d in jax.devices()) |
| 54 | + has_gpu = any(d.platform == "gpu" for d in jax.devices()) |
| 55 | + assert not has_tpu, "cpu_only standard decorated test accidentally executed on TPU hardware" |
| 56 | + assert not has_gpu, "cpu_only standard decorated test accidentally executed on GPU hardware" |
| 57 | + |
| 58 | + |
| 59 | +if __name__ == "__main__": |
| 60 | + unittest.main() |
0 commit comments