|
| 1 | +# Lester |
| 2 | + |
| 3 | +Minimal Lua test framework. |
| 4 | + |
| 5 | +Lester is a minimal unit testing framework for Lua with a focus on being simple to use. |
| 6 | + |
| 7 | +It is highly inspired by |
| 8 | +[Busted](http://olivinelabs.com/busted/) and [Lust](https://github.com/bjornbytes/lust). |
| 9 | +It was mainly created to replace Busted without dependencies in the |
| 10 | +[Nelua](https://github.com/edubart/nelua-lang) compiler. |
| 11 | + |
| 12 | +[](https://asciinema.org/a/GihfI07vCt9Q7cvL6xCtnoNl1) |
| 13 | + |
| 14 | +## Features |
| 15 | + |
| 16 | +* Minimal, just one file. |
| 17 | +* Self contained, no external dependencies. |
| 18 | +* Simple and hackable when needed. |
| 19 | +* Use `describe` and `it` blocks to describe tests. |
| 20 | +* Supports `before` and `after` handlers. |
| 21 | +* Supports marking tests as disabled to be skipped. |
| 22 | +* Colored output. |
| 23 | +* Configurable via the script or with environment variables. |
| 24 | +* Quiet mode, to use in live development. |
| 25 | +* Optionally filter tests by name. |
| 26 | +* Show traceback on errors. |
| 27 | +* Show time to complete tests. |
| 28 | +* Works with Lua 5.1+. |
| 29 | +* Efficient. |
| 30 | + |
| 31 | +## Usage |
| 32 | + |
| 33 | +Copy `lester.lua` file to a project and require it, |
| 34 | +which returns a table that includes all of the functionality: |
| 35 | + |
| 36 | +```lua |
| 37 | +local lester = require 'lester' |
| 38 | +local describe, it, expect = lester.describe, lester.it, lester.expect |
| 39 | + |
| 40 | +-- Customize lester configuration. |
| 41 | +lester.show_traceback = false |
| 42 | + |
| 43 | +describe('my project', function() |
| 44 | + lester.before(function() |
| 45 | + -- This function is run before every test. |
| 46 | + end) |
| 47 | + |
| 48 | + describe('module1', function() -- Describe blocks can be nested. |
| 49 | + it('feature1', function() |
| 50 | + expect.equal('something', 'something') -- Pass. |
| 51 | + end) |
| 52 | + |
| 53 | + it('feature2', function() |
| 54 | + expect.truthy(false) -- Fail. |
| 55 | + end) |
| 56 | + |
| 57 | + local feature3_test_enabled = false |
| 58 | + it('feature3', function() -- This test will be skipped. |
| 59 | + expect.truthy(false) -- Fail. |
| 60 | + end, feature3_test_enabled) |
| 61 | + end) |
| 62 | +end) |
| 63 | + |
| 64 | +lester.report() -- Print overall statistic of the tests run. |
| 65 | +lester.exit() -- Exit with success if all tests passed. |
| 66 | +``` |
| 67 | + |
| 68 | +## Customizing output with environment variables |
| 69 | + |
| 70 | +To customize the output of lester externally, |
| 71 | +you can set the following environment variables before running a test suite: |
| 72 | + |
| 73 | +* `LESTER_QUIET="true"`, omit print of passed tests. |
| 74 | +* `LESTER_COLORED="false"`, disable colored output. |
| 75 | +* `LESTER_SHOW_TRACEBACK="false"`, disable traceback on test failures. |
| 76 | +* `LESTER_SHOW_ERROR="false"`, omit print of error description of failed tests. |
| 77 | +* `LESTER_STOP_ON_FAIL="true"`, stop on first test failure. |
| 78 | +* `LESTER_UTF8TERM="false"`, disable printing of UTF-8 characters. |
| 79 | +* `LESTER_FILTER="some text"`, filter the tests that should be run. |
| 80 | + |
| 81 | +Note that these configurations can be changed via script too, check the documentation. |
| 82 | + |
| 83 | +## Customizing output with command line arguments |
| 84 | + |
| 85 | +You can also customize output using command line arguments |
| 86 | +if `lester.parse_args()` is called at startup. |
| 87 | + |
| 88 | +The following command line arguments are available: |
| 89 | + |
| 90 | +* `--quiet`, omit print of passed tests. |
| 91 | +* `--no-quiet`, show print of passed tests. |
| 92 | +* `--no-color`, disable colored output. |
| 93 | +* `--no-show-traceback`, disable traceback on test failures. |
| 94 | +* `--no-show-error`, omit print of error description of failed tests. |
| 95 | +* `--stop-on-fail`, stop on first test failure. |
| 96 | +* `--no-utf8term`, disable printing of UTF-8 characters. |
| 97 | +* `--filter="some text"`, filter the tests that should be run. |
| 98 | + |
| 99 | +## Documentation |
| 100 | + |
| 101 | +The full API reference and documentation can be viewed in the |
| 102 | +[documentation website](https://edubart.github.io/lester/). |
| 103 | + |
| 104 | +## Install |
| 105 | + |
| 106 | +You can use luarocks to install quickly: |
| 107 | + |
| 108 | +```bash |
| 109 | +luarocks install lester |
| 110 | +``` |
| 111 | + |
| 112 | +Or just copy the `lester.lua` file, the library is self contained in this single file with no dependencies. |
| 113 | + |
| 114 | +## Tests |
| 115 | + |
| 116 | +To check if everything is working as expected under your machine run `lua tests.lua` or `make test`. |
| 117 | + |
| 118 | +## License |
| 119 | + |
| 120 | +MIT, see LICENSE for details. |
0 commit comments