Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix doctests to work with pytest. #802

Merged
merged 10 commits into from
Oct 30, 2021
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to

### Fixed
* Added error checking for `r_min`, `r_max` arguments in `freud.density.RDF` and `freud.locality.NeighborList`.
* Doctests are now run with pytest.

## v2.7.0 -- 2021-10-01

Expand Down
1 change: 1 addition & 0 deletions doc/source/reference/credits.rst
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ Bradley Dice - **Lead developer**
* Contributed code, design, documentation, and testing for ``StaticStructureFactorDebye`` class.
* Fixed ``Box.contains`` to run in linear time, ``O(num_points)``.
* Contributed code, design, documentation, and testing for ``StaticStructureFactorDirect`` class.
* Fixed doctests to run with pytest.

Eric Harper, University of Michigan - **Former lead developer**

Expand Down
23 changes: 21 additions & 2 deletions freud/diffraction.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,22 @@ cdef class StaticStructureFactorDebye(_StaticStructureFactor):

Example for a single component system::

>>> box, points = freud.data.make_random_system(10, 100, seed=0)
>>> sf = freud.diffraction.StaticStructureFactorDebye(
... bins=100, k_max=10, k_min=0
... )
>>> sf.compute((box, points))
freud.diffraction.StaticStructureFactorDebye(...)

Example for partial mixed structure factor for a multiple component
system with types A and B::

>>> N_particles = 100
>>> box, points = freud.data.make_random_system(
... 10, N_particles, seed=0
... )
>>> A_points = points[:N_particles//2]
>>> B_points = points[N_particles//2:]
>>> sf = freud.diffraction.StaticStructureFactorDebye(
... bins=100, k_max=10, k_min=0
... )
Expand All @@ -200,6 +208,7 @@ cdef class StaticStructureFactorDebye(_StaticStructureFactor):
... query_points=B_points,
... N_total=N_particles
... )
freud.diffraction.StaticStructureFactorDebye(...)

Args:
system:
Expand Down Expand Up @@ -352,21 +361,31 @@ cdef class StaticStructureFactorDirect(_StaticStructureFactor):

Example for a single component system::

>>> box, points = freud.data.make_random_system(10, 100, seed=0)
>>> sf = freud.diffraction.StaticStructureFactorDirect(
... bins=100, k_max=10, k_min=0
... )
>>> sf.compute((box, points))
freud.diffraction.StaticStructureFactorDirect(...)

Example for partial mixed structure factor for multiple component
system AB::
system with types A and B::

>>> N_particles = 100
>>> box, points = freud.data.make_random_system(
... 10, N_particles, seed=0
... )
>>> A_points = points[:N_particles//2]
>>> B_points = points[N_particles//2:]
>>> sf = freud.diffraction.StaticStructureFactorDirect(
... bins=100, k_max=10, k_min=0
... )
>>> sf.compute(
... (box, A_points),
... query_points=B_points, N_total=N_particles
... query_points=B_points,
... N_total=N_particles
... )
freud.diffraction.StaticStructureFactorDirect(...)

Args:
system:
Expand Down
6 changes: 3 additions & 3 deletions requirements/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
ase==3.22.0
cmake==3.21.3
codecov==2.1.12
coverage==5.5
coverage==6.1
cython==0.29.24
dynasor==1.1b0; platform_system != "Windows"
dynasor==1.1.1; platform_system != "Windows"
garnett==0.7.1
GitPython==3.1.24
gsd==2.4.2
matplotlib>=3.0.0
numpy==1.21.2
pillow==8.3.2
pytest==6.2.5
pytest-cov==2.12.1
pytest-cov==3.0.0
rowan==1.3.0.post1
scikit-build==0.12.0
scipy==1.7.1
Expand Down
24 changes: 20 additions & 4 deletions tests/test_doctests.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
import doctest
import inspect

import pytest

import freud


def load_tests(loader, tests, ignore):
optionflags = doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE
def fetch_doctests():
finder = doctest.DocTestFinder()
for name, member in inspect.getmembers(freud):
if inspect.ismodule(member):
tests.addTests(doctest.DocTestSuite(member, optionflags=optionflags))
return tests
for docstring in finder.find(member):
if docstring.examples:
yield docstring


class TestDoctests:
@pytest.mark.parametrize(
"docstring", fetch_doctests(), ids=lambda docstring: docstring.name
)
def test_docstring(self, docstring):
optionflags = doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE
runner = doctest.DocTestRunner(optionflags=optionflags)
runner.run(docstring)
results = runner.summarize()
if results.failed:
raise AssertionError(results)