1 | """ |
---|
2 | py.test hooks for sasmodels |
---|
3 | |
---|
4 | Hooks for running sasmodels tests via py.test. |
---|
5 | |
---|
6 | *pytest_collection_modifyitems* adds the test description to the end of |
---|
7 | the test name. This is needed for the generated list of tests is sasmodels, |
---|
8 | where each test has a description giving the name of the model. For example |
---|
9 | "model_tests::[3]" becomes "model_tests::[3]::bcc_paracrystal-dll". Need to |
---|
10 | leave the "::[3]" in the name since that is the way you can indicate this |
---|
11 | test specifically from the py.test command line. [This is perhaps because |
---|
12 | the modifyitems hook is only called after test selection.] |
---|
13 | |
---|
14 | *pytest_ignore_collect* skips kernelcl.py if pyopencl cannot be imported. |
---|
15 | """ |
---|
16 | from __future__ import print_function |
---|
17 | |
---|
18 | import os.path |
---|
19 | |
---|
20 | import pytest |
---|
21 | from _pytest.unittest import TestCaseFunction |
---|
22 | |
---|
23 | try: |
---|
24 | import pyopencl |
---|
25 | TEST_PYOPENCL = True |
---|
26 | except ImportError: |
---|
27 | TEST_PYOPENCL = False |
---|
28 | |
---|
29 | def pytest_ignore_collect(path, config): |
---|
30 | ignore = TEST_PYOPENCL and path.basename == "kernelcl.py" |
---|
31 | return ignore |
---|
32 | |
---|
33 | USE_DOCSTRING_AS_DESCRIPTION = True |
---|
34 | def pytest_collection_modifyitems(session, config, items): |
---|
35 | """ |
---|
36 | Add description to the test node id if item is a function and function |
---|
37 | has a description attribute or __doc__ attribute. |
---|
38 | """ |
---|
39 | for item in items: |
---|
40 | #print(item.nodeid, type(item)) |
---|
41 | #for attr in dir(item): not attr.startswith('__') and print(attr, getattr(item, attr)) |
---|
42 | if isinstance(item, pytest.Function): |
---|
43 | if isinstance(item, TestCaseFunction): |
---|
44 | # TestCase uses item.name to find the method so skip |
---|
45 | continue |
---|
46 | function = item.obj |
---|
47 | |
---|
48 | # If the test case provides a "description" attribute then use it |
---|
49 | # as an extended description. If there is no description attribute, |
---|
50 | # then perhaps use the test docstring. |
---|
51 | if USE_DOCSTRING_AS_DESCRIPTION: |
---|
52 | description = getattr(function, 'description', function.__doc__) |
---|
53 | else: |
---|
54 | description = getattr(function, 'description', "") |
---|
55 | |
---|
56 | # If description is not supplied but yield args are, then use the |
---|
57 | # yield args for the description |
---|
58 | if not description and getattr(item, '_args', ()): |
---|
59 | description = str(item._args) if len(item._args) > 1 else str(item._args[0]) |
---|
60 | #print(item.nodeid, description, item._args) |
---|
61 | |
---|
62 | if description: |
---|
63 | # Strip spaces from start and end and strip dots from end |
---|
64 | # pytest converts '.' to '::' on output for some reason. |
---|
65 | description = description.strip().rstrip('.') |
---|
66 | # Join multi-line descriptions into a single line |
---|
67 | if '\n' in description: |
---|
68 | description = " ".join(line.strip() for line in description.split('\n')) |
---|
69 | |
---|
70 | # Set the description as part of the node identifier. |
---|
71 | if description: |
---|
72 | #print(type(item), dir(item)) |
---|
73 | #print(item.nodeid, description) |
---|
74 | #print(item.location, item.name, item.nodeid, item.originalname) |
---|
75 | # Note: leave the current name mostly as-is since the prefix |
---|
76 | # is needed to specify the nth test from a list of tests. |
---|
77 | #print("updating with", description) |
---|
78 | item.name += "::" + description |
---|
79 | #print("=>", item.nodeid) |
---|