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 |
Matthew Treinish | 97072c8 | 2013-10-01 11:54:15 -0400 | [diff] [blame] | 5 | http://docs.openstack.org/developer/hacking/ |
Joe Gordon | 1374f88 | 2013-07-12 17:00:34 +0100 | [diff] [blame] | 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 |
Matthew Treinish | 5e4c0f2 | 2013-09-10 18:38:28 +0000 | [diff] [blame] | 23 | - [T104] Scenario tests require a services decorator |
Attila Fazekas | 23fdf1d | 2013-06-09 16:35:23 +0200 | [diff] [blame] | 24 | |
Matthew Treinish | 8b37289 | 2012-12-07 17:13:16 -0500 | [diff] [blame] | 25 | Test 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 Fazekas | 10fd63d | 2013-07-04 18:38:21 +0200 | [diff] [blame] | 33 | Exception Handling |
| 34 | ------------------ |
| 35 | According to the ``The Zen of Python`` the |
Attila Fazekas | 58d2330 | 2013-07-24 10:25:02 +0200 | [diff] [blame] | 36 | ``Errors should never pass silently.`` |
Attila Fazekas | 10fd63d | 2013-07-04 18:38:21 +0200 | [diff] [blame] | 37 | Tempest usually runs in special environment (jenkins gate jobs), in every |
| 38 | error or failure situation we should provide as much error related |
| 39 | information as possible, because we usually do not have the chance to |
| 40 | investigate the situation after the issue happened. |
| 41 | |
| 42 | In every test case the abnormal situations must be very verbosely explained, |
| 43 | by the exception and the log. |
| 44 | |
| 45 | In most cases the very first issue is the most important information. |
| 46 | |
| 47 | Try to avoid using ``try`` blocks in the test cases, both the ``except`` |
| 48 | and ``finally`` block could replace the original exception, |
| 49 | when the additional operations leads to another exception. |
| 50 | |
| 51 | Just letting an exception to propagate, is not bad idea in a test case, |
| 52 | at all. |
| 53 | |
| 54 | Try to avoid using any exception handling construct which can hide the errors |
| 55 | origin. |
| 56 | |
| 57 | If you really need to use a ``try`` block, please ensure the original |
| 58 | exception at least logged. When the exception is logged you usually need |
| 59 | to ``raise`` the same or a different exception anyway. |
| 60 | |
Chris Yeoh | c2ff727 | 2013-07-22 22:25:25 +0930 | [diff] [blame] | 61 | Use of ``self.addCleanup`` is often a good way to avoid having to catch |
| 62 | exceptions and still ensure resources are correctly cleaned up if the |
| 63 | test fails part way through. |
| 64 | |
Attila Fazekas | 10fd63d | 2013-07-04 18:38:21 +0200 | [diff] [blame] | 65 | Use the ``self.assert*`` methods provided by the unit test framework |
| 66 | the signal failures early. |
| 67 | |
| 68 | Avoid using the ``self.fail`` alone, it's stack trace will signal |
| 69 | the ``self.fail`` line as the origin of the error. |
| 70 | |
| 71 | Avoid constructing complex boolean expressions for assertion. |
Attila Fazekas | 7899d31 | 2013-08-16 09:18:17 +0200 | [diff] [blame] | 72 | The ``self.assertTrue`` or ``self.assertFalse`` without a ``msg`` argument, |
| 73 | will just tell you the single boolean value, and you will not know anything |
| 74 | about the values used in the formula, the ``msg`` argument might be good enough |
| 75 | for providing more information. |
| 76 | |
| 77 | Most other assert method can include more information by default. |
Attila Fazekas | 10fd63d | 2013-07-04 18:38:21 +0200 | [diff] [blame] | 78 | For example ``self.assertIn`` can include the whole set. |
| 79 | |
Attila Fazekas | 7899d31 | 2013-08-16 09:18:17 +0200 | [diff] [blame] | 80 | Recommended to use testtools matcher for more tricky assertion. |
| 81 | `[doc] <http://testtools.readthedocs.org/en/latest/for-test-authors.html#matchers>`_ |
| 82 | |
| 83 | You 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 Fazekas | 10fd63d | 2013-07-04 18:38:21 +0200 | [diff] [blame] | 86 | If the test case fails you can see the related logs and the information |
| 87 | carried by the exception (exception class, backtrack and exception info). |
| 88 | This and the service logs are your only guide to find the root cause of flaky |
| 89 | issue. |
| 90 | |
Attila Fazekas | 7899d31 | 2013-08-16 09:18:17 +0200 | [diff] [blame] | 91 | Test cases are independent |
| 92 | -------------------------- |
| 93 | Every ``test_method`` must be callable individually and MUST NOT depends on, |
| 94 | any other ``test_method`` or ``test_method`` ordering. |
| 95 | |
| 96 | Test cases MAY depend on commonly initialized resources/facilities, like |
| 97 | credentials management, testresources and so on. These facilities, MUST be able |
| 98 | to work even if just one ``test_method`` selected for execution. |
| 99 | |
Matthew Treinish | 5e4c0f2 | 2013-09-10 18:38:28 +0000 | [diff] [blame] | 100 | Service Tagging |
| 101 | --------------- |
| 102 | Service tagging is used to specify which services are exercised by a particular |
| 103 | test method. You specify the services with the tempest.test.services decorator. |
| 104 | For example: |
| 105 | |
| 106 | @services('compute', 'image') |
| 107 | |
| 108 | Valid service tag names are the same as the list of directories in tempest.api |
| 109 | that have tests. |
| 110 | |
| 111 | For scenario tests having a service tag is required. For the api tests service |
| 112 | tags are only needed if the test method makes an api call (either directly or |
| 113 | indirectly through another service) that differs from the parent directory |
| 114 | name. For example, any test that make an api call to a service other than nova |
| 115 | in tempest.api.compute would require a service tag for those services, however |
| 116 | they do not need to be tagged as compute. |
| 117 | |
Chris Yeoh | c2ff727 | 2013-07-22 22:25:25 +0930 | [diff] [blame] | 118 | Guidelines |
| 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 Treinish | 96c28d1 | 2013-09-16 17:05:09 +0000 | [diff] [blame] | 125 | |
DennyZhang | 900f02b | 2013-09-23 08:34:04 -0500 | [diff] [blame] | 126 | Parallel Test Execution |
| 127 | ----------------------- |
Matthew Treinish | 96c28d1 | 2013-09-16 17:05:09 +0000 | [diff] [blame] | 128 | Tempest by default runs its tests in parallel this creates the possibility for |
| 129 | interesting interactions between tests which can cause unexpected failures. |
| 130 | Tenant isolation provides protection from most of the potential race conditions |
| 131 | between tests outside the same class. But there are still a few of things to |
| 132 | watch 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 |
DennyZhang | 900f02b | 2013-09-23 08:34:04 -0500 | [diff] [blame] | 136 | require admin privileges are outside of tenants. |
Matthew Treinish | 96c28d1 | 2013-09-16 17:05:09 +0000 | [diff] [blame] | 137 | |
| 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. |