|
| 1 | +# Go Write Tests |
| 2 | +using Pragmatic Best Practices |
| 3 | + |
| 4 | +Edward Haas, @redhat <!-- .element: style="position: absolute; left: 0; top: 100%; font-size: 0.6em" --> |
| 5 | +[email protected] <!-- .element: style="position: absolute; left: 0; top: 120%; font-size: 0.6em" --> |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +# /me |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +# Testing |
| 14 | + |
| 15 | +Why? |
| 16 | + |
| 17 | +Note: Because we are not prefect! |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +# Testing Levels |
| 22 | + |
| 23 | +- Unit |
| 24 | +- Integration |
| 25 | +- System |
| 26 | + |
| 27 | +Note: |
| 28 | +We can also consider compilation of typed languages and linters as an |
| 29 | +early development testing level. |
| 30 | + |
| 31 | +Most practices should fit them all. |
| 32 | + |
| 33 | +--- |
| 34 | + |
| 35 | +# Test Definitions |
| 36 | + |
| 37 | +-- |
| 38 | + |
| 39 | +# Test Format <!-- .element: style="font-size: 1.8em" --> |
| 40 | + |
| 41 | +- Setup |
| 42 | +- Exercise |
| 43 | +- Verify |
| 44 | +- Teardown |
| 45 | + |
| 46 | +Note: A test is expected to include these steps in its flow, although |
| 47 | +some may have no content. |
| 48 | + |
| 49 | +-- |
| 50 | + |
| 51 | +## Assertion |
| 52 | + |
| 53 | +expected vs actual results |
| 54 | + |
| 55 | +Note: |
| 56 | +- Appear mainly in the verify step, but may also end up in the setup and |
| 57 | + teardown. |
| 58 | +- Some test frameworks differentiate the error type when failing in the |
| 59 | + fixtures or in the test body (exercise and verify step). |
| 60 | + |
| 61 | +-- |
| 62 | + |
| 63 | +# Skipping |
| 64 | + |
| 65 | +- Explicitly by the test runner. |
| 66 | +- Explicitly by the test author. |
| 67 | +- Conditioned at run-time. |
| 68 | + |
| 69 | +Note: |
| 70 | +It is useful to: |
| 71 | +- Run a subset of tests, filtering out some. |
| 72 | +- Declare in-code that a test is broken and needs attention (XFail). |
| 73 | +- Dynamically detect that a test cannot run on the platform. |
| 74 | + I consider this a bad practice, as it may be missed. |
| 75 | + |
| 76 | +Consider the use of marking/labeling to filter the test list. |
| 77 | +This leaves the control to the test runner logic and avoids unintentional |
| 78 | +skips to occur (which in turn introduces holes in the coverage). |
| 79 | + |
| 80 | +-- |
| 81 | + |
| 82 | +# Focus |
| 83 | + |
| 84 | +Note: |
| 85 | +This is the opposite of skipping. |
| 86 | + |
| 87 | +Run only a specific test group, which is useful during development. |
| 88 | + |
| 89 | +It emphasises the need to have a test well isolated and not dependent on other |
| 90 | +tests. |
| 91 | + |
| 92 | +--- |
| 93 | + |
| 94 | +# Test Best Practices |
| 95 | + |
| 96 | +Note: |
| 97 | +Language and framework agnostic. |
| 98 | + |
| 99 | +May be opinionated and controversial. |
| 100 | + |
| 101 | +-- |
| 102 | + |
| 103 | +# Fail First |
| 104 | + |
| 105 | +Note: Are you sure the test can fail? Maybe it always passes. |
| 106 | + |
| 107 | +-- |
| 108 | + |
| 109 | +# Readability & Structure |
| 110 | + |
| 111 | +Code is being read many more times than being written. <!-- .element: style="font-size: 0.8em" --> |
| 112 | + |
| 113 | +<!-- .slide: data-background-image="cat_glasses.jpg" data-background-opacity="0.4"--> |
| 114 | + |
| 115 | +-- |
| 116 | + |
| 117 | +# Body vs Fixture |
| 118 | + |
| 119 | +-- |
| 120 | + |
| 121 | +# Isolation |
| 122 | + |
| 123 | +-- |
| 124 | + |
| 125 | +# Keep Assertions visible |
| 126 | + |
| 127 | +-- |
| 128 | + |
| 129 | +# Traceability |
| 130 | + |
| 131 | +-- |
| 132 | + |
| 133 | +# Shared Resources |
| 134 | + |
| 135 | +<!-- .slide: data-background-image="share_keyboard.jpg" data-background-opacity="0.3"--> |
| 136 | + |
| 137 | +-- |
| 138 | + |
| 139 | +# Continue On Failure |
| 140 | + |
| 141 | +Note: Expect tests to fail, stopping on the first failure is not informative |
| 142 | + enough. |
| 143 | + Said that, support leaving the system "freezed" for advance debugging. |
| 144 | + |
| 145 | +<!-- .slide: data-background-image="never_give_up.jpg" data-background-opacity="0.3"--> |
| 146 | + |
| 147 | +-- |
| 148 | + |
| 149 | +# Avoid Dead Tests |
| 150 | + |
| 151 | +-- |
| 152 | + |
| 153 | +# Parallel Tests |
| 154 | + |
| 155 | +Note: Production code has tests. Tests have only themselves. |
| 156 | + Do not leave unexecuted code, it rots. |
| 157 | + Keep only running tests. |
| 158 | + |
| 159 | +<!-- .slide: data-background-image="parallel.jpg" data-background-opacity="0.3"--> |
| 160 | + |
| 161 | +-- |
| 162 | + |
| 163 | +# XFail |
| 164 | + |
| 165 | +Expect to Fail |
| 166 | + |
| 167 | +Note: |
| 168 | +Record a detected bug or a missing feature. |
| 169 | + |
| 170 | +-- |
| 171 | + |
| 172 | +# !Randomization & Logic |
| 173 | + |
| 174 | +Note: |
| 175 | +Random Testing have their own test category, do not mix them with |
| 176 | +"regular" tests. |
| 177 | + |
| 178 | +Logic in tests should be limited, it competes with production code and |
| 179 | +may by itself be(come) wrong. |
| 180 | + |
| 181 | +--- |
| 182 | + |
| 183 | +# The imperfect world |
| 184 | + |
| 185 | +Workarounds |
| 186 | + |
| 187 | +-- |
| 188 | + |
| 189 | +# Stop On Failure |
| 190 | + |
| 191 | +-- |
| 192 | + |
| 193 | +# Clean @ Setup |
| 194 | + |
| 195 | +--- |
| 196 | + |
| 197 | +# Thank You |
| 198 | + |
| 199 | +https://ehaas.net/slides/decks/go_write_tests_using_pragmatic_best_practices |
| 200 | + |
0 commit comments