blob: 03e7dc37505634afcea6a1afddded8924a8fd815 [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
Attila Fazekas23fdf1d2013-06-09 16:35:23 +020023
Matthew Treinish8b372892012-12-07 17:13:16 -050024Test Data/Configuration
25-----------------------
26- Assume nothing about existing test data
27- Tests should be self contained (provide their own data)
28- Clean up test data at the completion of each test
29- Use configuration files for values that will vary by environment
30
31
Attila Fazekas10fd63d2013-07-04 18:38:21 +020032Exception Handling
33------------------
34According to the ``The Zen of Python`` the
Attila Fazekas58d23302013-07-24 10:25:02 +020035``Errors should never pass silently.``
Attila Fazekas10fd63d2013-07-04 18:38:21 +020036Tempest usually runs in special environment (jenkins gate jobs), in every
37error or failure situation we should provide as much error related
38information as possible, because we usually do not have the chance to
39investigate the situation after the issue happened.
40
41In every test case the abnormal situations must be very verbosely explained,
42by the exception and the log.
43
44In most cases the very first issue is the most important information.
45
46Try to avoid using ``try`` blocks in the test cases, both the ``except``
47and ``finally`` block could replace the original exception,
48when the additional operations leads to another exception.
49
50Just letting an exception to propagate, is not bad idea in a test case,
51 at all.
52
53Try to avoid using any exception handling construct which can hide the errors
54origin.
55
56If you really need to use a ``try`` block, please ensure the original
57exception at least logged. When the exception is logged you usually need
58to ``raise`` the same or a different exception anyway.
59
Chris Yeohc2ff7272013-07-22 22:25:25 +093060Use of ``self.addCleanup`` is often a good way to avoid having to catch
61exceptions and still ensure resources are correctly cleaned up if the
62test fails part way through.
63
Attila Fazekas10fd63d2013-07-04 18:38:21 +020064Use the ``self.assert*`` methods provided by the unit test framework
65 the signal failures early.
66
67Avoid using the ``self.fail`` alone, it's stack trace will signal
68 the ``self.fail`` line as the origin of the error.
69
70Avoid constructing complex boolean expressions for assertion.
Attila Fazekas7899d312013-08-16 09:18:17 +020071The ``self.assertTrue`` or ``self.assertFalse`` without a ``msg`` argument,
72will just tell you the single boolean value, and you will not know anything
73about the values used in the formula, the ``msg`` argument might be good enough
74for providing more information.
75
76Most other assert method can include more information by default.
Attila Fazekas10fd63d2013-07-04 18:38:21 +020077For example ``self.assertIn`` can include the whole set.
78
Attila Fazekas7899d312013-08-16 09:18:17 +020079Recommended to use testtools matcher for more tricky assertion.
80`[doc] <http://testtools.readthedocs.org/en/latest/for-test-authors.html#matchers>`_
81
82You can implement your own specific matcher as well.
83`[doc] <http://testtools.readthedocs.org/en/latest/for-test-authors.html#writing-your-own-matchers>`_
84
Attila Fazekas10fd63d2013-07-04 18:38:21 +020085If the test case fails you can see the related logs and the information
86carried by the exception (exception class, backtrack and exception info).
87This and the service logs are your only guide to find the root cause of flaky
88issue.
89
Attila Fazekas7899d312013-08-16 09:18:17 +020090Test cases are independent
91--------------------------
92Every ``test_method`` must be callable individually and MUST NOT depends on,
93any other ``test_method`` or ``test_method`` ordering.
94
95Test cases MAY depend on commonly initialized resources/facilities, like
96credentials management, testresources and so on. These facilities, MUST be able
97to work even if just one ``test_method`` selected for execution.
98
Chris Yeohc2ff7272013-07-22 22:25:25 +093099Guidelines
100----------
101- Do not submit changesets with only testcases which are skipped as
102 they will not be merged.
103- Consistently check the status code of responses in testcases. The
104 earlier a problem is detected the easier it is to debug, especially
105 where there is complicated setup required.