source: sasmodels/conftest.py @ a69d8cd

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

add support for pytest and use it on travis/appveyor

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