diff --git a/src/bin/sage-runtests b/src/bin/sage-runtests index bc8032f30b7..191e3a54095 100755 --- a/src/bin/sage-runtests +++ b/src/bin/sage-runtests @@ -145,21 +145,22 @@ if __name__ == "__main__": DC = DocTestController(args, args.filenames) err = DC.run() - try: - exit_code_pytest = 0 - import pytest - pytest_options = ["--import-mode", "importlib"] - if args.verbose: - pytest_options.append("-v") - exit_code_pytest = pytest.main(pytest_options + args.filenames) - if exit_code_pytest == 5: - # Exit code 5 means there were no test files, pass in this case + if "pytest" in DC.options.optional: + try: exit_code_pytest = 0 - - except ModuleNotFoundError: - print("Pytest is not installed, skip checking tests that rely on it.") - - if err == 0: - sys.exit(exit_code_pytest) - else: - sys.exit(err) + import pytest + pytest_options = ["--import-mode", "importlib"] + if args.verbose: + pytest_options.append("-v") + exit_code_pytest = pytest.main(pytest_options + args.filenames) + if exit_code_pytest == 5: + # Exit code 5 means there were no test files, pass in this case + exit_code_pytest = 0 + + except ModuleNotFoundError: + print("Pytest is not installed, skip checking tests that rely on it.") + + if err == 0: + sys.exit(exit_code_pytest) + + sys.exit(err)