| 1 |
# Integration Tests |
| 2 |
|
| 3 |
This directory contains Cylc integration tests. |
| 4 |
|
| 5 |
## How To Run These Tests |
| 6 |
|
| 7 |
```console |
| 8 |
$ pytest tests/i |
| 9 |
$ pytest tests/i -n 5 # run up to 5 tests in parallel |
| 10 |
$ pytest tests/i --dist=no -n0 # turn off xdist (allows --pdb etc) |
| 11 |
``` |
| 12 |
|
| 13 |
## What Are Integration Tests |
| 14 |
|
| 15 |
Integration tests aren't end-to-end tests. They focus on targeted interactions |
| 16 |
of multiple modules and may do a bit of monkeypatching to achieve that result. |
| 17 |
|
| 18 |
With Cylc this typically involves running workflows. |
| 19 |
|
| 20 |
The general approach is: |
| 21 |
|
| 22 |
1) Start a workflow. |
| 23 |
2) Put it in a funny state. |
| 24 |
3) Test how components interract to handle this state. |
| 25 |
|
| 26 |
I.e., the integration test framework runs the scheduler. The only thing it's |
| 27 |
really cutting out is the CLI. |
| 28 |
|
| 29 |
You can do everything, up to and including reference tests with it if so |
| 30 |
inclined, although that would really be a functional test implemented in Python: |
| 31 |
|
| 32 |
async with run(schd) as log: |
| 33 |
# run the workflow with a timeout of 60 seconds |
| 34 |
await asyncio.sleep(60) |
| 35 |
assert reftest(log) == ''' |
| 36 |
1/b triggered off [1/a] |
| 37 |
1/c triggered off [1/b] |
| 38 |
''' |
| 39 |
|
| 40 |
For a more integration'y approach to reftests we can do something like this |
| 41 |
which is essentially just another way of getting the "triggered off" information |
| 42 |
without having to run the main loop and bring race conditions into play: |
| 43 |
|
| 44 |
async with start(schd): |
| 45 |
assert set(schd.pool.get_tasks()) == {'1/a'} |
| 46 |
|
| 47 |
# setting a:succeeded should spawn b |
| 48 |
schd.command_reset('1/a', 'succeeded') |
| 49 |
assert set(schd.pool.get_tasks()) == {'1/b'} |
| 50 |
|
| 51 |
# setting b:x should spawn c |
| 52 |
schd.command_reset('1/b', 'x') |
| 53 |
assert set(schd.pool.get_tasks()) == {'1/b', '1/c'} |
| 54 |
|
| 55 |
## Guidelines |
| 56 |
|
| 57 |
Don't write functional tests here: |
| 58 |
|
| 59 |
* No sleep statements! |
| 60 |
* Avoid interaction with the command line. |
| 61 |
* Avoid testing interaction with other systems. |
| 62 |
* Don't get workflows to call out to executables. |
| 63 |
* Put workflows into funny states via artificial means rather than by |
| 64 |
getting the workflow to actually run to the desired state. |
| 65 |
* Avoid testing specific log messages or output formats where more general |
| 66 |
testing is possible. |
| 67 |
|
| 68 |
Don't write unit tests here: |
| 69 |
|
| 70 |
* No testing of odd methods and functions. |
| 71 |
* If it runs *really* quickly, it's likely a unit test. |
| 72 |
|
| 73 |
## How To Write Integation Tests |
| 74 |
|
| 75 |
Common test patterns are documented in `test_examples.py`. |
| 76 |
|
| 77 |
Workflows can be run in two ways: |
| 78 |
|
| 79 |
``` |
| 80 |
with start(schd): |
| 81 |
# starts the Scheduler but does not start the main loop |
| 82 |
# (always the better option if its possible) |
| 83 |
... |
| 84 |
|
| 85 |
with run(schd): |
| 86 |
# starts the Scheduler and sets the main loop running |
| 87 |
await asyncio.sleep(0) # yield control to the main loop |
| 88 |
... |
| 89 |
``` |
| 90 |
|
| 91 |
These methods both shut down the workflow / clean up after themselves. |
| 92 |
|
| 93 |
It is necessary to shut down workflows correctly to clean up resorces and |
| 94 |
running tasks. |