From f11ec32650290be59051809631137052efa58198 Mon Sep 17 00:00:00 2001 From: Alex Martani Date: Fri, 16 Jun 2023 18:59:52 -0300 Subject: [PATCH] Avoid passing __init__.py files to pytest to mitigate bug. --- python_pytest/pytest_shim.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/python_pytest/pytest_shim.py b/python_pytest/pytest_shim.py index a17398b..2827c2f 100644 --- a/python_pytest/pytest_shim.py +++ b/python_pytest/pytest_shim.py @@ -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"))) @@ -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))