|
| 1 | +# Test |
| 2 | + |
| 3 | +Test is an [Ash](https://github.com/ash-shell/ash) module that offers easy unit testing, with simple CI integrations. |
| 4 | + |
| 5 | +## Running Tests |
| 6 | + |
| 7 | +You can run tests by running the following command: |
| 8 | + |
| 9 | +```sh |
| 10 | +ash test $module-to-test |
| 11 | +``` |
| 12 | + |
| 13 | +Where `$module-to-test` is either a module alias, or a module package. |
| 14 | + |
| 15 | +## Hooking Up a Module to Travis |
| 16 | + |
| 17 | +You can generate the `.travis.yml` file by running the following command while in your modules directory: |
| 18 | + |
| 19 | +``` |
| 20 | +ash test:travis |
| 21 | +``` |
| 22 | + |
| 23 | +If your tests are already written, there is no further configuration you need to do beyond enabling your repository on Travis. |
| 24 | + |
| 25 | +## Writing Tests |
| 26 | + |
| 27 | +### The `ash_config.yaml` File |
| 28 | + |
| 29 | +To get started with writing tests, you must first add a `test_prefix` key to your modules [ash_config.yaml](https://github.com/ash-shell/ash#ash_configyaml) file. |
| 30 | + |
| 31 | +For example, if our module was named `Slugify`, we would add this line here: |
| 32 | + |
| 33 | +```yaml |
| 34 | +test_prefix: Slugify |
| 35 | +``` |
| 36 | +
|
| 37 | +### The `test.sh` File |
| 38 | + |
| 39 | +Now that we're ready to start writing tests, we'll have to create the `test.sh` file at the root of our module. |
| 40 | + |
| 41 | +> If you'd like to check out what a fully built test.sh file looks like before we jump in, check out [Slugify's](https://github.com/ash-shell/slugify/blob/master/test.sh) |
| 42 | + |
| 43 | +In the `test.sh` file, you can create functions that start with `"$test_prefix"__test_`, and those will get run when we run our tests. |
| 44 | + |
| 45 | +Following our previous example, if our module was named `Slugify`, the functions that we would want to test would have to start with `Slugify__test_"` |
| 46 | + |
| 47 | +When writing these functions, if at any point there is a failure you should return 1. When everything succeeds don't return anything. |
| 48 | + |
| 49 | +Here is a test from Slugify: |
| 50 | + |
| 51 | +```sh |
| 52 | +Slugify__test_slugify_spaces(){ |
| 53 | + # Space in the middle of a sentence |
| 54 | + Slugify_test_single_slugify "Brandon Romano" "brandon-romano" |
| 55 | + if [[ $? -ne 0 ]]; then return 1; fi |
| 56 | +
|
| 57 | + # Multiple Spaces |
| 58 | + Slugify_test_single_slugify "Radical Ricky" "radical-ricky" |
| 59 | + if [[ $? -ne 0 ]]; then return 1; fi |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +## Conventions to Follow to Write Great Tests |
| 64 | + |
| 65 | +Test provides some really great tools to help you write really great tests, but you should definitely follow a few conventions. |
| 66 | + |
| 67 | +Before jumping into this section, I will provide a screenshot that displays what a test looks like (with a forced failure): |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +> All that fancy indentation is handled for you :tada: |
| 72 | + |
| 73 | +### Provide Concise Names for Tests |
| 74 | + |
| 75 | +The name of your actual test method has to start with `"$test_prefix"__test_` by requirement, but anything after that is up for you to decide. |
| 76 | + |
| 77 | +Your test names should describe the module you are testing, as the test name is used in the output of the tests. |
| 78 | + |
| 79 | +For example, if we had a test `Slugify__test_slugify_spaces`, when running the test we would see `test_slugify_spaces` in both success and failure cases. |
| 80 | + |
| 81 | +The names are very important, as this is the only piece of information we see during a successful run of a test (as you can see above). |
| 82 | + |
| 83 | +### Provide a Fluent Failure Description |
| 84 | + |
| 85 | +For a failing test, we should always provide a description of what went wrong. The error message should be written for the context of the error message above (The first line with the unicode red arrow is the `Description`, the lines below it are the details). These descriptions should be concise. |
| 86 | + |
| 87 | +Failure descriptions are the first line that is echoed out in a test function. |
| 88 | + |
| 89 | +For example: |
| 90 | + |
| 91 | +```bash |
| 92 | +Slugify__test_slugify_spaces(){ |
| 93 | + local result="$(Slugify__slugify "Radical Ricky")" |
| 94 | + if [[ "$result" != "radical-ricky" ]]; then |
| 95 | + # This line below is our failure description |
| 96 | + echo "Slugify__slugify should convert 'Radical Ricky' into 'radical-ricky'" |
| 97 | + return 1 |
| 98 | + fi |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +### Provide Futher Details if Needed |
| 103 | + |
| 104 | +I would argue that most of the time further details are actually needed. Any additional lines echoed out are further details. |
| 105 | + |
| 106 | +Following our previous example, we probably would want to know the _actual_ result of the output if it weren't what we were expecting: |
| 107 | + |
| 108 | +```bash |
| 109 | +Slugify__test_slugify_spaces(){ |
| 110 | + local result="$(Slugify__slugify "Radical Ricky")" |
| 111 | + if [[ "$result" != "radical-ricky" ]]; then |
| 112 | + # This line below is our failure description |
| 113 | + echo "Slugify__slugify should convert 'Radical Ricky' into 'radical-ricky'" |
| 114 | + # This lines below are our failure details. We can have as many (or as little) of these as we want |
| 115 | + echo "Actual Result: '$result'" |
| 116 | + echo "Wow, another detail!" |
| 117 | + return 1 |
| 118 | + fi |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +## License |
| 123 | + |
| 124 | +[MIT](LICENSE.md) |
0 commit comments