Skip to content

Commit 703d90f

Browse files
committed
flaky
1 parent 2fed954 commit 703d90f

File tree

1 file changed

+68
-2
lines changed

1 file changed

+68
-2
lines changed

python_files/vscode_pytest/__init__.py

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@
3333
USES_PYTEST_DESCRIBE = True
3434

3535

36+
sys.path.append("/Users/eleanorboyd/vscode-python/.nox/install_python_libs/lib/python3.10")
37+
sys.path.append("/Users/eleanorboyd/vscode-python-debugger")
38+
sys.path.append("/Users/eleanorboyd/vscode-python-debugger/bundled")
39+
sys.path.append("/Users/eleanorboyd/vscode-python-debugger/bundled/libs")
40+
41+
import debugpy # noqa: E402
42+
43+
debugpy.connect(5678)
44+
debugpy.breakpoint() # noqa: E702
45+
46+
3647
class TestData(TypedDict):
3748
"""A general class that all test objects inherit from."""
3849

@@ -68,6 +79,7 @@ def __init__(self, message):
6879
collected_tests_so_far = []
6980
TEST_RUN_PIPE = os.getenv("TEST_RUN_PIPE")
7081
SYMLINK_PATH = None
82+
FLAKY_MAX_RUNS = None
7183

7284

7385
def pytest_load_initial_conftests(early_config, parser, args): # noqa: ARG001
@@ -92,6 +104,10 @@ def pytest_load_initial_conftests(early_config, parser, args): # noqa: ARG001
92104
global IS_DISCOVERY
93105
IS_DISCOVERY = True
94106

107+
# set the reruns value, -1 if not set
108+
global FLAKY_MAX_RUNS
109+
FLAKY_MAX_RUNS = get_reruns_value(args)
110+
95111
# check if --rootdir is in the args
96112
for arg in args:
97113
if "--rootdir=" in arg:
@@ -120,6 +136,29 @@ def pytest_load_initial_conftests(early_config, parser, args): # noqa: ARG001
120136
SYMLINK_PATH = rootdir
121137

122138

139+
def get_reruns_value(args):
140+
"""
141+
Extracts the value of the --reruns argument from a list of command-line arguments.
142+
143+
Args:
144+
args (list of str): A list of command-line arguments.
145+
146+
Returns:
147+
int: The integer value of the --reruns argument if found, otherwise -1.
148+
"""
149+
for arg in args:
150+
if arg.startswith("--reruns"):
151+
if "=" in arg:
152+
# Extract the value from --reruns=<value>
153+
return int(arg.split("=")[1])
154+
else:
155+
# Get the value from the next argument
156+
index = args.index(arg)
157+
if index + 1 < len(args):
158+
return int(args[index + 1])
159+
return -1
160+
161+
123162
def pytest_internalerror(excrepr, excinfo): # noqa: ARG001
124163
"""A pytest hook that is called when an internal error occurs.
125164
@@ -150,11 +189,37 @@ def pytest_exception_interact(node, call, report):
150189
else:
151190
ERRORS.append(report.longreprtext + "\n Check Python Test Logs for more details.")
152191
else:
192+
node_id = get_absolute_test_id(node.nodeid, get_node_path(node))
193+
# Check if Pytest-rerunfailures is enabled
194+
# check # run this is
195+
# check # of reruns allowed
196+
# if # of reruns is reached, then send the error message, otherwise do not send the error message
197+
198+
try:
199+
exec_count = node.execution_count
200+
# global is set if arg is present during pytest_load_initial_conftests
201+
# if not present, then -1
202+
flaky_max_runs = FLAKY_MAX_RUNS
203+
204+
# check for rerunfailures marker
205+
for m in node.own_markers:
206+
if m.name == "flaky":
207+
flaky_max_runs = m.kwargs.get("reruns", 0)
208+
break
209+
210+
# flaky_max_runs != -1 means test is flaky
211+
if flaky_max_runs != -1 and exec_count <= flaky_max_runs:
212+
return
213+
elif flaky_max_runs != -1 and exec_count > flaky_max_runs:
214+
print("Plugin info[vscode-pytest]: max reruns reached.")
215+
except AttributeError:
216+
pass
217+
153218
# If during execution, send this data that the given node failed.
154219
report_value = "error"
155220
if call.excinfo.typename == "AssertionError":
156221
report_value = "failure"
157-
node_id = get_absolute_test_id(node.nodeid, get_node_path(node))
222+
# Only add test to collected_tests_so_far if it is not a flaky test that will re-run
158223
if node_id not in collected_tests_so_far:
159224
collected_tests_so_far.append(node_id)
160225
item_result = create_test_outcome(
@@ -280,7 +345,8 @@ def pytest_report_teststatus(report, config): # noqa: ARG001
280345
node_path = cwd
281346
# Calculate the absolute test id and use this as the ID moving forward.
282347
absolute_node_id = get_absolute_test_id(report.nodeid, node_path)
283-
if absolute_node_id not in collected_tests_so_far:
348+
# If the test is not a rerun, add it to the collected_tests_so_far list.
349+
if report.outcome != "rerun" and absolute_node_id not in collected_tests_so_far:
284350
collected_tests_so_far.append(absolute_node_id)
285351
item_result = create_test_outcome(
286352
absolute_node_id,

0 commit comments

Comments
 (0)