A simple calculator toolkit written in Python, with several UIs, tests, and quality assurance facilities.
UI code is clearly separated from the business logic. Actually, two sorts of UIs are provided: a command-line interface (CLI) and a graphical user interface (GUI), based on Kivy.
Tests are provided for the business logic, for the CLI, and for the GUI.
All test cases are written using the unittest framework.
Test code is clearly separated from the main code: it is placed in a tests directory.
Quality assurance facilities include:
- test coverage
- static code analysis
- Python 3.11
- Kivy 2.3
- Coverage.py 7.4.0
- Mypy 1.9.0
- Pytest 8.1.0
The software can be run as either a desktop app or a command-line tool.
Recall restoring the dependencies before running the app for the first time.
Open a shell in this directory, and run the following command:
python -m calculator.ui.guiOpen a shell in this directory, and run the following command:
python -m calculator.ui.cli EXPRESSIONwhere EXPRESSION is a mathematical expression to be evaluated.
Open a shell in this directory, and run the following command:
pip install -r requirements-dev.txtThis shall restore also the dependencies from the requirements.txt file.
Assumption: you have restored the dependencies.
You may exploit the following commands to run all the tests:
python -m unittest discover -v -s testsWhile running the tests, the calculator GUI may quickly appear and disappear, multiple times. This is normal, as the tests are exercising the GUI.
You may exploit the following commands to run only the tests for the business logic:
python -m unittest discover -v -s tests -p 'test_model.py'A similar command may be exploited to test just the CLI (test_cli.py) or the GUI (test_gui.py).
Assumption: you have restored the dependencies, and you are using Visual Studio Code.
Lunch configurations are already provided in the .vscode directory.
You may switch to the "Run and Debug" view, and select the desired configuration from the dropdown menu.
Instructions here: https://code.visualstudio.com/docs/editor/debugging
Assumption: you have restored the dependencies.
Open a shell in this directory, and run the following commands (2 commands in a row):
python -m coverage run -m unittest discover -v -s tests
python -m coverage report -mThe output should be similar to the following:
Name Stmts Miss Cover Missing
------------------------------------------------------
calculator\__init__.py 38 3 92% 13, 39, 48
calculator\ui\cli.py 21 3 86% 16-17, 27
calculator\ui\gui.py 57 8 86% 55-57, 61, 63, 65, 69, 76
tests\test_cli.py 15 0 100%
tests\test_gui.py 30 0 100%
tests\test_model.py 38 0 100%
------------------------------------------------------
TOTAL 199 14 93%
You may make the output more pleasant, by generating a HTML report:
python -m coverage htmlThen, open the generated file htmlcov/index.html in your web browser.
Assumption: you have restored the dependencies.
Open a shell in this directory, and run the following command:
python -m compileall calculator testsIf no error is printed, the code is free from syntax / import errors.
Assumption: you have restored the dependencies.
Open a shell in this directory, and run the following command:
python -m mypy calculator testsThe output should be similar to the following:
calculator\__init__.py:13: error: Unsupported operand types for + ("str" and "int") [operator]
calculator\__init__.py:44: error: Parameterized generics cannot be used with class or instance checks [misc]
calculator\__init__.py:44: error: Argument 2 to "isinstance" has incompatible type "<typing special form>"; expected "_ClassInfo" [arg-type]
calculator\__init__.py:46: error: Returning Any from function declared to return "int | float" [no-any-return]
Found 4 errors in 1 file (checked 6 source files)