[a69d8cd] | 1 | """ |
---|
[b3af1c2] | 2 | pytest hooks for sasmodels |
---|
[a69d8cd] | 3 | |
---|
[b3af1c2] | 4 | Hooks for running sasmodels tests via pytest. |
---|
[a69d8cd] | 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 |
---|
[b3af1c2] | 11 | test specifically from the pytest command line. [This is perhaps because |
---|
[a69d8cd] | 12 | the modifyitems hook is only called after test selection.] |
---|
[bb4ff2a] | 13 | |
---|
| 14 | *pytest_ignore_collect* skips kernelcl.py if pyopencl cannot be imported. |
---|
[a69d8cd] | 15 | """ |
---|
| 16 | from __future__ import print_function |
---|
| 17 | |
---|
[bb4ff2a] | 18 | import os.path |
---|
| 19 | |
---|
[a69d8cd] | 20 | import pytest |
---|
| 21 | from _pytest.unittest import TestCaseFunction |
---|
[335271e] | 22 | from _pytest.compat import is_generator |
---|
| 23 | |
---|
| 24 | def pytest_pycollect_makeitem(collector, name, obj): |
---|
| 25 | """ |
---|
| 26 | Convert test generator into list of function tests so that pytest doesn't |
---|
| 27 | complain about deprecated yield tests. |
---|
| 28 | |
---|
| 29 | Note that unlike nose, the tests are generated and saved instead of run |
---|
| 30 | immediately. This means that any dynamic context, such as a for-loop |
---|
| 31 | variable, must be captured by wrapping the yield result in a function call. |
---|
| 32 | |
---|
| 33 | For example:: |
---|
| 34 | |
---|
| 35 | for value in 1, 2, 3: |
---|
| 36 | for test in test_cases: |
---|
| 37 | yield test, value |
---|
| 38 | |
---|
| 39 | will need to be changed to:: |
---|
| 40 | |
---|
| 41 | def build_test(test, value): |
---|
| 42 | return test, value |
---|
| 43 | for value in 1, 2, 3: |
---|
| 44 | for test in test_cases: |
---|
| 45 | yield build_test(test, value) |
---|
| 46 | |
---|
| 47 | This allows the context (test and value) to be captured by lexical closure |
---|
| 48 | in build_test. See https://stackoverflow.com/a/233835/6195051. |
---|
| 49 | """ |
---|
| 50 | if collector.istestfunction(obj, name) and is_generator(obj): |
---|
[0e7611b] | 51 | tests = [] |
---|
| 52 | for number, yielded in enumerate(obj()): |
---|
| 53 | index, call, args = split_yielded_test(yielded, number) |
---|
| 54 | test = pytest.Function(name+index, collector, args=args, callobj=call) |
---|
| 55 | tests.append(test) |
---|
[335271e] | 56 | return tests |
---|
| 57 | |
---|
[0e7611b] | 58 | def split_yielded_test(obj, number): |
---|
| 59 | if not isinstance(obj, (tuple, list)): |
---|
| 60 | obj = (obj,) |
---|
| 61 | if not callable(obj[0]): |
---|
| 62 | index = "['%s']"%obj[0] |
---|
| 63 | obj = obj[1:] |
---|
| 64 | else: |
---|
| 65 | index = "[%d]"%number |
---|
| 66 | call, args = obj[0], obj[1:] |
---|
| 67 | return index, call, args |
---|
[a69d8cd] | 68 | |
---|
| 69 | USE_DOCSTRING_AS_DESCRIPTION = True |
---|
| 70 | def pytest_collection_modifyitems(session, config, items): |
---|
| 71 | """ |
---|
| 72 | Add description to the test node id if item is a function and function |
---|
| 73 | has a description attribute or __doc__ attribute. |
---|
| 74 | """ |
---|
| 75 | for item in items: |
---|
| 76 | if isinstance(item, pytest.Function): |
---|
| 77 | if isinstance(item, TestCaseFunction): |
---|
| 78 | # TestCase uses item.name to find the method so skip |
---|
| 79 | continue |
---|
| 80 | function = item.obj |
---|
| 81 | |
---|
| 82 | # If the test case provides a "description" attribute then use it |
---|
| 83 | # as an extended description. If there is no description attribute, |
---|
| 84 | # then perhaps use the test docstring. |
---|
| 85 | if USE_DOCSTRING_AS_DESCRIPTION: |
---|
| 86 | description = getattr(function, 'description', function.__doc__) |
---|
| 87 | else: |
---|
| 88 | description = getattr(function, 'description', "") |
---|
| 89 | |
---|
| 90 | # If description is not supplied but yield args are, then use the |
---|
| 91 | # yield args for the description |
---|
| 92 | if not description and getattr(item, '_args', ()): |
---|
[b3af1c2] | 93 | description = (str(item._args) if len(item._args) > 1 |
---|
| 94 | else str(item._args[0])) |
---|
[a69d8cd] | 95 | |
---|
[b3af1c2] | 96 | # Set the description as part of the node identifier. |
---|
[a69d8cd] | 97 | if description: |
---|
| 98 | # Strip spaces from start and end and strip dots from end |
---|
| 99 | # pytest converts '.' to '::' on output for some reason. |
---|
| 100 | description = description.strip().rstrip('.') |
---|
| 101 | # Join multi-line descriptions into a single line |
---|
| 102 | if '\n' in description: |
---|
[b3af1c2] | 103 | description = " ".join(line.strip() |
---|
| 104 | for line in description.split('\n')) |
---|
[a69d8cd] | 105 | |
---|
| 106 | # Note: leave the current name mostly as-is since the prefix |
---|
| 107 | # is needed to specify the nth test from a list of tests. |
---|
| 108 | item.name += "::" + description |
---|