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