Metadata-Version: 2.5
Name: pyathy
Version: 0.1.22
Summary: Python acceptance testing harness (for) you: Gherkin for command-line programs
Project-URL: Homepage, https://github.com/bluemihai/pyathy
Project-URL: Documentation, https://github.com/bluemihai/pyathy/tree/main/docs
Project-URL: Source, https://github.com/bluemihai/pyathy
Project-URL: Issues, https://github.com/bluemihai/pyathy/issues
Author: Mihai Banulescu
License-Expression: MIT
License-File: LICENSE
Keywords: acceptance-testing,bdd,cli,gherkin,pytest-bdd,testing
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Framework :: Pytest
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Education
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Software Development :: Testing
Classifier: Topic :: Software Development :: Testing :: Acceptance
Classifier: Topic :: Software Development :: Testing :: BDD
Requires-Python: >=3.11
Requires-Dist: pytest-bdd>=9
Description-Content-Type: text/markdown

# pyathy

Test a Python command-line program with scenarios written in plain English.

You write `.feature` files (Gherkin: `Scenario`, `When`, `Then`) that type answers into
the program, fix its dice rolls, and check what it prints. pyathy runs each scenario against
a fresh start of `main.py` and shows each run, then one `✔`/`✘` line per scenario plus a total.
It is built on [pytest-bdd](https://github.com/pytest-dev/pytest-bdd) and needs Python 3.11+.

It was built for the Saxion ADSD python course, so that the acceptance tests of an assignment
can be read, and written, by people new to programming: the steps are the words a person
would use at the keyboard, and no code is needed. It tests any Python command-line program,
anywhere, the same way.

Ruby has this layer already: [Aruba](https://github.com/cucumber/aruba) gives Cucumber
ready-made steps for command-line programs (run it, type this, expect that output). Python's
pytest-bdd has no such layer; pyathy is that layer for Python.

## Install and use

Four ways to get the command. They run the same features the same way.

**From PyPI**, as a command on your PATH:

```
pipx install pyathy         # or: uv tool install pyathy
```

or inside a project's venv: `pip install pyathy`. For VS Code there is the pyathy extension,
`MB42.pyathy` in the Marketplace.

**No install: the `pyathy/` folder next to `main.py`.** The folder is built from this repo by
`scripts/build-folder.sh` into `dist/pyathy/`, with pytest-bdd inside, so nothing needs installing:

```
my-game/
  main.py
  features/
    game.feature
  pyathy/
```

```
python pyathy
```

In a course, an assignment's package is this folder plus its `features/`, zipped.

**Poetry**, with that same `pyathy/` folder in the project:

```
poetry add ./pyathy
poetry run pyathy
```

**pip**, in a venv: `pip install ./pyathy` for the folder, or from a clone of this repo:

```
python -m venv .venv
.venv/bin/pip install .          # Windows: .venv\Scripts\pip install .
cd examples/tictactoe
../../.venv/bin/pyathy           # Windows: ..\..\.venv\Scripts\pyathy
```

That runs the tic-tac-toe example's features against its `main.py`.

From the folder that holds `main.py` and `features/` (write `python pyathy …` for the folder,
`poetry run pyathy …` under Poetry):

```
pyathy                      run every features/*.feature against main.py
pyathy features/x.feature   run only that file (or folder)
pyathy check                find mistakes in the feature files, without running main.py
pyathy init                 create features/hello.feature (and a main.py) to start from
pyathy steps                every step you can use (plus your own, from features/*_steps.py)
pyathy -h                   usage and an example feature
```

Options, each with a short and a long form:

```
-q,  --quiet              only one line per scenario (a ✘ gets one reason line) and the total
-ff, --fail-fast          stop at the first scenario that fails
-nf, --next-failure       run the scenarios that failed last time, in order, stopping at the first that still fails
-s,  --solution [FOLDER]  run the features here against ./_solution/main.py, or ./FOLDER/main.py (-s obj2 means _solution-obj2)
-o,  --output show|hide|slow  the program's output in the report: show it (the default), hide it, or slow: a line at a time, to read along (on a terminal only)
     --delay SECONDS      the pause after each line under -o slow (default 0.1: a 100-line run takes 10 seconds; --delay alone means -o slow)
-h,  --help               this help
-v,  --version            pyathy's version and where it runs from
```

The report shows each scenario's run as a terminal would have (what was printed, what was
typed), then its `✔`/`✘` line with the reason under a `✘`, a summary per feature, and the last
line `TOTAL  n of m`; `-q` keeps only the `✔`/`✘` lines and the total. Every report ends with
`pyathy <version> · <where it runs from>`, the same as `-v`, so two copies on one machine are
never confused. `-nf` reads the failures remembered in `.pyathy/last-run.json`; `-s` is for
the teacher's folder, where the reference program sits in `_solution/`. The report's colours,
the long-run cut and the details of `-nf` and `-s`: [docs/running.md](https://github.com/bluemihai/pyathy/blob/main/docs/running.md).

`pyathy check` reads the feature files without running the program and lists, per file,
what would stop a scenario before it starts: a line no step matches (with the closest step,
`did you mean: I input Ada`), a file that is not valid Gherkin, a scenario or step indented
differently from the others, and a file step pyathy cannot find in the program. It exits with
1 when it found something; `--json` prints the same for an editor (the pyathy VS Code
extension uses it). Details and sample output: [docs/check.md](https://github.com/bluemihai/pyathy/blob/main/docs/check.md).

## A first feature

```gherkin
Feature: Tic Tac Toe

  Scenario: X wins with the top row
    When I input 1, 4, 2, 5, 3
    Then "Player X wins!" is printed
    And the program asks "Play again? (y/n)"
```

A `Scenario` is one run of the program, from a fresh start. `When` lines act (type answers,
roll dice) and `Then` lines check what the program did; `And` continues the line before it.

## Built-in steps, or your own

The built-in steps are the keyboard and the screen: type an answer, make the die show a
face, check what was printed, check a file. They need no code, so a feature can be read and
written by someone who has never seen a test; `examples/tictactoe` uses nothing else.
Your own steps speak the program's own language instead (`Ann has $1440`, `Ann owns
Harbour`): a few lines of Python each, in a `features/*_steps.py`, worth writing once a feature
file starts repeating the same five built-in lines to say one thing.

Each built-in works after `Given`, `When`, `Then`, `And` or `But`. Text checks ignore case and
trailing spaces. Colours in the program's output (`\033[31m`-style codes) are ignored when
matching, and shown in the report. `python pyathy steps` prints this list (colour-coded on
a terminal: the parts you replace in cyan).

Each step has its own page under [`docs/`](https://github.com/bluemihai/pyathy/blob/main/docs/README.md), with what it checks, when it is
needed, a scenario that runs and the message it fails with. Two guides: [turn-based
games](https://github.com/bluemihai/pyathy/blob/main/docs/turns.md) (`red rolls 6`, `red moves from 0 to 3`) and [starting from a saved
state](https://github.com/bluemihai/pyathy/blob/main/docs/saved-games.md) (`a saved game:`, `the program is started again`).

| Step | What it does |
|---|---|
| [`I start the program`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/start-the-program.md) / [`I run the program with puzzle.txt --hard`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/start-the-program.md) | run `main.py` until it asks something or ends; `with …` starts it with these command-line arguments |
| [`the program other.py`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/the-program-other-py.md) | run `other.py` instead of `main.py` |
| [`the program may take 30 seconds`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/the-program-may-take.md) | wait longer for a slow program (default 10) |
| [`main.py has DEBUG = True`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/file-has-constant.md) | before it starts, set `DEBUG = …` at the top of `main.py` to this Python value (in the copy; your file stays as it is) |
| [`main.py has a DEBUG setting`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/file-has-setting.md) | `main.py` has `DEBUG = …` at the top, outside any function (only checks it is there; sets nothing) |
| [`I input 4`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/i-input.md) / [`I input 4, left, no`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/i-input.md) | type answers, one per question |
| [`I input enter`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/i-input.md) / [`I input enter, q`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/i-input.md) | press enter, on its own or as one answer of the list (quote `"enter"` to type the word) |
| [`I roll 6`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/i-roll.md) / [`I roll 3 and 4`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/i-roll.md) | the next dice show these faces |
| [`the random choice is red`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/the-random-choice-is.md) | the next random pick returns this value |
| [`the dice show 2, 4, 1`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/the-dice-show.md) | a line printed has these faces in this order, any separators but digits between them (`2 4 1`, `[2, 4, 1]`), no digit right next to them |
| [`red rolls 6`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/rolls.md) / [`Ann rolls 4+6`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/rolls.md) | `I input enter` (if the program is waiting for it) then `I roll 6`, after checking it is red's turn: the last line announcing a turn names red and no other player, without digits (`Player red:`) |
| [`red rolls 3 and picks 2`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/rolls-and-picks.md) / [`… and answers 2`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/rolls-and-picks.md) | the same, then types 2 at the question that follows |
| [`these turns are played 3 times:`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/these-turns-are-played.md) + a table | one step per row (`\| red rolls 5 \|`), run in order, that many times |
| [`the players are red and green`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/the-players-are.md) | the names a turn announcement may use (otherwise: the names typed or rolled for so far) |
| [`red's turn is announced`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/turn-is-announced.md) / [`it is red's turn`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/turn-is-announced.md) | a line names red and no other player, without digits |
| [`a pawn moves from 0 to 3`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/moves-from-to.md) / [`red moves from 0 to 3`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/moves-from-to.md) | a line of this turn (since the last roll) has these two numbers, `to` or an arrow between them |
| [`nothing moves`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/nothing-moves.md) | no such line this turn |
| [`Kim is named the winner`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/is-named-the-winner.md) | a line printed names Kim, and no other player, and has won, wins, winner or win (case ignored) |
| [`"Hello" is printed`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/is-printed.md) | somewhere in the output |
| [`"Error" is printed 3 times`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/is-printed-n-times.md) | exactly that many times |
| [`"Game over" is not printed`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/is-not-printed.md) | nowhere in the output |
| [`the output starts with "Welcome"`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/the-output-starts-with.md) | the very first output |
| [`the output shows:`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/the-output-shows.md) + lines between `"""` | these lines, in a row |
| [`the line with "Total" also has "42"`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/the-line-with-also-has.md) | the last line printed with Total in it also has 42 |
| [`the "Bob" row has "7" under "Score"`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/the-row-has-under.md) / [`… has nothing under "Score"`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/the-row-has-under.md) | in the last table printed with a Score column (cells split by `│` or `\|`; tables lined up with spaces are not read), Bob's row has 7 there, or nothing |
| [`the program asks "Your name?"`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/the-program-asks.md) | it is now waiting for input after that question |
| [`"orange" is refused with a message`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/is-refused-with-a-message.md) | after typing it, a message was printed and the same question was asked again (a menu printed every time is not the message; `""` is pressing enter) |
| [`the program ends`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/the-program-ends.md) / [`the program is still running`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/the-program-is-still-running.md) | |
| [`a file "photos/beach.jpg"`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/a-file.md) | an empty file in the folder before the program starts (its folders made too) |
| [`a file "game.txt" containing "round 3"`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/a-file-containing.md) | put this one-line file in the folder before the program starts |
| [`a file "game.txt" with:`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/a-file-with.md) + lines between `"""` | the same, several lines |
| [`there is no file "game.txt"`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/there-is-no-file.md) | the program starts without it, whatever is in your folder (a saved game from playing, say) |
| [`a file "game.txt" is written`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/a-file-is-written.md) / [`no file is written`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/no-file-is-written.md) | the program made a file, or changed one |
| [`the file "game.txt" contains "round 4"`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/the-file-contains.md) | anywhere in the file |
| [`the file "game.txt" contains:`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/the-file-contains-lines.md) + lines between `"""` | these lines, in a row |
| [`a saved game:`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/a-saved-game.md) + lines between `"""` | these lines are in the program's save file before it starts |
| [`there is no saved game`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/there-is-no-saved-game.md) | the program starts without its save file |
| [`a saved game is written`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/a-saved-game-is-written.md) | the program made its save file, or changed it |
| [`the saved game holds:`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/the-saved-game-holds.md) + lines between `"""` | these lines, in a row, in the save file |
| [`no saved game is left`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/no-saved-game-is-left.md) | the save file was removed, or emptied |
| [`the program is started again`](https://github.com/bluemihai/pyathy/blob/main/docs/steps/the-program-is-started-again.md) | stop it and start it over in the same folder, so the files it wrote are still there |

## Your own steps

Every `*_steps.py` next to the `.feature` files is loaded. A step is a pattern in
[Cucumber style](https://github.com/cucumber/cucumber-expressions): `{name}` matches anything,
`{name:w}` one word, `{n:d}` a whole number, and each field becomes a parameter of the function.
`program.settle()` returns everything printed so far; `fail(program, message)` fails the step
with the program's last lines under the message.
`examples/monopoly/features/monopoly_steps.py` adds `Ann has $1460` and `Ann owns Harbour`:

```python
from pyathy.steps import fail, step

@step("{name:w} has ${cash:d}")
def has(program, name, cash):
    ...
```

`python pyathy steps` lists them under the built-ins, as written. The whole of it, with the
step above in full: [docs/own-steps.md](https://github.com/bluemihai/pyathy/blob/main/docs/own-steps.md).

## Pull an assignment's features

In a folder `lms grade` opened, `_node` links to the assignment in the curriculum and `_solution`
to its `solutionN/`. Next to `solutionN/` and `templateN/`, the curriculum keeps `featuresN/`:

```
pyathy pull          copy <_node>/featuresN/ into features/ (N from the _solution link)
pyathy               run features/
```

A pull into a folder that already has `features/` is refused unless `--force`.

Every pull, from the curriculum or a locked file, leaves one line in `features/pulled.txt`: when,
by which pyathy, what it pulled, and a fingerprint of the files (12 characters of their sha256;
the same files give the same fingerprint, wherever they came from):

```
pulled 2026-11-16 10:12 by pyathy 0.1.21: features2/ of lists at curriculum commit 4fd3e58, fingerprint 2f61c0e9a4b8
pulled 2026-11-16 10:12 by pyathy 0.1.21: variant 2 of python-exam, locked 2026-11-02 09:40 by pyathy 0.1.21, fingerprint 7b3d70192511
```

The curriculum commit is the last one that touched `featuresN/` (`plus uncommitted changes` when
the folder differs from it; left out when the curriculum is not a git checkout). A pull `--force`
replaces `features/` and the note with it. A plain `pyathy` ignores the note.

## Exams: lock and pull

An exam's features are not handed out; they stay in one password-locked file per exam,
holding every variant. The teacher who writes the exam locks it once:

```
pyathy lock python-exam variants/     variants/ holds 1/, 2/, … each with its .feature and *steps.py files
```

It asks the password twice and writes `python-exam.pyathy-locked` into the store: the folder
`$PYATHY_STORE`, or `~/.pyathy/locked/` when that is not set. An existing file is overwritten only
after a yes, or with `--force`. Colleagues who grade get a copy of that file and the password.

When the store is a git repo (a clone the teachers share), pyathy keeps it fresh: `pyathy lock`
commits the file it wrote ("lock <node>") and pushes, and `pyathy pull` runs `git pull --ff-only`
first. When git fails (offline, no remote), one `pyathy: warning:` line says so and the local copy
is used; a lock stays committed and goes up with the next push.

In the folder a grading tool opened for one attempt, named like `student@python-exam~1` (the exam
is read from the part between `@` and `~`):

```
pyathy pull         list the exam's variant numbers (no password needed)
pyathy pull 2       ask the password, put variant 2's files into features/
pyathy              run features/
```

A pull into a folder that already has `features/` is refused unless `--force`, which replaces the
whole `features/` folder.
For scripts, the password may come from `$PYATHY_PASSWORD`; it is never printed.

The locked file carries a stamp, readable without the password: when it was locked, by which
pyathy, and each variant's fingerprint. `pyathy lock` and a `pyathy pull` without a number print it:

```
locked 2026-11-02 09:40 by pyathy 0.1.21; fingerprints 1: cd663cb8515c, 2: 7b3d70192511
```

A file locked before the stamp (pyathy 0.1.20 and earlier) has none and still pulls.

What the lock does: without the password, the file shows only how many variants there are,
their numbers and the stamp; changing the stamp makes the file read as damaged; a wrong password or a damaged file is refused (`wrong password or damaged file`)
before anything is written. It uses Python's standard library only (scrypt for the key, an
HMAC-SHA256 keystream, an HMAC-SHA256 tag), so nothing needs installing.
What it does not do: once pulled, the files are plain text in that folder, readable by anyone who
can read the folder; and anyone with the password and the file can read every variant. Keep the
password with the people who grade, and remove `features/` when grading is done if the folder
travels further.

## Examples

`examples/tictactoe` (built-in steps only), `examples/sudoku` (boards checked with
`the output shows:`, a puzzle file as argument) and `examples/monopoly` (dice and its own
steps). `examples/edge` fails on purpose, to show the messages. Run them all with
`scripts/try-examples.sh`.

## License

MIT, see `LICENSE`.
