forked from pantsbuild/pants
/
pytest.py
107 lines (98 loc) · 3.76 KB
/
pytest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# Copyright 2016 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from typing import Optional, Tuple, cast
from pants.option.custom_types import shell_str
from pants.option.subsystem import Subsystem
class PyTest(Subsystem):
options_scope = "pytest"
help = "The pytest Python test framework (https://docs.pytest.org/)."
@classmethod
def register_options(cls, register):
super().register_options(register)
register(
"--args",
type=list,
member_type=shell_str,
passthrough=True,
help='Arguments to pass directly to Pytest, e.g. `--pytest-args="-k test_foo --quiet"`',
)
register(
"--version",
# This should be kept in sync with `requirements.txt`.
# TODO: To fix this, we should allow using a `target_option` referring to a
# `python_requirement_library` to override the version.
default="pytest>=6.0.1,<6.3",
advanced=True,
help="Requirement string for Pytest.",
)
register(
"--pytest-plugins",
type=list,
advanced=True,
default=["pytest-cov>=2.10.1,<2.12"],
help=(
"Requirement strings for any plugins or additional requirements you'd like to use."
),
)
register(
"--timeouts",
type=bool,
default=True,
help="Enable test target timeouts. If timeouts are enabled then test targets with a "
"timeout= parameter set on their target will time out after the given number of "
"seconds if not completed. If no timeout is set, then either the default timeout "
"is used or no timeout is configured.",
)
register(
"--timeout-default",
type=int,
advanced=True,
help=(
"The default timeout (in seconds) for a test target if the `timeout` field is not "
"set on the target."
),
)
register(
"--timeout-maximum",
type=int,
advanced=True,
help="The maximum timeout (in seconds) that may be used on a `python_tests` target.",
)
register(
"--junit-xml-dir",
type=str,
metavar="<DIR>",
default=None,
advanced=True,
help="Specifying a directory causes Junit XML result files to be emitted under "
"that dir for each test run.",
)
register(
"--junit-family",
type=str,
default="xunit2",
advanced=True,
help="The format of the generated XML file. See https://docs.pytest.org/en/latest/reference.html#confval-junit_family.",
)
register(
"--execution-slot-var",
type=str,
default=None,
advanced=True,
help=(
"If a non-empty string, the process execution slot id (an integer) will be exposed "
"to tests under this environment variable name."
),
)
def get_requirement_strings(self) -> Tuple[str, ...]:
"""Returns a tuple of requirements-style strings for Pytest and Pytest plugins."""
return (self.options.version, *self.options.pytest_plugins)
@property
def timeouts_enabled(self) -> bool:
return cast(bool, self.options.timeouts)
@property
def timeout_default(self) -> Optional[int]:
return cast(Optional[int], self.options.timeout_default)
@property
def timeout_maximum(self) -> Optional[int]:
return cast(Optional[int], self.options.timeout_maximum)