DEV Community

Cover image for Introducing Bat: Behavioral API Tester
Phil Mander
Phil Mander

Posted on • Originally published at Medium

Introducing Bat: Behavioral API Tester

I work at Harver where we have several mission critical, customer facing HTTP/Rest APIs built with Node.JS. When I joined, last year, the standard procedure to support developing and testing these APIs was to use Postman. But the process of integrating Postman with version control, writing Javascript tests with Postman's GUI and integrating into CI with Newman did not represent a particularly great developer experience to me. I wanted to find a way to improve upon this.

I have always been a fan of Cucumber, a tool for writing runnable software specifications in readable business language, and, in general, a Behavior Driven Development (BDD) approach to development. It occurred to me that Cucumber might be a great fit for developing and testing our HTTP APIs too.

From here, together with my colleagues at Harver, we created Bat (Behavioral API Tester); a tool to help write runnable specifications for HTTP APIs, such as Rest, RPC and GraphQL, in readable English. Such specs can then be easily automated and integrated into your continuous integration pipeline. It runs on Node.JS and is based on Cucumber.JS.

It works well for API testing with Cucumber because, within reason, there are a finite number of actions and assertions associated with testing an HTTP API. Bat builds upon Gherkin to create a common vocabulary (a DSL) for describing HTTP interactions. When taking a BDD approach to API development, such specifications provide a great way to write and review your intentions before writing any implementation code. For example:

Alt Text

The full set of step definitions is documented here. We’ve also provided (and will continue to add) a bunch of examples in the repo.

Because Bat is essentially a plugin for Cucumber.JS, it’s easy to extend with custom step definitions. We most often do this at Harver for non-standard stuff related to setting up the context (Given steps), such as custom authentication procedures or setting up test data. Internally, it uses SuperAgent as an HTTP client and to maintain sessions.

Bat has more useful features for making it usable in the real world such as variable replacement, compatibility with Postman’s environment file format and integration points with Open API specifications. See the Readme for more details.

Now Bat is open-source on Github and it is used by a broader audience, more missing features will become apparent. So please raise issues to request new features and raise bugs. If you want to contribute back to Bat, please check the Contributing Guide.

Top comments (1)

Collapse
 
theplenkov profile image
Petr Plenkov

Great idea! I like this, man!