Record and Replay Your App
Keploy records the API calls your app serves as test cases, and the calls your app makes to answer them, to its database or to other services, as mocks. keploy test then sends the recorded requests to your app again and answers its outgoing calls from the mocks, so the tests run with no database or other service running.
It runs natively, with no VM and no Docker:
- Linux (kernel 5.10 or later): Keploy intercepts calls with eBPF, which needs root. Run
keployas your own user; it asks forsudoitself when a command needs it. - macOS with Apple silicon: in user space, with no
sudo. Natively it understands the HTTP/HTTPS, gRPC, PostgreSQL, MySQL, MongoDB, Aerospike, Couchbase, Memcached, Apache HBase and Apache ZooKeeper calls of Go, Node.js, Python and Java apps. Calls to other services, such as Redis, Kafka or Apache Pulsar, are captured only as raw bytes and usually don't replay, so run an app that depends on them with Docker. - Windows (x86-64): the same commands, with
keploy.exeinstalled as the Windows guide shows.
Keploy doesn't support Intel Macs. On Windows on ARM, run Keploy in WSL.
To record the calls your existing tests make, with no app to start and no requests to send, see Mock your tests.
1. Install Keploy
On Linux and macOS:
source /dev/stdin <<<"$(curl -fsSL https://keploy.io/install.sh || echo 'echo Keploy installer download failed >&2; return 1')"
Then check it:
keploy --version
The script installs Keploy in ~/.keploy/bin, where it can update itself, and adds that folder to your PATH. The Linux and macOS guides have the details.
2. Sign in
You need a free Keploy account to record and replay.
keploy login
It prints a link and a code, and opens your browser at app.keploy.io to approve them. Keploy keeps the session and reuses it for every later command. If you skip this step, your first keploy record signs you in the same way.
- No browser on this machine (a remote shell):
keploy login --manual-loginasks for an API key from your Keploy dashboard instead. - CI, or any run with nobody at the keyboard: set
KEPLOY_API_KEY, and Keploy never opens a browser.
3. Record your app
From your project's folder, pass the command that starts your app, exactly as you run it yourself:
keploy record -c "CMD_TO_RUN_APP"
For example:
keploy record -c "go run main.go" # Go
keploy record -c "node server.js" # Node.js
keploy record -c ".venv/bin/python app.py" # Python, from a virtualenv
keploy record -c "java -jar target/app.jar" # Java
On macOS, pass the app's real command, not a launcher such as npm start or a wrapper script: macOS drops Keploy's interception when a protected system binary runs. For Java, that means the JDK's own java, as in keploy record -c "${JAVA_HOME:-$(/usr/libexec/java_home)}/bin/java -jar target/app.jar". For Go, build the app and pass its binary: go build -o app . && keploy record -c "./app", then keploy test -c "./app"; add app to .gitignore, or build it outside the repository, so the binary stays out of your commits. In this release, a go run command records on macOS, but the replay that keploy record starts right after it stops with "the application exited before it accepted a connection". The macOS guide has the details.
Keploy starts your app. Once it's up, send it the requests you want as tests, with curl, Postman, your frontend or a script. Keploy saves:
- each request and its response as a test case, in
keploy/test-set-0/tests/; - the calls your app made to answer them as mocks, in
keploy/test-set-0/mocks.yaml.
Each keploy record writes a new test set: test-set-1, test-set-2, and so on.
Stop recording with Ctrl+C, or pass --record-timer 2m to stop after two minutes.
Right after it stops, keploy record replays what it just recorded, to check that each test replays and to learn which fields change from one run to the next, such as timestamps and generated IDs, so they don't fail later replays. To skip that replay, pass --disableAutoReplay.
On macOS, in this release, that replay is slow for an app that calls other services: for a Go app, about 90 seconds for each test whose request made such a call, while keploy test replays the same set in seconds. It ends on its own, and the recording is already saved. To skip it, record with --disableAutoReplay, then replay the set with keploy test.
It ends with a NEXT block: where your tests stand and the command to run next. keploy status prints that block again at any time.
4. Replay the tests
keploy test -c "CMD_TO_RUN_APP" --delay 10
Keploy starts your app, waits --delay seconds for it to come up (5 by default), then sends each recorded request and compares the response with the recorded one. Your app's outgoing calls are answered from the mocks, so its database and the services it calls don't need to run.
--health-url http://localhost:8080/healthwaits until that URL answers with a 2xx instead of a fixed delay, for an app whose start-up time varies.-t test-set-0replays one test set.
Keploy prints each test's result and a summary, writes the full report to keploy/reports/test-run-<n>/test-set-<n>-report.yaml, uploads it to your Keploy dashboard and prints its link, and ends with the NEXT block.
5. Commit and run in CI
Commit keploy.yml and the keploy/ folder with your code, so your teammates and your CI replay the same tests.
The .gitignore that Keploy writes in keploy/ keeps the reports and each test set's mocks.yaml out of git. Instead, when a replay passes a test set, Keploy uploads its mocks to your Keploy account and pins them by hash in the set's config.yaml. A keploy test on another machine downloads the pinned mocks, so it must be signed in to your Keploy organization; in CI, with KEPLOY_API_KEY.
A test set that no replay has passed yet, such as one recorded with --disableAutoReplay, has no pin, and its mocks are only on your machine. Replay it with keploy test before you commit it.
To replay the tests on every pull request, see GitHub Actions.
Related
- Mock your tests: record and replay the calls your test suite makes.
- Installing Keploy on Linux.
- Installing Keploy on macOS: Apple silicon, natively or with Docker.
- Installing Keploy on Windows: native on x86-64, or WSL/Docker.
- Installing Keploy: every platform, with Docker and Podman.
- Keploy CLI Commands: every command and flag.
- Keploy configuration file: options for
keploy.yml.