Attila Fazekas | 23fdf1d | 2013-06-09 16:35:23 +0200 | [diff] [blame] | 1 | Tempest Coding Guide |
| 2 | ==================== |
| 3 | |
Joe Gordon | 1374f88 | 2013-07-12 17:00:34 +0100 | [diff] [blame] | 4 | - Step 1: Read the OpenStack Style Commandments |
| 5 | https://github.com/openstack-dev/hacking/blob/master/HACKING.rst |
| 6 | - Step 2: Read on |
| 7 | |
| 8 | Tempest Specific Commandments |
| 9 | ------------------------------ |
| 10 | |
| 11 | [T101] If a test is broken because of a bug it is appropriate to skip the test until |
| 12 | bug has been fixed. However, the skip message should be formatted so that |
| 13 | Tempest's skip tracking tool can watch the bug status. The skip message should |
| 14 | contain the string 'Bug' immediately followed by a space. Then the bug number |
| 15 | should be included in the message '#' in front of the number. |
| 16 | |
| 17 | Example:: |
| 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 Fazekas | 23fdf1d | 2013-06-09 16:35:23 +0200 | [diff] [blame] | 23 | |
Matthew Treinish | 8b37289 | 2012-12-07 17:13:16 -0500 | [diff] [blame] | 24 | Test 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 Fazekas | 10fd63d | 2013-07-04 18:38:21 +0200 | [diff] [blame] | 32 | Exception Handling |
| 33 | ------------------ |
| 34 | According to the ``The Zen of Python`` the |
Attila Fazekas | 58d2330 | 2013-07-24 10:25:02 +0200 | [diff] [blame] | 35 | ``Errors should never pass silently.`` |
Attila Fazekas | 10fd63d | 2013-07-04 18:38:21 +0200 | [diff] [blame] | 36 | Tempest usually runs in special environment (jenkins gate jobs), in every |
| 37 | error or failure situation we should provide as much error related |
| 38 | information as possible, because we usually do not have the chance to |
| 39 | investigate the situation after the issue happened. |
| 40 | |
| 41 | In every test case the abnormal situations must be very verbosely explained, |
| 42 | by the exception and the log. |
| 43 | |
| 44 | In most cases the very first issue is the most important information. |
| 45 | |
| 46 | Try to avoid using ``try`` blocks in the test cases, both the ``except`` |
| 47 | and ``finally`` block could replace the original exception, |
| 48 | when the additional operations leads to another exception. |
| 49 | |
| 50 | Just letting an exception to propagate, is not bad idea in a test case, |
| 51 | at all. |
| 52 | |
| 53 | Try to avoid using any exception handling construct which can hide the errors |
| 54 | origin. |
| 55 | |
| 56 | If you really need to use a ``try`` block, please ensure the original |
| 57 | exception at least logged. When the exception is logged you usually need |
| 58 | to ``raise`` the same or a different exception anyway. |
| 59 | |
Chris Yeoh | c2ff727 | 2013-07-22 22:25:25 +0930 | [diff] [blame] | 60 | Use of ``self.addCleanup`` is often a good way to avoid having to catch |
| 61 | exceptions and still ensure resources are correctly cleaned up if the |
| 62 | test fails part way through. |
| 63 | |
Attila Fazekas | 10fd63d | 2013-07-04 18:38:21 +0200 | [diff] [blame] | 64 | Use the ``self.assert*`` methods provided by the unit test framework |
| 65 | the signal failures early. |
| 66 | |
| 67 | Avoid using the ``self.fail`` alone, it's stack trace will signal |
| 68 | the ``self.fail`` line as the origin of the error. |
| 69 | |
| 70 | Avoid constructing complex boolean expressions for assertion. |
| 71 | The ``self.assertTrue`` or ``self.assertFalse`` will just tell you the |
| 72 | single boolean, and you will not know anything about the values used in |
| 73 | the formula. Most other assert method can include more information. |
| 74 | For example ``self.assertIn`` can include the whole set. |
| 75 | |
| 76 | If the test case fails you can see the related logs and the information |
| 77 | carried by the exception (exception class, backtrack and exception info). |
| 78 | This and the service logs are your only guide to find the root cause of flaky |
| 79 | issue. |
| 80 | |
Chris Yeoh | c2ff727 | 2013-07-22 22:25:25 +0930 | [diff] [blame] | 81 | Guidelines |
| 82 | ---------- |
| 83 | - Do not submit changesets with only testcases which are skipped as |
| 84 | they will not be merged. |
| 85 | - Consistently check the status code of responses in testcases. The |
| 86 | earlier a problem is detected the easier it is to debug, especially |
| 87 | where there is complicated setup required. |