cukelib
v0.5.2
Published
Cucumber step definitions and service control for API testing
Downloads
30
Maintainers
Readme
Cucumber Service Library -- A Toolbox for API Testing
Contributing This is a new library in active development. Bug reports, suggestions for improvement, etc. are welcome and should be submitted through github issues.
What you're looking at is a toolbox of services and steps for testing API's using Cucumber. The intent is for you to write automated tests for your API's as fast or faster than running manual tests (e.g Postman).
Features include:
- Starting multiple servers with automatic stopping and clean up
- HTTP server request and response steps
- SQL database queries
- Shell script testing steps
- ... and much more
Several self contained examples applications are included as well.
All of these facilities are decoupled as much as possible. You can take what you like and leave the rest. So let's dive right in.
Simple Echo Server Example
This tests a simple server that echos back it's request body. (Note on exceptionally terse Gherkin.)
Feature: Super Simple Echo Server
Scenario: A basic GET call just responds
When GET "/users"
Then responded with status code 200
Scenario: A basic PUT call echos its input body
Given PUT "/losers"
"""
[
"Sally Sad",
"Billy Bad"
]
"""
Then responded with status code 200
And response matched pattern
"""
[
"Sally Sad",
"Billy Bad"
]
"""
Here's all the support code that's needed:
const { childService, requestSteps, responseSteps } = require('cukelib');
module.exports = function () {
childService.initialize.call(this);
requestSteps.call(this, {
host: 'http://localhost:3001'
});
responseSteps.call(this);
this.Before(() => {
return childService.spawn({
name: 'echo_server',
cmd: 'node',
args: [`${__dirname}/../../index.js`, '--port=3001'],
});
});
};
That's simple enough, but there's actually a lot going on under the hood here.
- The
childService.spawn(...)
function launches the server, connects its output streams, and handles its errors. - Notice there's no need for an
After
hook to stop the server --childService.spawn(...)
also sets up its own cleanup hooks. - Furthermore you can launch the server in
BeforeFeatures
orBeforeFeature
hooks or even in actual steps, andcukelib
will stop and cleanup the server at the end of the run, at the end of the feature, or at the end of the scenario as appropriate. - The request text is actually parsed as YAML, and...
- The response text is matched using the lodash-match-pattern library, and...
- Both the request and support interpret single cell tables as argument strings, so a similar scenario can be concisely expressed:
Feature: Cleaner Request/Response Steps
Scenario: A basic PUT call echos its input body
Given PUT "/losers"
| [ Sally Sad, Billy Bad ] |
Then responded with status code 200
And response matched pattern
| [ _.isString, /\w+\s\w+/ ] |
Interested? Yeah, we're just getting started, but before we get into some details, here's some ...
Conventions
The library is generally organized around services and steps. Although it's mostly non-opinionated there are some organizational conventions. First the library avoids the Cucumber "World" object, and implements its own "Universe" mechanism under the covers. This leaves you free to manage the World as needed or access and use the Universe functionality as well.
Service conventions
All of the services are variations on the theme illustrated by childService
in the support code snippet above. They all implement launch
and stop
functionality and automatically run their stop
functions within an appropriate testing life-cycle hook. All of the services and most of the steps also have initialize
functions. In most cases it's fine to just call any one of them at the beginning of a step definition file.
And yes, it's possible and in fact typical, to launch multiple concurrent services.
Step definitions conventions
For the purposes of getting you started with testing quickly, I've made some non-dogmatic choices about the included step definitions. (Note on terse Gherkin.)
- Step definitions are strictly decoupled from their support function code which are in separate respective
..._steps.js
and..._support.js
files. This generally a good practice analogous to keeping views separate from business logic, but my main objective is to allow you to customize your own more relevant and readable step definitions. - The step definitions are intentionally terse. This is just a choice of simplicity over readability. Terse definitions are easier to write and easier to find, but again, you're free to customize your own.
- The step definitions follow a strict convention with "Given" and "When" (setup) steps in present tense, and "Then" (assertion) steps in past tense. This is may be a good practice overall, but it's especially necessary for disambiguating terse definitions.
- Postfix ... Not! steps. The module includes a tool for creating a logical opposite assertion steps from assertion steps. The step definition is the same as the original with a suffix of '... Not!'. This is to be read out loud as if from Wayne's World.
Library Details
- Universe manages a namespaced object which is copied from the "universe" scope to the feature scope for each new feature, and is copied from the feature scope to the scenario scope for each new scenario. Most of the steps and service make use of the Universe for their internal state.
- Service Control is the abstract parent of all of the services.
- Child Service launches shell executable servers as child processes of the cucumber process.
- Embed Service launches NodeJS servers (e.g.
http.Server
) embedded within the cucumber process. - Knex Service connects to SQL databases via the knex query builder.
- Create Database Service is a convenience service to create (and drop) feature databases.
- Selenium Standalone Service launches the selenium standalone server. This is included for completeness. Most likely you are managing your Selenium server separately.
- WebdriverIO Service launches the WebdriverIO Selenium interface as an embedded service within the cucumber process.
- GetSet Steps exposes access to the "universe" objects as cucumber steps.
- Request Steps provides cucumber steps for issuing http requests, usually to servers launched by the Child or Embed services.
- Request Response Overview
- Response Steps are for interpreting the results of Request Steps.
- SQL Overview
- SQL Steps provides steps for issuing and interpreting results of SQL commands issued through Knex Services.
- Shell Steps for running and interpreting output of shell scripts.
- Not! and Throw! Steps wrappers for generating logical "Not!" steps (Throw! steps are useful for testing the development of step libraries such as this one. They aren't useful for general library usage.)
- Diagnostic Steps for inspecting the state of the universe and debugging potential confusion.
Examples
- Simple Echo Server -- Child Service, Request Steps, Response Steps.
- Ad Server -- Child Service, Create Database Service, Knex Service, SQL Steps, Request Steps, Response Steps
Diagnostic Tips
- Run with environment variable CUKELIB_VERBOSITY set to 1, 2, or 3
Contributing
Thank You in Advance. By all means submit issues, bug reports, suggestions, etc. to the github issues.