$ uv init --package demo-stdin-stdout-pytest
Initialized project `demo-stdin-stdout-pytest` at `/home/cpitcher/working/python/examples/demo-stdin-stdout-pytest`
$ cd demo-stdin-stdout-pytest/
$ uv add --dev pytest
Using CPython 3.14.0
Creating virtual environment at: .venv
Resolved 7 packages in 172ms
Built demo-stdin-stdout-pytest @ file:///home/cpitcher/working/python/exampl
Prepared 1 package in 5ms
Installed 6 packages in 14ms
+ demo-stdin-stdout-pytest==0.1.0 (from file:///home/cpitcher/working/python/examples/demo-stdin-stdout-pytest)
+ iniconfig==2.3.0
+ packaging==25.0
+ pluggy==1.6.0
+ pygments==2.19.2
+ pytest==8.4.2
$ git add .gitignore .python-version README.md pyproject.toml src/ uv.lock
$ git commit -m "Initial commit"
[master (root-commit) ccfa99c] Initial commit
6 files changed, 114 insertions(+)
create mode 100644 .gitignore
create mode 100644 .python-version
create mode 100644 README.md
create mode 100644 pyproject.toml
create mode 100644 src/demo_stdin_stdout_pytest/__init__.py
create mode 100644 uv.lock
-
update
src/demo_pytest/__init__.py{.verbatim} to:import json from pathlib import Path def prompt_for_user_data() -> dict: name = input("Enter your name: ") age = input("Enter your age: ") city = input("Enter your city: ") return { "name": name, "age": age, "city": city } def save_user_data(data: dict, filepath: Path) -> None: with open(filepath, "w") as f: json.dump(data, f) def main() -> None: data = prompt_for_user_data() save_user_data(data, Path.cwd() / "data.json") print(f"Thanks {data['name']}") if __name__ == "__main__": main()
-
run the
main{.verbatim} function insrc/demo_pytest/__init__.py{.verbatim}:$ uv run demo-stdin-stdout-pytest Enter your name: alice Enter your age: 20 Enter your city: chicago Thanks alice -
examine the JSON output:
$ cat data.json {"name": "alice", "age": "20", "city": "chicago"} $ jq . data.json { "name": "alice", "age": "20", "city": "chicago" } -
add and commit the implementation
$ git add src/demo_stdin_stdout_pytest/__init__.py $ echo data.json >> .gitignore $ git add .gitignore $ git commit -m "Added implementation" [master 38acb3f] Added implementation 2 files changed, 24 insertions(+), 1 deletion(-) -
create Python test code in
tests/test_user_data.py{.verbatim}:from demo_stdin_stdout_pytest import prompt_for_user_data, save_user_data, print_confirmation import json from pathlib import Path def test_prompt_for_user_data(monkeypatch): inputs = iter(["Alice", "20", "Chicago"]) monkeypatch.setattr('builtins.input', lambda _: next(inputs)) data = prompt_for_user_data() assert data == { "name": "Alice", "age": "20", "city": "Chicago" } def test_save_user_data(tmp_path): data = { "name": "Bob", "age": "25", "city": "New York" } filepath = tmp_path / "user_data.json" save_user_data(data, filepath) with open(filepath, "r") as f: loaded_data = json.load(f) assert loaded_data == data def test_print_confirmation(capsys): print_confirmation("Charlie") captured = capsys.readouterr() assert captured.out.strip() == "Thanks Charlie"
-
run pytest:
$ uv run pytest ============================ test session starts ============================= platform linux -- Python 3.14.0, pytest-8.4.2, pluggy-1.6.0 rootdir: /home/cpitcher/working/python/examples/demo-stdin-stdout-pytest configfile: pyproject.toml collected 3 items tests/test_user_data.py ... [100%] ============================= 3 passed in 0.01s ============================== -
add and commit the tests:
$ git add tests/test_user_data.py $ git commit -m "Added tests" [master 9f43ca7] Added tests 1 file changed, 33 insertions(+) create mode 100644 tests/test_user_data.py