source: sasmodels/conftest.py @ 2ab1bac

core_shell_microgelsmagnetic_modelticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 2ab1bac was 2ab1bac, checked in by Paul Kienzle <pkienzle@…>, 6 years ago

include matplotlib in reqs; trying fixing opencl test for travis

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