Metadata-Version: 2.5
Name: pyathy
Version: 0.2.0
Summary: Plain-English acceptance tests for Python command-line programs
Project-URL: Homepage, https://gitlab.com/saxionnl/42/pyathy-public
Project-URL: Documentation, https://gitlab.com/saxionnl/42/pyathy-public/-/tree/main/docs
Project-URL: Source, https://gitlab.com/saxionnl/42/pyathy-public
Project-URL: Issues, https://gitlab.com/saxionnl/42/pyathy-public/-/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.

## Quick start

Three commands, in an empty folder:

```
pipx install pyathy         # or: uv tool install pyathy
pyathy init                 # creates features/hello.feature and a main.py
pyathy                      # runs it: TOTAL  1 of 1
```

`pyathy init` writes this feature:

```gherkin
Feature: Hello World

  Scenario: the program greets the world
    When I start the program
    Then "Hello World" is printed
    And the program ends
```

and this one-line `main.py`:

```python
print("Hello World")
```

## 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.

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+.

Written so that people new to programming can read and write the tests: the steps are the
words you would use at the keyboard, and no test 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.

## Is pyathy for me?

For developers, what it does and does not do today:

- it tests Python command-line programs, started as a file (`main.py`, or `the program other.py`)
- a package started with `python -m` is not supported yet
- stdout and stderr are read as one output; no step tells them apart
- no step checks the program's exit status yet
- pyathy itself exits with 1 when a scenario fails, so it fits CI

## Install and use

Two 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.

**Poetry**, in a Poetry project, as a dev dependency from PyPI:

```
poetry add --group dev pyathy
poetry run pyathy
```

Two more ways, both needing a clone of this repo (a no-install `pyathy/` folder, and a
venv from the clone), are in `docs/install.md`.

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

```
pyathy                      run every features/*.feature against the program (main.py unless a feature names another)
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` runs the
same features against a reference version in `_solution/`, e.g. to check the features
themselves pass on a known-good version. The report's colours, the long-run cut and the details of `-nf` and `-s`: [docs/running.md](https://gitlab.com/saxionnl/42/pyathy-public/-/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://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/check.md).

