Astropy Testing Tools

This section is primarily a reference for developers that want to understand or add to the Astropy testing machinery. See Testing Guidelines for an overview of running or writing the tests.

astropy.tests.helper Module

To ease development of tests that work with Astropy, the astropy.tests.helper module provides some utility functions to make tests that use Astropy conventions or classes easier to work with, e.g., functions to test for near-equality of Quantity objects.

The functionality here is not exhaustive, because much of the useful tools are either in the standard library, pytest, or numpy.testing. This module contains primarily functionality specific to the astropy core package or packages that follow the Astropy package template.

Reference/API

astropy.tests.helper Module

Functions

enable_deprecations_as_exceptions([…])

Turn on the feature that turns deprecations into exceptions.

treat_deprecations_as_exceptions()

Turn all DeprecationWarnings (which indicate deprecated uses of Python itself or Numpy, but not within Astropy, where we use our own deprecation warning class) into exceptions so that we find out about them early.

assert_follows_unicode_guidelines(x[, roundtrip])

Test that an object follows our Unicode policy.

assert_quantity_allclose(actual, desired[, …])

Raise an assertion if two objects are not equal up to desired tolerance.

check_pickling_recovery(original, protocol)

Try to pickle an object.

pickle_protocol(request)

Fixture to run all the tests for protocols 0 and 1, and -1 (most advanced).

generic_recursive_equality_test(a, b, …)

Check if the attributes of a and b are equal.

Classes

raises(exc)

A decorator to mark that a test should raise a given exception. Use as follows::.

catch_warnings(*classes)

A high-powered version of warnings.catch_warnings to use for testing and to make sure that there is no dependence on the order in which the tests are run.

Astropy Test Runner

When executing tests with astropy.test the call to pytest is controlled by the astropy.tests.runner.TestRunner class.

The TestRunner class is used to generate the astropy.test function, the test function generates a set of command line arguments to pytest. The arguments to pytest are defined in the run_tests method, the arguments to run_tests and their respective logic are defined in methods of TestRunner decorated with the keyword decorator. For an example of this see TestRunnerBase. This design makes it easy for packages to add or remove keyword arguments to their test runners, or define a whole new set of arguments by subclassing from TestRunnerBase.

Reference/API

class astropy.tests.runner.keyword(default_value=None, priority=0)[source]

A decorator to mark a method as keyword argument for the TestRunner.

Parameters
default_valueobject

The default value for the keyword argument. (Default: None)

priorityint

keyword argument methods are executed in order of descending priority.

class astropy.tests.runner.TestRunnerBase(*args, **kwargs)[source]

The base class for the TestRunner.

A test runner can be constructed by creating a subclass of this class and defining ‘keyword’ methods. These are methods that have the keyword decorator, these methods are used to construct allowed keyword arguments to the run_tests method as a way to allow customization of individual keyword arguments (and associated logic) without having to re-implement the whole run_tests method.

Examples

A simple keyword method:

class MyRunner(TestRunnerBase):

    @keyword('default_value'):
    def spam(self, spam, kwargs):
        """
        spam : `str`
            The parameter description for the run_tests docstring.
        """
        # Return value must be a list with a CLI parameter for pytest.
        return ['--spam={}'.format(spam)]
class astropy.tests.runner.TestRunner(*args, **kwargs)[source]

A test runner for astropy tests