From f863428150c03430bbc072d7386f3c8dd9a64221 Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Fri, 6 May 2022 22:32:43 +0000 Subject: [PATCH] Only run doctests in pytest when doctest-modules is specified --- src/bin/sage | 4 ++-- src/conftest.py | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/bin/sage b/src/bin/sage index e94cc50940e..1daf4d91538 100755 --- a/src/bin/sage +++ b/src/bin/sage @@ -960,10 +960,10 @@ if [ "$1" = '-pytest' -o "$1" = '--pytest' ]; then for a in $*; do case $a in -*) ;; - *) exec pytest --rootdir="$SAGE_SRC" "$@" + *) exec pytest --rootdir="$SAGE_SRC" --doctest-modules "$@" esac done - exec pytest --rootdir="$SAGE_SRC" "$@" "$SAGE_SRC" + exec pytest --rootdir="$SAGE_SRC" --doctest-modules "$@" "$SAGE_SRC" else echo "Run 'sage -i pytest' to install" fi diff --git a/src/conftest.py b/src/conftest.py index b45ea31fcb9..5ec6bc953b9 100644 --- a/src/conftest.py +++ b/src/conftest.py @@ -130,7 +130,8 @@ def pytest_collect_file( # hit this here if someone explicitly runs `pytest some_file.pyx`. return pytest.skip("Skipping Cython file") elif file_path.suffix == ".py": - return SageDoctestModule.from_parent(parent, path=file_path) + if parent.config.option.doctestmodules: + return SageDoctestModule.from_parent(parent, path=file_path) @pytest.fixture(autouse=True) @@ -142,12 +143,12 @@ def add_imports(doctest_namespace: dict[str, Any]): """ # Inject sage.all into each doctest dict_all = sage.all.__dict__ - + # Remove '__package__' item from the globals since it is not # always in the globals in an actual Sage session. - dict_all.pop('__package__', None) + dict_all.pop("__package__", None) sage_namespace = dict(dict_all) - sage_namespace['__name__'] = '__main__' - + sage_namespace["__name__"] = "__main__" + doctest_namespace.update(**sage_namespace)