## 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://gitlab.com/saxionnl/42/pyathy-public/-/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://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/turns.md) (`red rolls 6`, `red moves from 0 to 3`) and [starting from a saved
state](https://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/saved-games.md) (`a saved game:`, `the program is started again`).

### First steps

| Step | What it does |
|---|---|
| [`I start the program`](https://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/steps/start-the-program.md) / [`I run the program with puzzle.txt --hard`](https://gitlab.com/saxionnl/42/pyathy-public/-/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 |
| [`I input 4`](https://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/steps/i-input.md) / [`I input 4, left, no`](https://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/steps/i-input.md) | type answers, one per question |
| [`"Hello" is printed`](https://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/steps/is-printed.md) | somewhere in the output |
| [`the program asks "Your name?"`](https://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/steps/the-program-asks.md) | it is now waiting for input after that question |
| [`"orange" is refused with a message`](https://gitlab.com/saxionnl/42/pyathy-public/-/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://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/steps/the-program-ends.md) / [`the program is still running`](https://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/steps/the-program-is-still-running.md) | |

### Starting the program, and more output checks

| Step | What it does |
|---|---|
| [`the program other.py`](https://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/steps/the-program-other-py.md) | run `other.py` instead of `main.py` |
| [`the program may take 30 seconds`](https://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/steps/the-program-may-take.md) | wait longer for a slow program (default 10) |
| [`main.py has DEBUG = True`](https://gitlab.com/saxionnl/42/pyathy-public/-/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://gitlab.com/saxionnl/42/pyathy-public/-/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 enter`](https://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/steps/i-input.md) / [`I input enter, q`](https://gitlab.com/saxionnl/42/pyathy-public/-/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) |
| [`"Error" is printed 3 times`](https://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/steps/is-printed-n-times.md) | exactly that many times |
| [`"Game over" is not printed`](https://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/steps/is-not-printed.md) | nowhere in the output |
| [`the output starts with "Welcome"`](https://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/steps/the-output-starts-with.md) | the very first output |
| [`the output shows:`](https://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/steps/the-output-shows.md) + lines between `"""` | these lines, in a row |
| [`the line with "Total" also has "42"`](https://gitlab.com/saxionnl/42/pyathy-public/-/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://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/steps/the-row-has-under.md) / [`… has nothing under "Score"`](https://gitlab.com/saxionnl/42/pyathy-public/-/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 |

### Files and saved games

| Step | What it does |
|---|---|
| [`a file "photos/beach.jpg"`](https://gitlab.com/saxionnl/42/pyathy-public/-/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://gitlab.com/saxionnl/42/pyathy-public/-/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://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/steps/a-file-with.md) + lines between `"""` | the same, several lines |
| [`there is no file "game.txt"`](https://gitlab.com/saxionnl/42/pyathy-public/-/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://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/steps/a-file-is-written.md) / [`no file is written`](https://gitlab.com/saxionnl/42/pyathy-public/-/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://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/steps/the-file-contains.md) | anywhere in the file |
| [`the file "game.txt" contains:`](https://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/steps/the-file-contains-lines.md) + lines between `"""` | these lines, in a row |
| [`a saved game:`](https://gitlab.com/saxionnl/42/pyathy-public/-/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://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/steps/there-is-no-saved-game.md) | the program starts without its save file |
| [`a saved game is written`](https://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/steps/a-saved-game-is-written.md) | the program made its save file, or changed it |
| [`the saved game holds:`](https://gitlab.com/saxionnl/42/pyathy-public/-/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://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/steps/no-saved-game-is-left.md) | the save file was removed, or emptied |
| [`the program is started again`](https://gitlab.com/saxionnl/42/pyathy-public/-/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 |

### Dice, random and turns

| Step | What it does |
|---|---|
| [`I roll 6`](https://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/steps/i-roll.md) / [`I roll 3 and 4`](https://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/steps/i-roll.md) | the next dice show these faces |
| [`the random choice is red`](https://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/steps/the-random-choice-is.md) | the next random pick returns this value |
| [`the dice show 2, 4, 1`](https://gitlab.com/saxionnl/42/pyathy-public/-/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://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/steps/rolls.md) / [`Ann rolls 4+6`](https://gitlab.com/saxionnl/42/pyathy-public/-/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://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/steps/rolls-and-picks.md) / [`… and answers 2`](https://gitlab.com/saxionnl/42/pyathy-public/-/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://gitlab.com/saxionnl/42/pyathy-public/-/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://gitlab.com/saxionnl/42/pyathy-public/-/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://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/steps/turn-is-announced.md) / [`it is red's turn`](https://gitlab.com/saxionnl/42/pyathy-public/-/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://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/steps/moves-from-to.md) / [`red moves from 0 to 3`](https://gitlab.com/saxionnl/42/pyathy-public/-/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://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/steps/nothing-moves.md) | no such line this turn |
| [`Cam is named the winner`](https://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/steps/is-named-the-winner.md) | a line printed names Cam, and no other player, and has won, wins, winner or win (case ignored) |

Dice or random choice? It depends on the random call:

- over exactly six consecutive whole numbers (`randint(1, 6)`, `randrange(6)`, `choice([1, 2, 3, 4, 5, 6])`): a die, so `I roll 4` (the 4th side: `randrange(6)` returns 3, so `randrange(6) + 1` shows 4)
- any other call (`randint(1, 10)`, `choice(colours)`): `the random choice is 7`, or `red`

## 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://gitlab.com/saxionnl/42/pyathy-public/-/blob/main/docs/own-steps.md).

## 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`.

## Courses

`pyathy pull` and `pyathy lock` hand out and lock feature files; see `docs/grading.md`.

## License

MIT, see `LICENSE`.
