pytest error: argument -k: expected one argument (tested)
pytest: error: argument -k: expected one argument means nothing followed -k, usually because a CI variable was empty and unquoted (pytest -k $FILTER). Quote it, pytest -k "$FILTER", and quote expressions with spaces: -k "login or logout".
pytest parses its command line with argparse. -k, -m, -p, --maxfail and similar options take a value; if nothing follows them, you get error: argument -k: expected one argument and exit code 4 (usage error) before any test runs. Measured on pytest 9.1.1.
1. The option has no value
$ cat test_auth.py
def test_login():
assert 1
def test_logout():
assert 1
exit code: 0
$ pytest -k
ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: argument -k: expected one argument
exit code: 4
2. The usual CI cause: an empty, unquoted variable
$ FILTER="" ; pytest -q -k $FILTER
ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: argument -k: expected one argument
exit code: 4
$ FILTER="" ; pytest -q -k "$FILTER"
.. [100%]
2 passed in 0.00s
exit code: 0
In a shell script or CI step, pytest -k $FILTER with an empty FILTER expands to plain pytest -k. Quote it (-k "$FILTER"): an empty expression selects all tests, as shown. Or only add -k when the variable is set.
3. Expressions with spaces must be one argument
$ pytest -q -k login logout
no tests ran in 0.00s
ERROR: file or directory not found: logout
exit code: 4
$ pytest -q -k "login or logout"
.. [100%]
2 passed in 0.00s
exit code: 0
$ pytest -q -k=login
. [100%]
1 passed, 1 deselected in 0.00s
exit code: 0
Unquoted, logout is taken as a path, giving file or directory not found (exit 4). Quote the whole expression; -k=login also works.
Related: pytest error: unrecognized arguments, pytest exit codes.
Frequently asked questions
What does pytest 'expected one argument' mean?
An option that needs a value (-k, -m, -p, --maxfail) was given without one. pytest exits with code 4 before running tests.
Why does pytest -k work locally but fail in CI?
The CI variable used after -k is empty and unquoted, so the shell passes no value. Quote it: -k "$FILTER".
How do I pass a -k expression with spaces?
Quote the whole expression: pytest -k "login or logout".
Written with AI assistance. Measured on 2026-10-23 with pytest 9.1.1 on Python 3.13.5 in a clean virtualenv (bash). Each block was run verbatim and the output pasted unedited. PlainHeaders is our extension; Python and pytest are free.