Debian Codegraph @

docs/such_dsl.rst

raw
1 ======================================
2 Such: a Functional-Test Friendly DSL
3 ======================================
4
5 .. note ::
6
7 New in version 0.4
8
9 Such is a DSL for writing tests with expensive, nested fixtures --
10 which typically means functional tests. It requires the layers plugin
11 (see :doc:`plugins/layers`).
12
13 What does it look like?
14 =======================
15
16 Unlike some python testing DSLs, such is just plain old python.
17
18 .. literalinclude :: ../nose2/tests/functional/support/such/test_such.py
19 :language: python
20
21 The tests it defines are unittest tests, and can be used with nose2
22 with just the layers plugin. You also have the option of activating a
23 reporting plugin (:class:`nose2.plugins.layers.LayerReporter`) to
24 provide a more discursive brand of output:
25
26 .. literalinclude :: ../nose2/tests/functional/support/such/output.txt
27
28 How does it work?
29 =================
30
31 Such uses the things in python that are most like anonymous code
32 blocks to allow you to construct tests with meaningful names and
33 deeply-nested fixtures. Compared to DSLs in languages that do allow
34 blocks, it is a little bit more verbose -- the block-like decorators
35 that mark fixture methods and test cases need to decorate *something*,
36 so each fixture and test case has to have a function definition. You
37 can use the same function name over and over here, or give each
38 function a meaningful name.
39
40 The set of tests begins with a description of the system under test as
41 a whole, marked with the ``A`` context manager:
42
43 .. code-block :: python
44
45 from nose2.tools import such
46
47 with such.A('system described here') as it:
48 # ...
49
50 Groups of tests are marked by the ``having`` context manager:
51
52 .. code-block :: python
53
54 with it.having('a description of a group'):
55 # ...
56
57 Within a test group (including the top-level group), fixtures are
58 marked with decorators:
59
60 .. code-block :: python
61
62 @it.has_setup
63 def setup():
64 # ...
65
66 @it.has_test_setup
67 def setup_each_test_case():
68 # ...
69
70 And tests are likewise marked with the ``should`` decorator:
71
72 .. code-block :: python
73
74 @it.should('exhibit the behavior described here')
75 def test(case):
76 # ...
77
78 Test cases may optionally take one argument. If they do, they will be
79 passed the :class:`unittest.TestCase` instance generated for the
80 test. They can use this ``TestCase`` instance to execute assert methods,
81 among other things. Test functions can also call assert methods on the
82 top-level scenario instance, if they don't take the ``case`` argument:
83
84 .. code-block :: python
85
86 @it.should("be able to use the scenario's assert methods")
87 def test():
88 it.assertEqual(something, 'a value')
89
90 @it.should("optionally take an argument")
91 def test(case):
92 case.assertEqual(case.attribute, 'some value')
93
94 Finally, to actually generate tests, you **must** call ``createTests`` on
95 the top-level scenario instance:
96
97 .. code-block :: python
98
99 it.createTests(globals())
100
101 This call generates the :class:`unittest.TestCase` instances for all
102 of the tests, and the layer classes that hold the fixtures defined in
103 the test groups. See :doc:`plugins/layers` for more about test
104 layers.
105
106 Running tests
107 -------------
108
109 Since order is often significant in functional tests, **such DSL tests
110 always execute in the order in which they are defined in the
111 module**. Parent groups run before child groups, and sibling groups
112 and sibling tests within a group execute in the order in which they
113 are defined.
114
115 Otherwise, tests written in the such DSL are collected and run just like any
116 other tests, with one exception: their names. The name of a such test
117 case is the name of its immediately surrounding group, plus the
118 description of the test, prepended with ``test ####:``, where ``####``
119 is the test's (``0`` -indexed) position within its group.
120
121 To run a case individually, you must pass in this full name -- usually you'll have to quote it. For example, to run the case ``should do more things``
122 defined above (assuming the layers plugin is activated by a config
123 file, and the test module is in the normal path of test collection),
124 you would run nose2 like this::
125
126 nose2 "test_such.having an expensive fixture.test 0000: should do more things"
127
128 That is, for the generated test case, the **group description** is
129 the **class name**, and the **test case description** is the **test
130 case name**. As you can see if you run an individual test with the
131 layer reporter active, all of the group fixtures execute in proper
132 order when a test is run individually::
133
134 $ nose2 "test_such.having an expensive fixture.test 0000: should do more things"
135 A system with complex setup
136 having an expensive fixture
137 should do more things ... ok
138
139 ----------------------------------------------------------------------
140 Ran 1 test in 0.000s
141
142 OK
143
144
145 Reference
146 =========
147
148 .. automodule :: nose2.tools.such
149 :members: A, Scenario