Skip to content

Commit 9800fd8

Browse files
Merge pull request #3 from amartani/main
Avoid passing __init__.py files to pytest to mitigate bug
2 parents dc31e4e + f11ec32 commit 9800fd8

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

python_pytest/pytest_shim.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
if __name__ == "__main__":
1010
pytest_args = ["--ignore=external"]
1111

12+
# pytest runs tests twice if __init__.py is passed explicitly as an argument. Remove any __init__.py file to avoid that.
13+
# https://github.com/pytest-dev/pytest/issues/9313
14+
args = [arg for arg in sys.argv[1:] if arg.startswith("-") or os.path.basename(arg) != "__init__.py"]
15+
1216
if os.environ.get("XML_OUTPUT_FILE"):
1317
pytest_args.append("--junitxml={xml_output_file}".format(xml_output_file=os.environ.get("XML_OUTPUT_FILE")))
1418

@@ -19,19 +23,19 @@
1923
# If the test filter does not start with a class-like name, then use test filtering instead
2024
# --test_filter=test_fn
2125
if not module_name[0].isupper():
22-
pytest_args.extend(sys.argv[1:])
26+
pytest_args.extend(args)
2327
pytest_args.append("-k={filter}".format(filter=module_name))
2428
else:
2529
# --test_filter=TestClass.test_fn
2630
# Add test filter to path-like args
27-
for arg in sys.argv[1:]:
31+
for arg in args:
2832
if not arg.startswith("--"):
2933
# Maybe a src file? Add test class/method selection to it. Not sure if this will work if the
3034
# symbol can't be found in the test file.
3135
arg = "{arg}::{module_fn}".format(arg=arg, module_fn=module_name)
3236
pytest_args.append(arg)
3337
else:
34-
pytest_args.extend(sys.argv[1:])
38+
pytest_args.extend(args)
3539

3640
print(pytest_args, file=sys.stderr)
3741
raise SystemExit(pytest.main(pytest_args))

0 commit comments

Comments
 (0)