From 8dd5bcec5043447d72a1766ea67fba132fe29700 Mon Sep 17 00:00:00 2001 From: venomade Date: Mon, 2 Nov 2026 10:43:47 +0000 Subject: [PATCH] Add calculator tests and a local quality gate --- .gitignore | 2 ++ README.md | 12 ++++++++++++ check.sh | 6 ++++++ requirements-dev.txt | 1 + test_calculator.py | 21 +++++++++++++++++++++ 5 files changed, 42 insertions(+) create mode 100755 check.sh create mode 100644 requirements-dev.txt create mode 100644 test_calculator.py diff --git a/.gitignore b/.gitignore index ff00544..229c2f6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ __pycache__/ +.pytest/cache/ +.venv/ *.pyc .env keep-demo.log diff --git a/README.md b/README.md index 23ffabe..65a0845 100644 --- a/README.md +++ b/README.md @@ -14,3 +14,15 @@ Run: `./run.sh` - Lastly enter a second number - Read the result, printed as `Result: ` followed by the answer. - When done stop with `Ctrl+c` + +## Testing +- Enter a `venv` and install `pytest`: +```sh +python3 -m venv .venv +source .venv/bin/activate +python3 -m pip install -r requirements-dev.txt +python3 -m pytest --version +``` +- Run: `./test.sh` +- All tests should return `PASSED` and the return code of `test.sh` should be `1`. + diff --git a/check.sh b/check.sh new file mode 100755 index 0000000..c7f34a0 --- /dev/null +++ b/check.sh @@ -0,0 +1,6 @@ +#!/bin/bash +set -e +cd -- "$(dirname -- "$0")" +echo "=== Calculator quality gate ===" +python3 -m pytest -v +echo "QUALITY GATE PASSED" diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 0000000..e079f8a --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1 @@ +pytest diff --git a/test_calculator.py b/test_calculator.py new file mode 100644 index 0000000..4d99646 --- /dev/null +++ b/test_calculator.py @@ -0,0 +1,21 @@ +from calculator import add, subtract, multiply, divide + +def test_add(): + assert add(2, 3) == 5 + +def test_divide(): + assert divide(6, 2) == 3 + +def test_subtract(): + assert subtract(5, 3) == 2 + +def test_multiply(): + assert multiply(4, 3) == 12 + +def test_divide_uneven(): + # Arrange + a, b = 5, 2 + # Act + result = divide(a, b) + # Assert + assert result == 2.5