source: sasmodels/conftest.py @ f354e46

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

py.test needs kernelcl import to succeed even if pyopencl is not available

  • Property mode set to 100644
File size: 3.1 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
23USE_DOCSTRING_AS_DESCRIPTION = True
24def pytest_collection_modifyitems(session, config, items):
25    """
26    Add description to the test node id if item is a function and function
27    has a description attribute or __doc__ attribute.
28    """
29    for item in items:
30        #print(item.nodeid, type(item))
31        #for attr in dir(item): not attr.startswith('__') and print(attr, getattr(item, attr))
32        if isinstance(item, pytest.Function):
33            if isinstance(item, TestCaseFunction):
34                # TestCase uses item.name to find the method so skip
35                continue
36            function = item.obj
37
38            # If the test case provides a "description" attribute then use it
39            # as an extended description.  If there is no description attribute,
40            # then perhaps use the test docstring.
41            if USE_DOCSTRING_AS_DESCRIPTION:
42                description = getattr(function, 'description', function.__doc__)
43            else:
44                description = getattr(function, 'description', "")
45
46            # If description is not supplied but yield args are, then use the
47            # yield args for the description
48            if not description and getattr(item, '_args', ()):
49                description = str(item._args) if len(item._args) > 1 else str(item._args[0])
50            #print(item.nodeid, description, item._args)
51
52            if description:
53                # Strip spaces from start and end and strip dots from end
54                # pytest converts '.' to '::' on output for some reason.
55                description = description.strip().rstrip('.')
56                # Join multi-line descriptions into a single line
57                if '\n' in description:
58                    description = " ".join(line.strip() for line in description.split('\n'))
59
60            # Set the description as part of the node identifier.
61            if description:
62                #print(type(item), dir(item))
63                #print(item.nodeid, description)
64                #print(item.location, item.name, item.nodeid, item.originalname)
65                # Note: leave the current name mostly as-is since the prefix
66                # is needed to specify the nth test from a list of tests.
67                #print("updating with", description)
68                item.name += "::" + description
69            #print("=>", item.nodeid)
Note: See TracBrowser for help on using the repository browser.