Skip to content

Commit 553d8d4

Browse files
liamhuberpmrv
andauthored
[patch] Run Doctest (#18)
* Add doctest * Fix import alarm example * Modify deprecator example to ignore the stated output I can't get the test to nicely see the error message; since we anyhow have unit tests for this I am not hung up on whether the docstrings remain perfectly verbatim correct and am content to leave the printed warning as plain-text docstring instead of tested-code docstring. * Skip deprecate doctests since checking warnings is awkward * Revert to nicer output formatting Now that doctest ignores it anyway --------- Co-authored-by: Marvin Poul <poul@mpie.de>
1 parent bce8017 commit 553d8d4

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

pyiron_snippets/deprecate.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,33 @@ class Deprecator:
2424
If message and version are not initialized or given during the decorating
2525
call the respective parts are left out from the message.
2626
27+
>>> from pyiron_snippets.deprecate import Deprecator
28+
>>>
2729
>>> deprecate = Deprecator()
2830
>>> @deprecate
2931
... def foo(a, b):
3032
... pass
31-
>>> foo(1, 2)
33+
>>> foo(1, 2) # doctest: +SKIP
3234
DeprecationWarning: __main__.foo is deprecated
3335
3436
>>> @deprecate("use bar() instead")
3537
... def foo(a, b):
3638
... pass
37-
>>> foo(1, 2)
39+
>>> foo(1, 2) # doctest: +SKIP
3840
DeprecationWarning: __main__.foo is deprecated: use bar instead
3941
4042
>>> @deprecate("use bar() instead", version="0.4.0")
4143
... def foo(a, b):
4244
... pass
43-
>>> foo(1, 2)
44-
DeprecationWarning: __main__.foo is deprecated: use bar instead. It is not
45-
guaranteed to be in service in vers. 0.4.0
45+
>>> foo(1, 2) # doctest: +SKIP
46+
DeprecationWarning: __main__.foo is deprecated: use bar instead. It is not guaranteed to be in service in vers. 0.4.0
4647
4748
>>> deprecate = Deprecator(message="I say no!", version="0.5.0")
4849
>>> @deprecate
4950
... def foo(a, b):
5051
... pass
51-
>>> foo(1, 2)
52-
DeprecationWarning: __main__.foo is deprecated: I say no! It is not
53-
guaranteed to be in service in vers. 0.5.0
52+
>>> foo(1, 2) # doctest: +SKIP
53+
DeprecationWarning: __main__.foo is deprecated: I say no! It is not guaranteed to be in service in vers. 0.5.0`
5454
5555
Alternatively the decorator can also be called with `arguments` set to a dictionary
5656
mapping names of keyword arguments to deprecation messages. In this case the
@@ -62,7 +62,7 @@ class Deprecator:
6262
... def foo(bar=None, baz=None):
6363
... pass
6464
>>> foo(baz=True)
65-
>>> foo(bar=True)
65+
>>> foo(bar=True) # doctest: +SKIP
6666
DeprecationWarning: __main__.foo(bar=True) is deprecated: use baz instead.
6767
6868
As a short-cut, it is also possible to pass the values in the arguments dict
@@ -72,8 +72,9 @@ class Deprecator:
7272
... def foo(bar=None, baz=None):
7373
... pass
7474
>>> foo(baz=True)
75-
>>> foo(bar=True)
75+
>>> foo(bar=True) # doctest: +SKIP
7676
DeprecationWarning: __main__.foo(bar=True) is deprecated: use baz instead.
77+
7778
"""
7879

7980
def __init__(self, message=None, version=None, pending=False):

pyiron_snippets/import_alarm.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ class ImportAlarm:
2020
>>> try:
2121
... from mystery_package import Enigma, Puzzle, Conundrum
2222
... import_alarm = ImportAlarm()
23-
>>> except ImportError:
24-
>>> import_alarm = ImportAlarm(
23+
... except ImportError:
24+
... import_alarm = ImportAlarm(
2525
... "MysteryJob relies on mystery_package, but this was unavailable. Please ensure your python environment "
2626
... "has access to mystery_package, e.g. with `conda install -c conda-forge mystery_package`"
2727
... )
2828
...
2929
>>> class MysteryJob:
3030
... @import_alarm
31-
... def __init__(self, project, job_name)
31+
... def __init__(self, project, job_name):
3232
... super().__init__()
3333
... self.riddles = [Enigma(), Puzzle(), Conundrum()]
3434

tests/unit/test_docs.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import doctest
2+
import pkgutil
3+
import unittest
4+
5+
import pyiron_snippets
6+
7+
8+
def load_tests(loader, tests, ignore):
9+
for importer, name, ispkg in pkgutil.walk_packages(
10+
pyiron_snippets.__path__, pyiron_snippets.__name__ + '.'
11+
):
12+
tests.addTests(doctest.DocTestSuite(name))
13+
return tests
14+
15+
16+
class TestTriggerFromIDE(unittest.TestCase):
17+
"""
18+
Just so we can instruct it to run unit tests here with a gui run command on the file
19+
"""
20+
21+
def test_void(self):
22+
pass
23+
24+
25+
if __name__ == '__main__':
26+
unittest.main()

0 commit comments

Comments
 (0)