Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin/public/tests/pytest_wrong_test_m…
Browse files Browse the repository at this point in the history
…ethods' into public/tests/pytest_sage_files
  • Loading branch information
tobiasdiez committed Mar 29, 2022
2 parents af92e19 + 7644854 commit 24a0b17
Show file tree
Hide file tree
Showing 32 changed files with 280 additions and 238 deletions.
9 changes: 9 additions & 0 deletions src/bin/sage-coverage
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ class CoverageResults:
if not docstring:
self.no_doc.append(fullname)
return

if "pytest" in docstring:
self.good.append(fullname)
return

if "sage: " not in docstring:
self.no_test.append(fullname)
return
Expand Down Expand Up @@ -276,6 +281,10 @@ def go(filename):
or filename.endswith('.sage')):
return

# Filter pytest files which are not supposed to have doctests
if filename.endswith('_test.py'):
return

with open(filename, 'r') as f:
cr = CoverageResults(filename).check_file(f)
bad = cr.no_doc or cr.no_test or cr.possibly_wrong
Expand Down
36 changes: 29 additions & 7 deletions src/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,38 @@
from importlib.machinery import ModuleSpec
from importlib.util import spec_from_loader
from pathlib import Path
from typing import Any, Sequence
from typing import Any, List, Sequence

import pytest

# Ignore a few test files that are (not yet) using pytest
collect_ignore = [
"sage/misc/nested_class_test.py",
"sage/repl/rich_output/backend_test.py",
"sage/tests/deprecation_test.py"
]

def pytest_collection_modifyitems(
session: pytest.Session, config: pytest.Config, items: List[pytest.Item]
):
"""
This hook is called after collection has been performed, and can be used to
modify the list of items that will be run.
See `pytest documentation <https://docs.pytest.org/en/latest/reference/reference.html#std-hook-pytest_collection_modifyitems>`_.
"""
skip_as_false_positive = pytest.mark.skip(
reason="Skipping this because its not a pytest test but an ordinary"
+ "method that happens to start with 'test_'"
)
for item in items:
# Add a mark to all tests that should be skipped
if item.name in [
"test_relation_maxima",
"test_b2_local",
"test_b2_global",
"test_a1a3_local",
"test_a1a3_global",
"test_rst_global",
"test_comparison",
"test_signed_infinity",
"test_pickle",
]:
item.add_marker(skip_as_false_positive)


def pytest_collect_file(parent: pytest.Collector, file_path: Path):
Expand Down
6 changes: 6 additions & 0 deletions src/doc/en/developer/coding_basics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,12 @@ information. You can use the existing functions of Sage as templates.
However, lines only containing double colons `::` do not
end "TESTS" blocks.

Sometimes (but rarely) one has private or protected methods that don't need a
proper ``EXAMPLES`` doctest. In these cases, one can either write traditional
doctest using the ``TESTS`` block or use pytest to test the method.
In the latter case, one has to add ``TESTS: pytest`` to the docstring, so that
the method is explicitly marked as tested.

Note about Sphinx directives vs. other blocks
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
2 changes: 1 addition & 1 deletion src/doc/en/reference/misc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ Testing

sage/misc/sage_unittest
sage/misc/random_testing
sage/misc/nested_class_test
sage/misc/test_nested_class

Benchmarking and Profiling
~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion src/doc/en/reference/repl/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Display Backend Infrastructure
sage/repl/rich_output/output_catalog

sage/repl/rich_output/backend_base
sage/repl/rich_output/backend_test
sage/repl/rich_output/test_backend
sage/repl/rich_output/backend_doctest
sage/repl/rich_output/backend_ipython

