Pylerno
English

Test a complete command

A unit test can show that a function adds a task to a list. An integration test can show that a second Python process sees the task after the first process exits. That extra check exercises argument parsing, file storage, and output together.

On this page

Define the observable behavior

Start in the TaskFlow app directory from the packaging guide. The requirement is concrete: after adding Review patch, a later list --json must return exactly one unfinished task with that title and ID 1. Use a new data file for every test so it can run twice, in any order.

Exercise the boundary

Create tests/test_roundtrip.py alongside the supplied tests. tmp_path is a pytest fixture. For this test invocation it supplies an isolated temporary directory as a Path. Keeping the filename inside it leaves the learner’s real tasks untouched.

python
import json
from pathlib import Path
import subprocess
import sys

PROJECT = Path(__file__).resolve().parents[1]


def command(path, *arguments):
    return subprocess.run(
        [sys.executable, "-m", "taskflow", "--data", str(path), *arguments],
        cwd=PROJECT,
        capture_output=True,
        text=True,
        timeout=10,
    )


def test_task_survives_restart(tmp_path):
    path = tmp_path / "tasks.json"
    added = command(path, "add", "Review patch")
    assert added.returncode == 0, added.stderr

    listed = command(path, "list", "--json")
    assert listed.returncode == 0, listed.stderr
    assert json.loads(listed.stdout) == [
        {"id": 1, "title": "Review patch", "done": False}
    ]

sys.executable selects the interpreter that launched pytest. Each call starts a fresh process, while both calls use the same file. Inspect the exit status before decoding the JSON. Otherwise an early command error can look like a JSON syntax problem.

Prove the failure path

Append this second test to the same file. It snapshots the bytes after a valid write and verifies they survive a rejected command. Checking only the exit status could miss a bug that reports an error after changing the file.

python
def test_unknown_id_preserves_file(tmp_path):
    path = tmp_path / "tasks.json"
    assert command(path, "add", "Keep me").returncode == 0
    before = path.read_bytes()

    result = command(path, "done", "999")

    assert result.returncode == 2
    assert result.stderr.strip()
    assert path.read_bytes() == before

Run and extend the tests

Use the Python interpreter from your build environment. On macOS or Linux, run the commands below from app. On Windows substitute .build-env\Scripts\python.exe. Both new tests should pass.

sh
.build-env/bin/python -m pip install pytest
.build-env/bin/python -m pytest -q tests/test_roundtrip.py

For practice, extend the successful test with done 1. Query list --status open --json and verify the resulting list is empty. Then query list --status done --json and verify it contains the original ID with done set to True.

Check your understanding

Which check proves that a task survives restarting the program?

Choose one answer

Search documentation

Share this page