blob: 9878b67f121850e66922018b70b0a50ffceffd45 [file] [log] [blame]
Attila Fazekas23fdf1d2013-06-09 16:35:23 +02001Tempest Coding Guide
2====================
3
Joe Gordon1374f882013-07-12 17:00:34 +01004- Step 1: Read the OpenStack Style Commandments
5 https://github.com/openstack-dev/hacking/blob/master/HACKING.rst
6- Step 2: Read on
7
8Tempest Specific Commandments
9------------------------------
10
11[T101] If a test is broken because of a bug it is appropriate to skip the test until
12bug has been fixed. However, the skip message should be formatted so that
13Tempest's skip tracking tool can watch the bug status. The skip message should
14contain the string 'Bug' immediately followed by a space. Then the bug number
15should be included in the message '#' in front of the number.
16
17Example::
18
19 @testtools.skip("Skipped until the Bug #980688 is resolved")
20
21- [T102] Cannot import OpenStack python clients in tempest/api tests
22- [T103] tempest/tests is deprecated
Matthew Treinish5e4c0f22013-09-10 18:38:28 +000023- [T104] Scenario tests require a services decorator
Attila Fazekas23fdf1d2013-06-09 16:35:23 +020024
Matthew Treinish8b372892012-12-07 17:13:16 -050025Test Data/Configuration
26-----------------------
27- Assume nothing about existing test data
28- Tests should be self contained (provide their own data)
29- Clean up test data at the completion of each test
30- Use configuration files for values that will vary by environment
31
32
Attila Fazekas10fd63d2013-07-04 18:38:21 +020033Exception Handling
34------------------
35According to the ``The Zen of Python`` the
Attila Fazekas58d23302013-07-24 10:25:02 +020036``Errors should never pass silently.``
Attila Fazekas10fd63d2013-07-04 18:38:21 +020037Tempest usually runs in special environment (jenkins gate jobs), in every
38error or failure situation we should provide as much error related
39information as possible, because we usually do not have the chance to
40investigate the situation after the issue happened.
41
42In every test case the abnormal situations must be very verbosely explained,
43by the exception and the log.
44
45In most cases the very first issue is the most important information.
46
47Try to avoid using ``try`` blocks in the test cases, both the ``except``
48and ``finally`` block could replace the original exception,
49when the additional operations leads to another exception.
50
51Just letting an exception to propagate, is not bad idea in a test case,
52 at all.
53
54Try to avoid using any exception handling construct which can hide the errors
55origin.
56
57If you really need to use a ``try`` block, please ensure the original
58exception at least logged. When the exception is logged you usually need
59to ``raise`` the same or a different exception anyway.
60
Chris Yeohc2ff7272013-07-22 22:25:25 +093061Use of ``self.addCleanup`` is often a good way to avoid having to catch
62exceptions and still ensure resources are correctly cleaned up if the
63test fails part way through.
64
Attila Fazekas10fd63d2013-07-04 18:38:21 +020065Use the ``self.assert*`` methods provided by the unit test framework
66 the signal failures early.
67
68Avoid using the ``self.fail`` alone, it's stack trace will signal
69 the ``self.fail`` line as the origin of the error.
70
71Avoid constructing complex boolean expressions for assertion.
Attila Fazekas7899d312013-08-16 09:18:17 +020072The ``self.assertTrue`` or ``self.assertFalse`` without a ``msg`` argument,
73will just tell you the single boolean value, and you will not know anything
74about the values used in the formula, the ``msg`` argument might be good enough
75for providing more information.
76
77Most other assert method can include more information by default.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020078For example ``self.assertIn`` can include the whole set.
79
Attila Fazekas7899d312013-08-16 09:18:17 +020080Recommended to use testtools matcher for more tricky assertion.
81`[doc] <http://testtools.readthedocs.org/en/latest/for-test-authors.html#matchers>`_
82
83You can implement your own specific matcher as well.
84`[doc] <http://testtools.readthedocs.org/en/latest/for-test-authors.html#writing-your-own-matchers>`_
85
Attila Fazekas10fd63d2013-07-04 18:38:21 +020086If the test case fails you can see the related logs and the information
87carried by the exception (exception class, backtrack and exception info).
88This and the service logs are your only guide to find the root cause of flaky
89issue.
90
Attila Fazekas7899d312013-08-16 09:18:17 +020091Test cases are independent
92--------------------------
93Every ``test_method`` must be callable individually and MUST NOT depends on,
94any other ``test_method`` or ``test_method`` ordering.
95
96Test cases MAY depend on commonly initialized resources/facilities, like
97credentials management, testresources and so on. These facilities, MUST be able
98to work even if just one ``test_method`` selected for execution.
99
Matthew Treinish5e4c0f22013-09-10 18:38:28 +0000100Service Tagging
101---------------
102Service tagging is used to specify which services are exercised by a particular
103test method. You specify the services with the tempest.test.services decorator.
104For example:
105
106@services('compute', 'image')
107
108Valid service tag names are the same as the list of directories in tempest.api
109that have tests.
110
111For scenario tests having a service tag is required. For the api tests service
112tags are only needed if the test method makes an api call (either directly or
113indirectly through another service) that differs from the parent directory
114name. For example, any test that make an api call to a service other than nova
115in tempest.api.compute would require a service tag for those services, however
116they do not need to be tagged as compute.
117
Chris Yeohc2ff7272013-07-22 22:25:25 +0930118Guidelines
119----------
120- Do not submit changesets with only testcases which are skipped as
121 they will not be merged.
122- Consistently check the status code of responses in testcases. The
123 earlier a problem is detected the easier it is to debug, especially
124 where there is complicated setup required.
Matthew Treinish96c28d12013-09-16 17:05:09 +0000125
DennyZhang900f02b2013-09-23 08:34:04 -0500126Parallel Test Execution
127-----------------------
Matthew Treinish96c28d12013-09-16 17:05:09 +0000128Tempest by default runs its tests in parallel this creates the possibility for
129interesting interactions between tests which can cause unexpected failures.
130Tenant isolation provides protection from most of the potential race conditions
131between tests outside the same class. But there are still a few of things to
132watch out for to try to avoid issues when running your tests in parallel.
133
134- Resources outside of a tenant scope still have the potential to conflict. This
135 is a larger concern for the admin tests since most resources and actions that
DennyZhang900f02b2013-09-23 08:34:04 -0500136 require admin privileges are outside of tenants.
Matthew Treinish96c28d12013-09-16 17:05:09 +0000137
138- Races between methods in the same class are not a problem because
139 parallelization in tempest is at the test class level, but if there is a json
140 and xml version of the same test class there could still be a race between
141 methods.
142
143- The rand_name() function from tempest.common.utils.data_utils should be used
144 anywhere a resource is created with a name. Static naming should be avoided
145 to prevent resource conflicts.
146
147- If the execution of a set of tests is required to be serialized then locking
148 can be used to perform this. See AggregatesAdminTest in
149 tempest.api.compute.admin for an example of using locking.