Expand Down
2 changes: 1 addition & 1 deletion src/sage/categories/primer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1152,7 +1152,7 @@ class ElementMethods:
reveal some glitches in their implementation, in particular around
class naming and introspection. Sage currently works around the
more annoying ones but some remain visible. See
e.g. :mod:`sage.misc.nested_class_test`.
e.g. :mod:`sage.misc.test_nested_class`.
Let us now look at the categories of ``C``::
Expand Down
6 changes: 3 additions & 3 deletions src/sage/crypto/mq/sr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3339,7 +3339,7 @@ def __exit__(self, typ, value, tb):
"""
self.sr._allow_zero_inversions = self.allow_zero_inversions

def test_consistency(max_n=2, **kwargs):
def _test_consistency(max_n=2, **kwargs):
r"""
Test all combinations of ``r``, ``c``, ``e`` and ``n`` in ``(1,
2)`` for consistency of random encryptions and their polynomial
Expand All @@ -3358,8 +3358,8 @@ def test_consistency(max_n=2, **kwargs):
on machines with "only" 2GB of RAM, we test ``max_n`` = 1, which
has a more reasonable memory usage. ::
sage: from sage.crypto.mq.sr import test_consistency
sage: test_consistency(1) # long time (65s on sage.math, 2012)
sage: from sage.crypto.mq.sr import _test_consistency
sage: _test_consistency(1) # long time (65s on sage.math, 2012)
True
"""
consistent = True
Expand Down
2 changes: 1 addition & 1 deletion src/sage/docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ def process_docstring_module_title(app, what, name, obj, options, docstringlines
break

skip_picklability_check_modules = [
#'sage.misc.nested_class_test', # for test only
#'sage.misc.test_nested_class', # for test only
'sage.misc.latex',
'sage.misc.explain_pickle',
'__builtin__',
Expand Down
59 changes: 32 additions & 27 deletions src/sage/graphs/graph_decompositions/modular_decomposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def modular_decomposition(graph):
The Icosahedral graph is Prime::
sage: from sage.graphs.graph_decompositions.modular_decomposition import *
sage: from sage.graphs.graph_decompositions.modular_decomposition import modular_decomposition, print_md_tree, _test_modular_decomposition
sage: print_md_tree(modular_decomposition(graphs.IcosahedralGraph()))
PRIME
5
Expand Down Expand Up @@ -376,7 +376,7 @@ def modular_decomposition(graph):
....: 11:[12,1], 12:[11,1], 13:[14,16,17,1], 14:[13,17,1],
....: 16:[13,17,1], 17:[13,14,16,18,1], 18:[17], 24:[5,4,3,1]}
sage: g = Graph(d)
sage: test_modular_decomposition(modular_decomposition(g), g)
sage: _test_modular_decomposition(modular_decomposition(g), g)
True
Graph from the :wikipedia:`Modular_decomposition`::
Expand All @@ -386,7 +386,7 @@ def modular_decomposition(graph):
....: 7:[2,3,4,5,8,9,10,11], 8:[6,7,9,10,11], 9:[6,7,8,10,11],
....: 10:[6,7,8,9], 11:[6,7,8,9]}
sage: g = Graph(d2)
sage: test_modular_decomposition(modular_decomposition(g), g)
sage: _test_modular_decomposition(modular_decomposition(g), g)
True
"""
if graph.is_directed():
Expand Down Expand Up @@ -2488,8 +2488,8 @@ def gamma_classes(graph):
Ensure that the returned vertex sets from some random graphs are modules::
sage: from sage.graphs.graph_decompositions.modular_decomposition import test_gamma_modules
sage: test_gamma_modules(2, 10, 0.5)
sage: from sage.graphs.graph_decompositions.modular_decomposition import _test_gamma_modules
sage: _test_gamma_modules(2, 10, 0.5)
"""
from itertools import chain
from sage.sets.disjoint_set import DisjointSet
Expand Down Expand Up @@ -2538,7 +2538,8 @@ def habib_maurer_algorithm(graph, g_classes=None):
The Icosahedral graph is Prime::
sage: from sage.graphs.graph_decompositions.modular_decomposition import *
sage: from sage.graphs.graph_decompositions.modular_decomposition import (
....: modular_decomposition, habib_maurer_algorithm, print_md_tree, _test_modular_decomposition)
sage: print_md_tree(habib_maurer_algorithm(graphs.IcosahedralGraph()))
PRIME
1
Expand Down Expand Up @@ -2599,7 +2600,7 @@ def habib_maurer_algorithm(graph, g_classes=None):
....: 11:[12,1], 12:[11,1], 13:[14,16,17,1], 14:[13,17,1],
....: 16:[13,17,1], 17:[13,14,16,18,1], 18:[17], 24:[5,4,3,1]}
sage: g = Graph(d)
sage: test_modular_decomposition(habib_maurer_algorithm(g), g)
sage: _test_modular_decomposition(habib_maurer_algorithm(g), g)
True
Graph from the :wikipedia:`Modular_decomposition`::
Expand All @@ -2609,7 +2610,7 @@ def habib_maurer_algorithm(graph, g_classes=None):
....: 7:[2,3,4,5,8,9,10,11], 8:[6,7,9,10,11], 9:[6,7,8,10,11],
....: 10:[6,7,8,9], 11:[6,7,8,9]}
sage: g = Graph(d2)
sage: test_modular_decomposition(habib_maurer_algorithm(g), g)
sage: _test_modular_decomposition(habib_maurer_algorithm(g), g)
True
Tetrahedral Graph is Series::
Expand Down Expand Up @@ -2705,7 +2706,7 @@ def habib_maurer_algorithm(graph, g_classes=None):
#=============================================================================

# Function implemented for testing
def test_modular_decomposition(tree_root, graph):
def _test_modular_decomposition(tree_root, graph):
"""
Test the input modular decomposition tree using recursion.
Expand All @@ -2721,30 +2722,32 @@ def test_modular_decomposition(tree_root, graph):
EXAMPLES::
sage: from sage.graphs.graph_decompositions.modular_decomposition import *
sage: from sage.graphs.graph_decompositions.modular_decomposition import (
....: modular_decomposition, _test_modular_decomposition)
sage: g = graphs.HexahedralGraph()
sage: test_modular_decomposition(modular_decomposition(g), g)
sage: _test_modular_decomposition(modular_decomposition(g), g)
True
"""
if tree_root.node_type != NodeType.NORMAL:
for module in tree_root.children:
if not test_module(module, graph):
if not _test_module(module, graph):
# test whether modules pass the defining
# characteristics of modules
return False
if not test_modular_decomposition(module,
graph.subgraph(get_vertices(module))):
if not _test_modular_decomposition(
module, graph.subgraph(get_vertices(module))
):
# recursively test the modular decomposition subtrees
return False

if not test_maximal_modules(tree_root, graph):
if not _test_maximal_modules(tree_root, graph):
# test whether the mdoules are maximal in nature
return False

return True

# Function implemented for testing
def test_maximal_modules(tree_root, graph):
def _test_maximal_modules(tree_root, graph):
r"""
Test the maximal nature of modules in a modular decomposition tree.
Expand All @@ -2770,9 +2773,10 @@ def test_maximal_modules(tree_root, graph):
EXAMPLES::
sage: from sage.graphs.graph_decompositions.modular_decomposition import *
sage: from sage.graphs.graph_decompositions.modular_decomposition import (
....: modular_decomposition, _test_maximal_modules)
sage: g = graphs.HexahedralGraph()
sage: test_maximal_modules(modular_decomposition(g), g)
sage: _test_maximal_modules(modular_decomposition(g), g)
True
"""
if tree_root.node_type != NodeType.NORMAL:
Expand Down Expand Up @@ -2903,7 +2907,7 @@ def form_module(index, other_index, tree_root, graph):
break

# Function implemented for testing
def test_module(module, graph):
def _test_module(module, graph):
"""
Test whether input module is actually a module
Expand All @@ -2919,12 +2923,13 @@ def test_module(module, graph):
EXAMPLES::
sage: from sage.graphs.graph_decompositions.modular_decomposition import *
sage: from sage.graphs.graph_decompositions.modular_decomposition import (
....: modular_decomposition, _test_module)
sage: g = graphs.HexahedralGraph()
sage: tree_root = modular_decomposition(g)
sage: test_module(tree_root, g)
sage: _test_module(tree_root, g)
True
sage: test_module(tree_root.children[0], g)
sage: _test_module(tree_root.children[0], g)
True
"""
# A single vertex is a module
Expand Down Expand Up @@ -3212,7 +3217,7 @@ def relabel_tree(root, perm):

from sage.misc.random_testing import random_testing
@random_testing
def test_gamma_modules(trials, vertices, prob, verbose=False):
def _test_gamma_modules(trials, vertices, prob, verbose=False):
r"""
Verify that the vertices of each gamma class of a random graph are modules
of that graph.
Expand All @@ -3230,8 +3235,8 @@ def test_gamma_modules(trials, vertices, prob, verbose=False):
EXAMPLES::
sage: from sage.graphs.graph_decompositions.modular_decomposition import *
sage: test_gamma_modules(3, 7, 0.5)
sage: from sage.graphs.graph_decompositions.modular_decomposition import _test_gamma_modules
sage: _test_gamma_modules(3, 7, 0.5)
"""
from sage.graphs.generators.random import RandomGNP
for _ in range(trials):
Expand Down Expand Up @@ -3272,8 +3277,8 @@ def permute_decomposition(trials, algorithm, vertices, prob, verbose=False):
print(random_perm)
t1 = algorithm(g1)
t2 = algorithm(g2)
assert(test_modular_decomposition(t1, g1))
assert(test_modular_decomposition(t2, g2))
assert _test_modular_decomposition(t1, g1)
assert _test_modular_decomposition(t2, g2)
t1p = relabel_tree(t1, random_perm)
assert(equivalent_trees(t1p, t2))
if verbose:
Expand Down
30 changes: 15 additions & 15 deletions src/sage/homology/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
TESTS::
sage: from sage.homology.tests import test_random_chain_complex
sage: test_random_chain_complex(trials=20) # optional - CHomP
sage: test_random_chain_complex(level=2, trials=20) # optional - CHomP
sage: test_random_chain_complex(level=3, trials=20) # long time # optional - CHomP
sage: from sage.homology.tests import test_random_simplicial_complex
sage: test_random_simplicial_complex(level=1, trials=20) # optional - CHomP
sage: test_random_simplicial_complex(level=2, trials=20) # optional - CHomP
sage: test_random_simplicial_complex(level=5/2, trials=10) # long time # optional - CHomP
sage: from sage.homology.tests import _test_random_chain_complex
sage: _test_random_chain_complex(trials=20) # optional - CHomP
sage: _test_random_chain_complex(level=2, trials=20) # optional - CHomP
sage: _test_random_chain_complex(level=3, trials=20) # long time # optional - CHomP
sage: from sage.homology.tests import _test_random_simplicial_complex
sage: _test_random_simplicial_complex(level=1, trials=20) # optional - CHomP
sage: _test_random_simplicial_complex(level=2, trials=20) # optional - CHomP
sage: _test_random_simplicial_complex(level=5/2, trials=10) # long time # optional - CHomP
"""

