code.vegaprotocol.io/vega@v0.79.0/core/integration/README.md (about) 1 # Integration Tests 2 3 This is the home of the system integrations tests. 4 5 ## Running the tests 6 7 They can be run from the root of vega with: 8 9 ```shell 10 make integrationtest 11 ``` 12 13 or 14 15 ```shell 16 go test ./... 17 ``` 18 19 ### Running just the integration tests 20 21 The integration tests have been hooked up to run as regular unit tests, so you can run just the integration tests with a 22 simple command: 23 24 ```shell 25 go test ./integration/... 26 ``` 27 28 When running these tests, you'll probably want to get a more verbose output (showing which steps of the tests passed and 29 failed), which can be done by adding 2 flags: 30 31 ``` 32 go test -v ./integration/... --godog.format=pretty 33 ``` 34 35 The `-v` flag tells `go test` to run with verbose output (sending logging to stdout). The `-godog.format=pretty` flag ( 36 which must be put at the end) instructs godog to print out the scenario's and, in case an assertion fails, show which 37 particular step of a given scenario didn't work. 38 39 ### Running specific scenario's 40 41 To run only certain tests (feature files), you can simply add the paths to a given feature file to the command: 42 43 ```shell 44 go test -v ./integration/... --godog.format=pretty $(pwd)/integration/features/my-feature.feature 45 ``` 46 47 ### Race detection and cache 48 49 For performance reasons, `go test` will check whether the source of a package has changed, and reuse compiled objects or 50 even test results in case it determines nothing has changed. Because the integration tests are tucked away in their own 51 package, changes to _other_ packages might not be compiled, and tests could possibly pass without changes being applied. 52 To ensure no cached results are used, the `-count` flag can be used: 53 54 ```shell 55 go test -v -count=1 ./integration/... --godog.format=pretty 56 ``` 57 58 Should there be tests that are intermittently failing, this could indicate a data race somewhere in the code. To use the 59 race detector to check for this, you can add the `-race` flag to the command. The full commands then would be: 60 61 ```shell 62 # Run all integration tests, verbose mode, ensure recompiled binaries, enable race detection, and use godog pretty formatting 63 go test -v -count=1 -race ./integration/... --godog.format=pretty 64 65 # Same as above, but only run a specific feature file: 66 go test -v -count=1 -race ./integration/... --godog.format=pretty $(pwd)/integration/feature/my-feature.feature 67 ``` 68 69 Race detection is a complex thing to do, so it will make running tests significantly slower. The pipeline runs the tests 70 with race detection, so this shouldn't be required to do locally. 71 72 ### Reproducing/replicating system tests 73 74 The system tests run on a higher level. They submit a new market proposal, get said market accepted through governance, 75 and then start trading. They use a `LogNormal` risk model, and specific fee parameters. David kindly provided the 76 long/short risk factors for a simple risk model that result in the same margin requirements and same fees being applied 77 to the trades. To create an integration test that replicates the system test results (transfers, balances, fees, etc...) 78 , simply start your feature file with the following: 79 80 ```gherkin 81 Feature: A feature that reproduces some system test 82 83 Background: 84 Given the markets: 85 | id | quote name | asset | risk model | margin calculator | auction duration | price monitoring | data source config | 86 | ETH/DEC20 | ETH | ETH | default-simple-risk-model | default-margin-calculator | 1 | default-none | default-for-future | 87 And the initial insurance pool balance is "0" for all the markets 88 ``` 89 90 ### Debug problems with VSCode 91 92 You might have a situation where you need to closely investigate the state of a tested application or the tests script itself. You might want to set some breakpoints, at which test execution stops, and you can view that state. 93 94 This process is called debugging, and we have an initial/template configuration for you. Please follow these steps: 95 1. Open and edit [.vscode/launch.json](.vscode/launch.json) config file (do not commit changes to this file): 96 - to run one `.feature` file, then edit `Debug .feature test` section and point to your `.feature` file, 97 - to run single Scenario in a `.feature` file, then change `.feature` file path and specify the line of a Scenario in that file. All in section `Debug single Scenario in .feature test`, 98 2. Set breakpoints in tests code or application code (note: it has to be in `.go` files, no `.feature`), 99 3. In VSCode switch to `Run and Debug` view (an icon with a bug from left side-bar), 100 4. From top drop-down select which option you want to run, e.g. `Debug .feature test`, 101 5. Click green Play button, 102 6. Observe results in `Debug Console` tab at bottom. 103 104 ## Life cycle 105 106 To get a market up and running, here is the process: 107 108 1. Configuration of network parameters. They have default values so it's not required, but if we want to override them, 109 it should be done in the first step. 110 2. Configuration of market. 111 3. Declaration of the traders and their general account balance. 112 4. Placement of orders by the traders, so the market can have a mark price. 113 114 Once these steps are done, the market should be in a proper state. 115 116 ## Steps 117 118 The list of steps is located in `./main_test.go`. 119 120 ### Market instantiation 121 122 Setting up a market is complex and the base for everything. As a result, we created a "lego-like" system to help us 123 strike the balance between flexibility and re-usability. 124 125 #### Flexibility with steps 126 127 A market is composed of several sets of parameters grouped by domain, such as margin, risk model, fees, and so on. 128 129 Each set of parameters is declared in its own step into which a custom name is given. In our "lego" analogy, these named 130 sets would be the "blocks". 131 132 To declare a market, we tell to our market which "blocks" to use. 133 134 Here is an example where we declare a risk model named "simple-risk-model-1". Then, we declare a "BTC" market, to which 135 we associate the risk model "simple-risk-model-1". 136 137 ```gherkin 138 Given the simple risk model named "simple-risk-model-1": 139 | long | short | max move up | min move down | probability of trading | 140 | 0.1 | 0.1 | 10 | -10 | 0.1 | 141 And the markets: 142 | id | quote name | asset | risk model | 143 | ETH/DEC21 | BTC | BTC | simple-risk-model-1 | 144 ``` 145 146 #### Re-usability with defaults 147 148 Because markets are tedious to instantiate, most of the time, we instantiate them using defaults stored in JSON files 149 inside the folder `steps/market/defaults`. 150 151 Each sub-folders contain the defaults for their domain. Referencing a default for the price monitoring that is not in 152 the `price-monitoring` folder will result in failure. 153 154 Using defaults works just like the named set, except that the file name will be used as the name. As a result, if the 155 file containing the defaults is named `default-basic.json`, then the name to fill in will be `default-basic`. 156 157 This is the recommended way. It's also fine to introduce a new defaults as long as it's used more than a couple of 158 times. 159 160 You can mix the use of steps and defaults in market declaration. 161 162 ### Debug 163 164 Sometimes, you need to log some state. For this, you can use the `debug ...` steps. 165 166 ## Convention 167 168 ### Glossary 169 170 We should move toward building our ubiquitous language and use domain language and avoid the use of synonyms. 171 172 If we talk about `submitting an order`, we avoid using `placing an order.` 173 174 ### Structuring a feature test 175 176 #### File 177 178 A feature test's file should be named by the feature / command it's testing, such 179 has: `maintenance_call_for_margin_account.go`. 180 181 The file name should match, or at least be close to, the description of the `Feature` keyword. 182 183 To be avoided: 184 185 * A prefix with a pull-request or an issue number, such as `4284-cancel-order.go`. 186 * A vague name, or context, such as `orders.go` or `cancellation.go` 187 188 #### Feature 189 190 The `Feature` keyword should describe the feature to be tested in proper sentences, with context. 191 192 It should match, or at least be close to, the name of the file it lives in. 193 194 ##### Examples 195 196 Let's assume, we have a file called `trader_cancels_orders.go` 197 198 ###### Good 199 200 ```gherkin 201 Feature: Traders can cancel his orders under certain conditions 202 ``` 203 204 By reading this, we get to know who's the main actor, the action and the target. Saying _"Under certain conditions"_ is 205 vague, but it's enough as that's the purpose of the `Scenario` to be more specific. At least, we know there are 206 conditions. 207 208 ###### Bad 209 210 ```gherkin 211 Feature: cancel orders 212 ``` 213 214 This is too vague. 215 216 ```gherkin 217 Feature: Should monitor prices 218 ``` 219 220 This seems completely unrelated to what the file name mentions. 221 222 #### Scenario 223 224 The `Scenario` keyword should describe the tested case of the command, and, thus, should never be empty. 225 226 A file can contain multiple scenarios if they test the same feature. Unrelated tests should live in a dedicated file. 227 228 If the feature to be tested is order cancellation, we could have: 229 230 ##### Examples 231 232 ###### Good 233 234 ```gherkin 235 Feature: Trader can cancel orders under certain condition 236 237 Scenario: Trader can cancel his order if not matched 238 ... 239 240 Scenario: Trader cannot cancel another trader's order 241 ... 242 ``` 243 244 We know who is doing what on what. 245 246 ###### Bad 247 248 ```gherkin 249 Scenario: Works 250 ... 251 252 Scenario: fail ! 253 ... 254 ``` 255 256 Oh yeah ? 257 258 ```gherkin 259 Scenario: 260 ... 261 ``` 262 263 Okay... 264 265 #### Given 266 267 `Given` should only be used for prerequisite declaration. Arguably, it's a bit tricky to distinguish a prerequisite from 268 what's not. For now, as a rule of thumb, we consider market declaration and traders initial deposit to the general 269 account to be the pre-requisites. Other steps should use the keywords below. 270 271 ##### Examples 272 273 ###### Good 274 275 ```gherkin 276 Given the market ... 277 And the traders general account balance ... 278 ``` 279 280 #### When 281 282 `When` should only be used when issuing a command. It shouldn't be used for assertions. The preferred construct of 283 command steps is: 284 285 ``` 286 <actor> <action verb> <target> 287 ``` 288 289 Construction with passive voice is accepted, if it makes more sense than the active voice. 290 291 ##### Examples 292 293 ###### Good 294 295 ```gherkin 296 When traders submit the following orders 297 ``` 298 299 We know who does what. 300 301 ```gherkin 302 When an oracle data is submitted 303 ``` 304 305 The passive voice sounds better `The system receives the following oracle data`. 306 307 #### Then 308 309 `Then` should only be used when asserting a state. It shouldn't be use for commands. The preferred construct of 310 assertion steps is: 311 312 ``` 313 <actor> should <state verb> <target> 314 ``` 315 316 ##### Examples 317 318 ###### Good 319 320 ```gherkin 321 Then trader trader-1 should have a balance of 100 ETH 322 ``` 323 324 We know what we expect from whom. 325 326 ###### Bad 327 328 ```gherkin 329 Then trader trader-1 have a balance of 100 ETH 330 ``` 331 332 We miss the `should` that emphasize the expectation. 333 334 ```gherkin 335 Then the orders should fails 336 ``` 337 338 This is too vague. 339 340 ```gherkin 341 Then the trader places an order 342 ``` 343 344 It's a command. The keywords should be used to help the reader to distinguish a command from an assertion. Even if the 345 above sentence makes sense, it breaks the structure `When command > Then assertion`, and we might end up with a list 346 of `Then`: 347 348 ```gherkin 349 Then the trader places an order 350 Then the trader should have balance ... 351 Then an oracle data is sent 352 Then the settlement data should be updated 353 ``` 354 355 We are no longer be able to sort out the commands from the assertions at first glance. 356 357 #### And / But 358 359 `And` can be used by any of the previous keywords and should follow the sentence construction of the keyword it is 360 backing. Use `But` for negative outcomes. 361 362 ### Step 363 364 #### Text 365 366 * The first word should start we a lower-case letter. 367 * Words (and table columns) should be lower-case with space separation, like plain human style. No upper-case location 368 to be remembered. 369 * Acronyms should be lower-case, like the rest, without trailing dot. We want to avoid interrogation such as : `ID` 370 or `Id` or `Id.` ? 371 372 ###### Good 373 374 ```gherkin 375 When the market id should contain the asset "..." 376 ``` 377 378 All lower-case. 379 380 ###### Bad 381 382 ```gherkin 383 Then The Market Id should appears in U.R.L with QuoteName 384 ``` 385 386 ### Single-line step 387 388 #### Error 389 390 We should verify the error message on every expected failure using `because` followed by the error message. 391 392 ###### Good 393 394 ```gherkin 395 Then the order "1234" should be rejected because "....." 396 ``` 397 398 We ensure the error is the expected one, and the context is clear, no need for additional comments. 399 400 ###### Bad 401 402 ```gherkin 403 Then the order "1234" should be rejected 404 ``` 405 406 It may have not failed for the reason we expected. And, we may be tempted to add a comment to explain the reason of the 407 failure. 408 409 ### Table step 410 411 #### Error 412 413 The column to verify the error should always be named `error`. 414 415 #### Date 416 417 Prefer `expiration date` over `expires at` or `started at`.