Blog

API mocks in CI: app tests without a test stand

App tests fail not because of the code, but because the stand was restarted overnight or someone deleted the test order. A build machine needs an API that answers the same way on every run. The bench-mocks command-line tool from Microkoi Bench starts a proxy with the same mocks and cases a developer uses, and lets the test switch them as it goes.

Why tests against a shared stand are flaky

The marketplace catalog test expects twenty products, and today the stand has eighteen. The empty-cart test breaks because a neighboring run left an item in the cart. Someone investigates every such failure by hand, and a month later nobody trusts a red build.

Mocks fix this if they’re the same ones the developer uses. A Bench workspace lives in git together with its mocks and cases, and bench-mocks reads it straight from the build checkout. Nothing is duplicated, nothing drifts.

Where the tool lives

SystemPath
macOSMicrokoiBench.app/Contents/MacOS/bench-mocks
Windowsbench-mocks.exe next to MicrokoiBench.exe
Linux/usr/bin/bench-mocks

The tool writes nothing to the workspace and keeps everything in memory. That’s why you can run several instances on one machine, with the Bench window open alongside. Its output is English only, the same on every machine.

Start the proxy and wait until it’s ready

When the proxy and the control API are ready, the tool writes one JSON line to standard output: the proxy address, the API address and the session token. The log goes to standard error, so a script can read the output right away.

Starting in a build script
READY=$(mktemp)
trap 'rm -f "$READY"' EXIT

bench-mocks serve --workspace ./api-space \
  --case "Empty catalog" > "$READY" &
SERVE=$!

for _ in $(seq 100); do
  [ -s "$READY" ] && break
  kill -0 "$SERVE" 2>/dev/null || break
  sleep 0.1
done
[ -s "$READY" ] || { wait "$SERVE"; exit 1; }

TOKEN=$(jq -r .token "$READY")
CONTROL=$(jq -r .control "$READY")
PROXY=$(jq -r .proxy "$READY")

No need to wait a fixed five seconds: the line is written in one piece, so a non-empty file means the addresses are ready. If the tool exits early, the loop stops and the exit code tells you why.

  • --case turns a case on at startup. Without it, root mocks answer.
  • --proxy-port and --control-port default to 0: a free port is picked automatically, so parallel jobs don’t collide.
  • --no-mocks starts the proxy with mocks off — requests go to the real server.
  • --standalone is needed in a container where the tool runs as process 1; otherwise it assumes its parent is gone and exits.

The token is a session secret: whoever knows it can switch cases and stop the proxy. Don’t print the ready line into the build log, mark the token as a secret in your CI settings, and create the file with mktemp and delete it after the run.

Point the app at the proxy

Set the address from the proxy field as the HTTP proxy in the app or on the device. The proxy listens on 127.0.0.1, which is enough for emulators: the Android emulator sees the machine at 10.0.2.2, and the iOS simulator uses 127.0.0.1. --listen 0.0.0.0 is only needed for a physical device and opens the proxy to the whole network — do that only on an isolated stand network.

To let the app read HTTPS, install the Bench root certificate as trusted: bench-mocks ca export prints it in PEM or DER. It’s the same certificate the Bench app installs.

Switch cases right from the test

The catalog test runs with the “Empty catalog” case, the order screen test with “Order canceled”. No proxy restart between them: a request to the control API switches the case, and it applies from the app’s next request.

Control API
AUTH="Authorization: Bearer $TOKEN"

curl -s -X PUT -H "$AUTH" -d '{"name":"Order canceled"}' \
  "http://$CONTROL/v1/active-case"

curl -s -H "$AUTH" "http://$CONTROL/v1/status"
curl -s -H "$AUTH" "http://$CONTROL/v1/report"
curl -s -X POST -H "$AUTH" "http://$CONTROL/v1/stop"
RequestWhat it does
PUT /v1/active-caseTurns a case on by name
DELETE /v1/active-caseTurns the case off: root mocks answer
PUT /v1/mocksTurns all mocks on or off
GET /v1/statusCase, request counters, warnings
GET /v1/reportWhich mocks answered and how many times
POST /v1/stopStops the tool

If there’s no case with that name, the 404 response suggests the closest name in error.suggestion. A typo in the test shows up immediately, not after half an hour of digging.

Wait for network quiet, not a timer

In the GET /v1/status response, the requests field has inflight — requests that haven’t finished — and idleMs — how many milliseconds passed without a new request. When inflight is 0 and idleMs is above 300–500, the app has finished loading the screen. The test checks the screen when the data has arrived, not after a random three seconds.

Check warnings at the start of the run

A broken mock file doesn’t stop the tool: that mock simply doesn’t answer. To avoid hunting for the cause in odd responses, read the warnings field in GET /v1/status or run bench-mocks cases list --workspace ./api-space --json before the run. An empty list means everything was read.

Exit codes for the build script

CodeWhat happened
0Normal stop: via the API, a signal or together with the parent
2Invalid arguments
3Workspace not found
4Workspace is damaged or the case can’t be turned on
5Case not found
6Port in use
7Launch not allowed, for example the beta has expired

The tool stops by itself when the process that started it exits. A crashed script won’t leave the proxy holding the port until the next build.

bench-mocks doesn’t show traffic and doesn’t run mock servers, breakpoints or monitoring. The result of its work is the GET /v1/report report.

What you get

The build no longer depends on the state of the stand. An empty catalog, a canceled order and a server error arrive the same way on every run, and the mocks the tests use are the same ones the developer sees in Bench.

Run bench-mocks from beta 0.9.6 on your build machine and write to beta@microkoi.com about the commands and report fields you missed. We read every email.

Microkoi BenchMocksResponses you set, in the proxy

Try it on your own project

The beta is free and needs no sign-up. Download it, pick your project folder and start the proxy — your first requests will show up within minutes.

Version 0.9.6 · macOS, Windows and Linux · no sign-up