How to run tests on a remote machine over SSH
Copy your working tree with rsync, run the suite over ssh, and copy the reports back. The one-liner, where it breaks, and how to split a suite across hosts.
Riley Hilliard8 min read
testingcliagents
The simplest way to run tests on a remote machine is two commands: rsync copies your working tree to the other machine, and ssh runs the test command there. Your uncommitted changes go along with the copy, the output streams back to your terminal, and ssh exits with the test command’s exit code, so a script can tell a pass from a failure. The laptop stays cool and usable while a spare desktop or a mini PC in the closet does the work.
That pair covers one person and one host, and gets harder once you have several hosts, several runs at the same time, or a coding agent that runs the suite after every change.
How do you run tests on a remote machine over SSH?
You need passwordless SSH to the remote (if ssh testbox logs in without a password prompt, you have it), rsync on both machines, and the project’s toolchain installed on the remote. Then:
rsync -az --delete --exclude .git --exclude node_modules ./ testbox:src/myapp/ \
&& ssh -t testbox 'cd src/myapp && npm ci && npm test'
What the less obvious parts of that line do:
- The trailing slash on
./copies the directory’s contents intosrc/myapp/rather than creatingsrc/myapp/myapp. --deleteremoves files on the remote that you deleted locally, so a test you removed doesn’t keep running there. rsync won’t delete excluded paths on the receiving side, so the remote’s ownnode_modulessurvives.- Excluding
.gitandnode_moduleskeeps the sync small.node_modulesalso contains platform-specific binaries, so copying a macOS install to a Linux box breaks it; install on the remote instead. Add.venv, build output and test caches to the list for the same reasons. ssh -tallocates a terminal on the remote, so Ctrl+C on your side reaches the test process and colored output stays colored.
What breaks with rsync and ssh?
The remote copy drifts from your working tree. Anything the tests write (coverage files, snapshots, temp databases) stays on the remote between runs, and so does anything you excluded. If you change package.json and the lockfile but the install step is skipped to save time, the next run uses the old dependencies and passes or fails for the wrong reasons.
Concurrent runs overwrite each other. Two terminals, two teammates or two coding agents syncing to the same remote directory replace each other’s files while a test run is reading them. Even with separate directories, two suites that each start a worker per core will share one machine’s CPU, and timing-sensitive tests start failing.
Killed sessions leave processes behind. If the SSH connection drops, or you run without -t and press Ctrl+C, the remote test process can keep running. It still holds its ports and database, so the next run fails on “address already in use” and you go looking for a stray process with ps.
Results stay on the remote. The terminal output comes back, but JUnit XML, coverage reports and screenshots don’t. You need a second rsync in the other direction:
rsync -az testbox:src/myapp/coverage/ ./coverage/
None of these are hard to script around for one host. Scripting around all of them, for several hosts, is a project.
Why not just use CI?
CI runs your tests on another machine, and it’s the right place for the run that gates a merge. For the inner loop it’s slow. Each iteration needs a commit and a push, the job waits in a queue, the runner starts cold and reinstalls dependencies, and you read the result in a browser. A remote run over SSH starts from a warm copy of your tree, including changes you haven’t committed, and streams output to the terminal you’re already in.
How do you split a test suite across machines?
Splitting a suite into shards and running each shard on a different host cuts the wall-clock time. The usual split points are by package, by directory or by layer (backend tests on one host, frontend on another). Several runners also split a suite for you: Jest, Vitest and Playwright accept --shard=1/3 to run one third of the suite, --shard=2/3 for the next, and so on.
Doing it by hand takes one rsync and one ssh per host, started in the background, plus a wait and some way to combine the exit codes. We built rr to do that for you. It syncs your working tree to remote machines over rsync and SSH, runs the command, and streams the output back. There’s no daemon on the remote; each host needs SSH access and rsync.
$ brew install rileyhilliard/tap/rrHosts go in ~/.rr/config.yaml, which stays on your machine. Tasks go in .rr.yaml in the project, which you commit. A parallel task lists other tasks, and rr sends each one to a free host at the same time:
# .rr.yaml
version: 1
tasks:
test-backend:
run: cd backend && pytest
test-frontend:
run: cd frontend && npm test
test-pipeline:
run: cd pipeline && python -m pytest
test:
parallel: [test-backend, test-frontend, test-pipeline]
fail_fast: false
rr test runs all three, and rr test-backend runs one. Extra arguments forward into the task, so rr test-backend -- tests/test_auth.py -x runs a single file. rr test --dry-run shows the plan without running anything. A subtask that needs specific hardware, a GPU box say, takes a hosts: list, and the scheduler keeps it there.
The speedup is close to linear when the shards are even: shard a suite across three hosts and it finishes in roughly a third of the time. The floor is your slowest shard, so keep shards similar in size. OpenData’s suite, about 14,000 tests across a Python data pipeline, a FastAPI backend and TypeScript packages, runs through one command, rr test.
rr’s sync also handles the drift problem. It skips .git, node_modules, .venv and the usual caches by default, applies your root .gitignore, and keeps directories that only exist on the remote (like a .venv built there) in place. When a lockfile changes locally, rr deletes the matching install directory on the remote before syncing, so the next install starts clean instead of reusing stale dependencies.
What happens when two runs want the same host?
rr takes a lock on a host before it syncs anything, so one run never rewrites files under another run’s command. If the first host is locked, rr tries the next one. If every host is locked, it cycles through them until one frees up, reporting who holds each lock while it waits. Two agents that run the suite at the same moment end up on different machines, or one waits its turn.
The lock is a directory created atomically under /tmp on the remote, and the holder refreshes it every 30 seconds. A lock that stops being refreshed goes stale after 90 seconds (lock.stale), and one left by a killed rr process on your own machine is cleared right away, so a crashed run doesn’t block the host for long. rr unlock clears one by hand. How long a run waits is set by lock.timeout for a single host (default 5m) and lock.wait_timeout for cycling through several (default 1m).
In a parallel run, a host whose connection fails is marked unavailable and its task is requeued on another host.
Git worktrees get their own remote directories automatically. In a linked worktree, the remote path ends in repo@worktree-name instead of repo, so two branches checked out side by side, such as two coding agents running in parallel, don’t overwrite each other’s copy on the remote.
How do you get results back in a usable form?
By default rr writes JSON events to stderr, one per phase (connect, sync, exec), and ends with a single result event. Your test command’s own output passes through unchanged, so it reads the same as it would locally. --pretty switches to spinners and colors for when a person is watching.
For pytest, Jest and Go test, rr parses the output and puts pass and fail counts in details.summary and each failing test, with its file:line and message, in details.failures. A run that collected no tests keeps its exit code but sets details.no_tests, so a typo in a path filter doesn’t read as a passing suite. Failures carry an error code such as LOCK_HELD, SSH_TIMEOUT or DEPENDENCY_MISSING with a suggested fix. That’s mostly for coding agents, which can branch on a code without parsing a paragraph of output; we go through the reasoning in how to design a CLI for AI agents.
Files come back with rr pull, the --pull flag on rr run, or a pull: list on a task:
rr pull coverage.xml
rr run "pytest --junitxml=report.xml" --pull report.xml
Pulls run whether the command passed or failed. In a parallel run, each subtask’s files land in their own directory under the destination, so three shards writing report.xml don’t overwrite each other. On the remote side, subtasks that land on the same host share its directory, so if two shards can run on one host, give each its own output path. Each parallel run also writes per-task logs and a summary.json to ~/.rr/logs/<task>-<timestamp>/, and rr logs lists recent runs.