-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathtest_examples.py
More file actions
61 lines (47 loc) · 1.91 KB
/
test_examples.py
File metadata and controls
61 lines (47 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
test_examples.py
"""
from inspect import getmembers, isfunction
import pytest
from slycot import examples
examplefunctions = [fun for (fname, fun) in getmembers(examples)
if isfunction(fun) and "_example" in fname]
# Exempt certain functions to produce warnings with attributes (iwarn, info)
#
# Failed to compute beta(A) within the specified tolerance
examples.ab13fd_example.allow_iwarninfo = [(None, 1)]
def check_warn(recwarn, examplefun):
"""Returns True if a warning occurs that is not exempt"""
for w in recwarn:
try:
if (w.message.iwarn, w.message.info) in examplefun.allow_iwarninfo:
continue
except AttributeError:
pass
return True
@pytest.mark.parametrize('examplefun', examplefunctions)
#ignore numpy ABI change warnings https://github.com/numpy/numpy/pull/432
@pytest.mark.filterwarnings("ignore:numpy.dtype size changed")
@pytest.mark.filterwarnings("ignore:numpy.ufunc size changed")
def test_example(examplefun, capsys, recwarn):
"""
Test the examples.
Test that all the examples work, produce some (unchecked) output but no
exceptions or warnings.
"""
examplefun()
captured = capsys.readouterr()
# fail for first in order
failconditions = [
((not len(captured.out) > 0), "Example {} did not print any results\n"),
(captured.err, "Example {} wrote to stderr\n"),
(check_warn(recwarn, examplefun), "Example {} produced a warning.\n")]
for failed, msgfmt in failconditions:
if failed:
pytest.fail(msgfmt.format(examplefun.__name__) +
"Captured output:\n{}\n"
"Captured stderr:\n{}\n"
"Captured warnings:\n{}\n"
"".format(captured.out,
captured.err,
[w.message for w in recwarn]))