Simplified Unit And E2E testing with WebTau
Business Logic Data Preparation
Business logic testing often involves creating data input and validating logic output. I want to show you how you can simplify complex domain data creation using WebTau
API.
Throughout this article we will be building a Game Store server and its various business rules.
Let’s start with implementation and testing of games checkout discount logic.
Business department came up with the idea that we should encourage people to buy different game genres. So discount depends on:
- how many distinct type of games you buy
- each distinct game type will yield 10% discount, as long as games cost more than $15
- no more than three distinct games types will be counted
As an example, given the following checkout cart, a user will get a 20% discount:

Let’s create a discount calculator to implement the business rule:

To test that code we will need to create a sufficient number of Game
instances to make sure we cover:
- Count only one game per type
- Count only games with high enough price
- Should count no more than three distinct types
Creating many Game
instances is a lot of boilerplate:

When we deal with boilerplate multiple things can happen:
- copy pasting and not updating values
- not creating enough instances
To streamline the process of data creation, WebTau
provides table
API:

Let’s take a look at implementation of createGames
:

By using table
and a domain specific methods like createGames
you make your tests easier to write, read and maintain.
Business Logic Data Validation
We just simplified our data creation process. Now let’s see how we can simplify data validation one.
This time we will test a recommendation engine. One of its role is to provide a new game to play based on the games you already played. For now the engine will recommend the least played game in the least played type of games.
Here is the test:

Our current implementation has a bug, and uses incorrect type
for the returned game. When mismatch occurs, WebTau output provides all the information you need to understand the issue and highlights important parts of the output:

WebTau
did good with an object validation with a nested object. Let's take a look at how it fares with a list of objects.
Games Library class has a method to find top N games. It returns a list of games. Let’s test it.


Using trace
method is optional and can be helpful when dealing with complex data to see what's going on. When failure happens, WebTau
displays actual complex data and highlights values within that you need to take a look at.
We did validation using TableData
.
Let's take a look at how a property by property validation would look like.

To contrast it with table
approach:

When dealing with validation boilerplate code you can face even worse problems than with data preparation:
- ignore certain fields validation
- copy and paste wrong values and they match incorrect logic
REST API And Database
We tested some business logic and now lets move on to REST API testing. Game server exposes /api/game
end-point to list available games. To test it, we will manually insert records into GAME
database table and then use http.get
request to validate a piece of data.



Reporting
As you may have noticed, WebTau produces a lot of useful output to the console. In addition, it also generates HTML rich self-contained report. A single HTML file anyone can slack or email to your colleagues. Continuous Integration can send it to a chat or put under shared file system, and it will just work. On top of it, the report supports permalink navigation, so you can have a handy link to a specific portion of your test suite.

Even when your test passes, it still has plenty of information you can find useful. Here is an example of HTTP response fields your test validated: JSON fields your test touched are marked.

There Is More
There are so many things WebTau can do and I want to share them with you very much. But I want to keep this article short. Head over to WebTau GitHub to learn more about
- REPL mode
- HTTP Response Coverage
- Open API Integration
- Performance Metrics
- Browser UI testing
- GraphQL
- CLI testing
- Fake and Proxy Servers
- Personas
- Test Containers Integration
Don’t be afraid by the sheer amount of things the tool can do. It is all modular, and you can use only what you need.
GitHub And Star WebTau
If you find it interesting, go to WebTau GitHub page and star the project!
There is also a link to Discord Server where you can ask any questions or report a bug.