#cp.spec
An synchronous/asynchronous test library for Lua.
This library uses a syntax similar to Ruby RSpec or Mocha.js.
#Simple Synchronous Test
To create a test, create a new file ending with _spec.lua
. For example, simple_spec.lua
:
It can be run from the Debug Console like so:
It will report something like this:
#Simple Synchronous Failure
If a test fails, it gives a report of where it failed, and if provided, the related message:
This will result in something like this:
You can then check the line that failed and resolve the issue.
#Simple Asynchronous Test
Performing an asynchronous test is only a little more complicated. We'll modify our simple_spec.lua
to use of the Run.This instance available to every test:
Other than using hs.timer
to actually make this asynchronous, the key additions here are:
this:wait(5)
: Tells the test that it is asynchronous, and to wait 5 seconds before timing out.this:done()
: Called inside the asynchronous function to indicate that it's complete.
Asycnchronous (and synchronous) tests can also be terminated by a failed assert
, an error
or a call to this:fail(...) or this:abort(...)
#Multiple tests
Most things you're testing will require more than a single test. For this, We use Specification, most simply via the describe function:
This will now run two tests, and report something like this:
#Data-driven Testing
When testing a feature, there are often multiple variations you want to test, and repeating individual tests can get tedious.
This is a great place to use the where feature. Our previous test can become something like this:
Other variations can be added easily by adding more rows.
#Running Multiple Specs
As shown above, you can run a single spec like so:
You can also run that spec an all other specs under the same path by adding ".*"
to the end.
Or run every spec in your system like so:
#Submodules
- cp.spec.DefaultHandler
- cp.spec.Definition
- cp.spec.Error
- cp.spec.Handled
- cp.spec.Handler
- cp.spec.Message
- cp.spec.Report
- cp.spec.Run
- cp.spec.Scenario
- cp.spec.Specification
- cp.spec.TestCase
- cp.spec.TestSuite
- cp.spec.Where
- cp.spec.expect
#API Overview
Functions - API calls offered directly by the extension