from sage.misc.random_testing import random_testing
Expand Down Expand Up @@ -56,7 +56,7 @@ def random_chain_complex(level=1):
return ChainComplex({dim: mat}, degree = deg)

@random_testing
def test_random_chain_complex(level=1, trials=1, verbose=False):
def _test_random_chain_complex(level=1, trials=1, verbose=False):
"""
Compute the homology of a random chain complex with and without
CHomP, and compare the results. If they are not the same, raise
Expand All @@ -72,8 +72,8 @@ def test_random_chain_complex(level=1, trials=1, verbose=False):
EXAMPLES::
sage: from sage.homology.tests import test_random_chain_complex
sage: test_random_chain_complex(trials=2) # optional - CHomP
sage: from sage.homology.tests import _test_random_chain_complex
sage: _test_random_chain_complex(trials=2) # optional - CHomP
"""
for i in range(trials):
C = random_chain_complex(level=level)
Expand Down Expand Up @@ -111,7 +111,7 @@ def random_simplicial_complex(level=1, p=0.5):
return RandomComplex(n, dim, p)

@random_testing
def test_random_simplicial_complex(level=1, trials=1, verbose=False):
def _test_random_simplicial_complex(level=1, trials=1, verbose=False):
"""
Compute the homology of a random simplicial complex with and
without CHomP, and compare the results. If they are not the same,
Expand All @@ -129,8 +129,8 @@ def test_random_simplicial_complex(level=1, trials=1, verbose=False):
EXAMPLES::
sage: from sage.homology.tests import test_random_simplicial_complex
sage: test_random_simplicial_complex(trials=2) # optional - CHomP
sage: from sage.homology.tests import _test_random_simplicial_complex
sage: _test_random_simplicial_complex(trials=2) # optional - CHomP
"""
for i in range(trials):
X = random_simplicial_complex(level=level)
Expand Down
Loading

0 comments on commit 24a0b17

Please sign in to comment.