-
Notifications
You must be signed in to change notification settings - Fork 0
/
pytest_skipuntil.py
80 lines (61 loc) · 2.57 KB
/
pytest_skipuntil.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
from datetime import datetime
from enum import Enum
from logging import getLogger
import pytest
logger = getLogger(__name__)
# TODO: [ ] datetime -> date switch
# TODO: [ ] add strict feature
MARKER_NAME = "skip_until"
class MessageLevel(str, Enum):
error = "error"
warning = "warning"
class SkipUntilPlugin:
name = MARKER_NAME
def __init__(self, config):
self.config = config
self.messages = []
def pytest_collection_modifyitems(self, items):
for testcase in items:
for marker in testcase.own_markers:
if marker.name == "skip_until":
deadline = marker.kwargs.get("deadline")
if not deadline and marker.args:
deadline = marker.args[0]
if deadline is None:
raise pytest.UsageError("The deadline is not defined for skip_until!")
if not isinstance(deadline, datetime):
raise pytest.UsageError(
f"Unexpected deadline type {type(deadline)} is passed "
f"to skip_until, please specify a datetime!",
)
file_location = testcase.location[0]
if datetime.now() > deadline:
self.messages.append(
(
f"{file_location}::{testcase.name}: the deadline for "
"the test has passed",
MessageLevel.error,
)
)
continue
msg = marker.kwargs.get("msg") or "not specified"
message = f"The test is suppressed until {deadline}. The reason is: {msg}"
self.messages.append((f"{file_location}: {message}", MessageLevel.warning))
testcase.add_marker(pytest.mark.skip(reason=message))
def pytest_terminal_summary(
self,
terminalreporter,
exitstatus,
config,
) -> None:
for message, level in self.messages:
if level == MessageLevel.error:
terminalreporter.write_line(message, red=True)
if level == MessageLevel.warning:
terminalreporter.write_line(message, yellow=True)
def pytest_configure(config):
config.pluginmanager.register(SkipUntilPlugin(config), SkipUntilPlugin.name)
config.addinivalue_line(
"markers",
f"{MARKER_NAME}(...): skip test until datetime",
)