Skip to content

Commit

Permalink
Merge pull request #3 from amartani/main
Browse files Browse the repository at this point in the history
Avoid passing __init__.py files to pytest to mitigate bug
  • Loading branch information
caseyduquettesc authored Jun 16, 2023
2 parents dc31e4e + f11ec32 commit 9800fd8
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions python_pytest/pytest_shim.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
if __name__ == "__main__":
pytest_args = ["--ignore=external"]

# pytest runs tests twice if __init__.py is passed explicitly as an argument. Remove any __init__.py file to avoid that.
# https://github.com/pytest-dev/pytest/issues/9313
args = [arg for arg in sys.argv[1:] if arg.startswith("-") or os.path.basename(arg) != "__init__.py"]

if os.environ.get("XML_OUTPUT_FILE"):
pytest_args.append("--junitxml={xml_output_file}".format(xml_output_file=os.environ.get("XML_OUTPUT_FILE")))

Expand All @@ -19,19 +23,19 @@
# If the test filter does not start with a class-like name, then use test filtering instead
# --test_filter=test_fn
if not module_name[0].isupper():
pytest_args.extend(sys.argv[1:])
pytest_args.extend(args)
pytest_args.append("-k={filter}".format(filter=module_name))
else:
# --test_filter=TestClass.test_fn
# Add test filter to path-like args
for arg in sys.argv[1:]:
for arg in args:
if not arg.startswith("--"):
# Maybe a src file? Add test class/method selection to it. Not sure if this will work if the
# symbol can't be found in the test file.
arg = "{arg}::{module_fn}".format(arg=arg, module_fn=module_name)
pytest_args.append(arg)
else:
pytest_args.extend(sys.argv[1:])
pytest_args.extend(args)

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

0 comments on commit 9800fd8

Please sign in to comment.