Turning a Bash Alias Into a One-Command Test Runner for Agents
Every test suite develops a ritual: activate the virtual environment, export some variables, change to the right directory, remember the right pytest flags, then press Enter. It's fine the first time. By the hundredth time it's friction, and friction is what makes people skip running tests locally before they push. A small alias or shell function turns that ritual into one command you don't have to think about.
There's a second reason to get this right now. A coding agent like Copilot's agent mode or Claude Code runs your tests too, sometimes dozens of times while it works on a change. It doesn't know your ritual unless you tell it, and it runs commands in a non-interactive shell where your personal aliases don't exist. If the agent has to rebuild the test command from scratch each time, you get inconsistent runs, missing environment variables and, sometimes, live API calls you never meant to make.
So I'll start with the alias, fix the common mistakes that make shell shortcuts lie about failures, and then move the runner into the repository so you, CI and your agents all use the same command.
Learn Node.js, Cucumber, GitHub Copilot, APIs, CI/CD, and modern automation by building a complete framework.
Start with the command you actually run
For a typical pytest API suite, the ritual looks something like this:
cd ~/projects/my-api-tests
source .venv/bin/activate
export BASE_URL=https://staging.example.com
export API_KEY=...
pytest tests/ -q --tb=short -m smoke
The first version of a runner is just those lines wrapped up. Before you write it, take the secrets out. Put BASE_URL and API_KEY in a .env file that's listed in .gitignore, and never type a key into an alias where it ends up in your shell config and history. Keeping test secrets out of your version control history covers why this matters beyond tidiness.
Alias first, then a function
A first attempt usually looks like this:
# ~/.bashrc or ~/.zshrc
alias runtests='(cd ~/projects/my-api-tests && source .venv/bin/activate && set -a && source .env && set +a && pytest -q --tb=short)'
The parentheses run everything in a subshell, so the cd and the exported variables don't leak into your session. set -a exports every variable the .env file defines, and set +a turns that off again. The && chain stops at the first failure, so a missing virtual environment errors out instead of running the wrong Python.
The limit is arguments. An alias is text substitution, so runtests -m smoke appends -m smoke after the closing parenthesis, which is a syntax error. That's the point to switch to a function, which receives its arguments as "$@":
runtests() {
(
set -o pipefail
cd ~/projects/my-api-tests || exit 1
source .venv/bin/activate
set -a; source .env; set +a
pytest -q --tb=short "$@" 2>&1 | tee /tmp/last-test-run.txt
)
}
Now runtests, runtests -m smoke, runtests tests/test_orders.py -k refund and runtests --lf all work. This definition works in both Bash and zsh. You don't need export -f, which is Bash-only and only matters if child shells need the function.
Keep the exit code honest
That set -o pipefail line matters more than it looks. Without it, a pipeline's exit status is the status of its last command, which is tee, and tee almost always succeeds. Your runner would report success on a failing run. A person reading the terminal might notice. A git hook, a script or an agent checking the exit code won't.
Watch the flags too. -v and -q cancel each other out, so pick one. I use -q --tb=short locally: a compact progress line, then a short traceback for each failure. Saving output with tee is worth keeping, because when someone asks what a failure said, the full text is in a file and you don't need to re-run.
Target environments without clobbering variables
A common pattern is a wrapper per environment, such as BASE_URL=https://staging.example.com runtests. It has a subtle bug. The function sources .env after the override, so if .env also sets BASE_URL, the file silently wins and you test the wrong environment. I prefer one file per environment, chosen by a single variable:
set -a; source ".env.${TARGET:-local}"; set +a
Then TARGET=staging runtests -m smoke loads .env.staging, and a missing file is an obvious error rather than a quiet fallback.
Move the runner into the repo so agents can use it
Your alias lives in your dotfiles. Your teammates don't have it, CI doesn't have it, and coding agents don't either: they generally run commands in a non-interactive shell where aliases from your shell config aren't loaded. So the real runner belongs in the repository, and the alias becomes a thin shortcut to it:
#!/usr/bin/env bash
# scripts/test.sh: the one command people, CI and agents use to run tests
set -euo pipefail
cd "$(dirname "$0")/.."
[[ -d .venv ]] || { echo "ERROR: .venv missing. See docs/local-setup.md" >&2; exit 2; }
envfile=".env.${TARGET:-local}"
[[ -f "$envfile" ]] || { echo "ERROR: $envfile not found" >&2; exit 2; }
set -a
source "$envfile"
set +a
# Default to the safe subset: no live third-party calls unless asked.
if [[ $# -eq 0 ]]; then
set -- -m "not live_api"
fi
mkdir -p reports
exec .venv/bin/python -m pytest -q --tb=short -rfE --junitxml=reports/junit.xml "$@"
A few choices here are aimed at agents as much as people. The pre-flight checks fail fast with a message that says what to do, which an agent can act on, rather than a confusing import error halfway through. Calling .venv/bin/python directly avoids depending on activation. exec means the script's exit code is pytest's exit code. -rfE prints a short summary of failures and errors at the end, which is the part an agent most needs to read. And the JUnit XML file gives CI and any triage tooling a structured result instead of scraped terminal text.
The default marker expression is the safety rail. An agent fixing a typo shouldn't be hitting a rate-limited partner API forty times. Marker design is its own topic, covered in tagging API tests so CI can run the right subset.
Then tell the agent the command exists. In .github/copilot-instructions.md, or your agent's equivalent instruction file:
## Running tests
- Run tests with scripts/test.sh. Do not call pytest directly.
- Pass a path or -k expression to run a subset: scripts/test.sh tests/api/test_orders.py
- Do not run -m live_api unless the task asks for it.
- A non-zero exit code is a failure. Read the summary at the end of the output.
Your personal shortcut is now one line: alias runtests='~/projects/my-api-tests/scripts/test.sh'. Because the alias just names a script, appended arguments work as you'd expect.
Keep it from drifting
Runners break quietly when the setup changes: a renamed directory, a new required variable, a different virtual environment tool. Because the script lives in the repo, changes to it go through review with the change that needed them, and the pre-flight checks flag drift the moment it happens. I also run the same script in CI, so a broken runner shows up there rather than on one person's laptop.
A one-command runner is a small thing that changes habits. People run tests more often when it's easy, and agents run them correctly when the right command is written down. Small, repeatable habits like this add up, as I discuss in keyboard shortcuts and habits that save real time. If you'd like to set up a full API test project like this with GitHub Copilot, my API testing courses walk through it.