A decorator for applying a mark on test functions and classes. ``MarkDecorators`` are created with ``pytest.mark``:: mark1 = pytest.mark.NAME # Simple MarkDecorator mark2 = pytest.mark.NAME(name1=value) # Parametrized MarkDecorator and can then be applied as decorators t
| 320 | |
| 321 | @dataclasses.dataclass |
| 322 | class MarkDecorator: |
| 323 | """A decorator for applying a mark on test functions and classes. |
| 324 | |
| 325 | ``MarkDecorators`` are created with ``pytest.mark``:: |
| 326 | |
| 327 | mark1 = pytest.mark.NAME # Simple MarkDecorator |
| 328 | mark2 = pytest.mark.NAME(name1=value) # Parametrized MarkDecorator |
| 329 | |
| 330 | and can then be applied as decorators to test functions:: |
| 331 | |
| 332 | @mark2 |
| 333 | def test_function(): |
| 334 | pass |
| 335 | |
| 336 | When a ``MarkDecorator`` is called, it does the following: |
| 337 | |
| 338 | 1. If called with a single class as its only positional argument and no |
| 339 | additional keyword arguments, it attaches the mark to the class so it |
| 340 | gets applied automatically to all test cases found in that class. |
| 341 | |
| 342 | 2. If called with a single function as its only positional argument and |
| 343 | no additional keyword arguments, it attaches the mark to the function, |
| 344 | containing all the arguments already stored internally in the |
| 345 | ``MarkDecorator``. |
| 346 | |
| 347 | 3. When called in any other case, it returns a new ``MarkDecorator`` |
| 348 | instance with the original ``MarkDecorator``'s content updated with |
| 349 | the arguments passed to this call. |
| 350 | |
| 351 | Note: The rules above prevent a ``MarkDecorator`` from storing only a |
| 352 | single function or class reference as its positional argument with no |
| 353 | additional keyword or positional arguments. You can work around this by |
| 354 | using `with_args()`. |
| 355 | """ |
| 356 | |
| 357 | mark: Mark |
| 358 | |
| 359 | def __init__(self, mark: Mark, *, _ispytest: bool = False) -> None: |
| 360 | """:meta private:""" |
| 361 | check_ispytest(_ispytest) |
| 362 | self.mark = mark |
| 363 | |
| 364 | @property |
| 365 | def name(self) -> str: |
| 366 | """Alias for mark.name.""" |
| 367 | return self.mark.name |
| 368 | |
| 369 | @property |
| 370 | def args(self) -> tuple[Any, ...]: |
| 371 | """Alias for mark.args.""" |
| 372 | return self.mark.args |
| 373 | |
| 374 | @property |
| 375 | def kwargs(self) -> Mapping[str, Any]: |
| 376 | """Alias for mark.kwargs.""" |
| 377 | return self.mark.kwargs |
| 378 | |
| 379 | @property |
no outgoing calls
no test coverage detected