Attrib: tag and select tests with attributes

Attribute selector plugin.

Oftentimes when testing you will want to select tests based on criteria rather then simply by filename. For example, you might want to run all tests except for the slow ones. You can do this with the Attribute selector plugin by setting attributes on your test methods. Here is an example:

def test_big_download():
    import urllib
    # commence slowness...

test_big_download.slow = 1

Once you’ve assigned an attribute slow = 1 you can exclude that test and all other tests having the slow attribute by running

$ nosetests -a '!slow'

There is also a decorator available for you that will set attributes. Here’s how to set slow=1 like above with the decorator:

from nose.plugins.attrib import attr
@attr('slow')
def test_big_download():
    import urllib
    # commence slowness...

And here’s how to set an attribute with a specific value:

from nose.plugins.attrib import attr
@attr(speed='slow')
def test_big_download():
    import urllib
    # commence slowness...

This test could be run with

$ nosetests -a speed=slow

Below is a reference to the different syntaxes available.

Simple syntax

Examples of using the -a and --attr options:

  • nosetests -a status=stable

    Only runs tests with attribute “status” having value “stable”

  • nosetests -a priority=2,status=stable

    Runs tests having both attributes and values

  • nosetests -a priority=2 -a slow

    Runs tests that match either attribute

  • nosetests -a tags=http

    If a test’s tags attribute was a list and it contained the value http then it would be run

  • nosetests -a slow

    Runs tests with the attribute slow if its value does not equal False (False, [], “”, etc...)

  • nosetests -a '!slow'

    Runs tests that do NOT have the attribute slow or have a slow attribute that is equal to False NOTE: if your shell (like bash) interprets ‘!’ as a special character make sure to put single quotes around it.

Expression Evaluation

Examples using the -A and --eval-attr options:

  • nosetests -A "not slow" Evaluates the Python expression “not slow” and runs the test if True
  • nosetests -A "(priority > 5) and not slow" Evaluates a complex Python expression and runs the test if True

Options

-a=ATTR, --attr=ATTR

Run only tests that have attributes specified by ATTR [NOSE_ATTR]

-A=EXPR, --eval-attr=EXPR

Run only tests for whose attributes the Python expression EXPR evaluates to True [NOSE_EVAL_ATTR]

Plugin

class nose.plugins.attrib.AttributeSelector

Bases: nose.plugins.base.Plugin

Selects test cases to be run based on their attributes.

configure(options, config)

Configure the plugin and system, based on selected options.

attr and eval_attr may each be lists.

self.attribs will be a list of lists of tuples. In that list, each list is a group of attributes, all of which must match for the rule to match.

options(parser, env)

Register command line options

wantClass(cls)

Accept the class if the class or any method is wanted.

wantFunction(function)

Accept the function if its attributes match.

wantMethod(method)

Accept the method if its attributes match.

Source

Table Of Contents

Previous topic

AllModules: collect tests in all modules

Next topic

Capture: capture stdout during tests

This Page