1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
===============================
Selecting tests with attributes
===============================
.. note ::
New in version 0.2
Filter tests by attribute, excluding any tests whose attributes do not
match any of the specified attributes.
Attributes may be simple values or lists, and may be attributes of a
test method (or function), a test case class, or the callable yielded
by a generator test.
Given the following test module, the attrib plugin can be used to
select tests in the following ways (and others!):
.. note ::
All examples assume the attrib plugin has been activated in a config file:
.. code-block :: ini
[unittest]
plugins = nose2.plugins.attrib
.. literalinclude :: attrib_example.py
:language: python
Select tests having an attribute
________________________________
Running nose2 like this::
nose2 -v -A fast
Runs these tests::
test_fast (attrib_example.Test) ... ok
test_faster (attrib_example.Test) ... ok
This selects all tests that define the attribute as any ``True`` value.
Select tests that do not have an attribute
__________________________________________
Running nose2 like this::
nose2 -v -A '!fast'
Runs these tests::
test_slow (attrib_example.Test) ... ok
test_slower (attrib_example.Test) ... ok
This selects all tests that define the attribute as a ``False`` value,
*and those tests that do not have the attribute at all*.
Select tests having an attribute with a particular value
--------------------------------------------------------
Running nose2 like this::
nose2 -v -A layer=2
Runs these tests::
test_fast (attrib_example.Test) ... ok
test_slow (attrib_example.Test) ... ok
This selects all tests that define the attribute with a matching
value. The attribute value of each test case is converted to a string
before comparison with the specified value. Comparison is
case-insensitive.
Select tests having a value in a list attribute
-----------------------------------------------
Running nose2 like this::
nose2 -v -A flags=red
Runs these tests::
test_faster (attrib_example.Test) ... ok
test_slower (attrib_example.Test) ... ok
Since the ``flags`` attribute is a list, this test selects all tests
with the value ``red`` in their ``flags`` attribute. Comparison done
after string conversion and is case-insensitive.
Select tests that do not have a value in a list attribute
---------------------------------------------------------
Running nose2 like this::
nose2 -v -A '!flags=red'
Runs these tests::
test_fast (attrib_example.Test) ... ok
The result in this case can be somewhat counter-intuitive. What the
``attrib`` plugin selects when you negate an attribute that is in a list
are only those tests that *have the list attribute* but *without the
value* specified. Tests that do not have the attribute at all are
*not* selected.
Select tests using Python expressions
-------------------------------------
For more complex cases, you can use the :option:`-E` command-line
option to pass a Python expression that will be evaluated in the
context of each test case. Only those test cases where the expression
evaluates to ``True`` (and don't raise an exception) will be selected.
Running nose2 like this::
nose2 -v -E '"blue" in flags and layer > 2'
Runs only one test::
test_slower (attrib_example.Test) ... ok
.. autoplugin :: nose2.plugins.attrib.AttributeSelector
|