From 4479a3339f5b9e222abe996af99d158c7dc464ae Mon Sep 17 00:00:00 2001 From: 3020772 <3020772@stud.hs-mannheim.de> Date: Mon, 8 Dec 2025 09:21:54 +0100 Subject: [PATCH] Add pytest dependency and initial unit test --- python/.devcontainer/Dockerfile | 22 +++++------ python/.devcontainer/devcontainer.json | 37 ++++++++++--------- python/.vscode/settings.json | 14 +++---- python/00_python_basics/test.py | 12 +++--- python/01_python_tooling/sum_calculator.py | 19 ++++++++++ .../01_python_tooling/test_sum_calculator.py | 26 +++++++++++++ 6 files changed, 88 insertions(+), 42 deletions(-) create mode 100644 python/01_python_tooling/sum_calculator.py create mode 100644 python/01_python_tooling/test_sum_calculator.py diff --git a/python/.devcontainer/Dockerfile b/python/.devcontainer/Dockerfile index 506fbab..7cf707e 100644 --- a/python/.devcontainer/Dockerfile +++ b/python/.devcontainer/Dockerfile @@ -1,11 +1,11 @@ -FROM python:3.11-slim - -RUN apt-get update && apt-get install -y build-essential - -ARG USERNAME=vscode -ARG USER_UID=1000 -ARG USER_GID=$USER_UID - -RUN groupadd --gid $USER_GID $USERNAME && useradd --uid $USER_UID --gid $USER_GID -m $USERNAME -USER $USERNAME -WORKDIR /workspace +FROM python:3.11-slim + +RUN apt-get update && apt-get install -y build-essential + +ARG USERNAME=vscode +ARG USER_UID=1000 +ARG USER_GID=$USER_UID + +RUN groupadd --gid $USER_GID $USERNAME && useradd --uid $USER_UID --gid $USER_GID -m $USERNAME +USER $USERNAME +WORKDIR /workspace diff --git a/python/.devcontainer/devcontainer.json b/python/.devcontainer/devcontainer.json index 881b3d5..ec8d953 100644 --- a/python/.devcontainer/devcontainer.json +++ b/python/.devcontainer/devcontainer.json @@ -1,18 +1,19 @@ -{ - "name": "Python 3.11 Dev Container", - "dockerFile": "Dockerfile", - "mounts": [ - "source=${localWorkspaceFolder},target=/workspace,type=bind" - ], - "settings": { - "python.linting.enabled": true, - "python.linting.pylintEnabled": true, - "python.formatting.provider": "black" - }, - "extensions": [ - "ms-python.python", - "ms-python.vscode-pylance" - ], - "postCreateCommand": "pip install --upgrade pip", - "remoteUser": "vscode" -} +{ + "name": "Python 3.11 Dev Container", + "dockerFile": "Dockerfile", + "mounts": [ + "source=${localWorkspaceFolder},target=/workspace,type=bind" + ], + "settings": { + "python.linting.enabled": true, + "python.linting.pylintEnabled": true, + "python.formatting.provider": "black" + }, + "extensions": [ + "ms-python.python", + "ms-python.vscode-pylance" + ], + + "postCreateCommand": "pip install --upgrade pip && pip install pytest black", + "remoteUser": "vscode" +} \ No newline at end of file diff --git a/python/.vscode/settings.json b/python/.vscode/settings.json index bb689c0..d47085c 100644 --- a/python/.vscode/settings.json +++ b/python/.vscode/settings.json @@ -1,8 +1,8 @@ -{ - "terminal.integrated.profiles.linux": { - "bash": { - "path": "bash", - "icon": "terminal-bash" - } - } +{ + "terminal.integrated.profiles.linux": { + "bash": { + "path": "bash", + "icon": "terminal-bash" + } + } } \ No newline at end of file diff --git a/python/00_python_basics/test.py b/python/00_python_basics/test.py index bb88a3f..ac4a77a 100644 --- a/python/00_python_basics/test.py +++ b/python/00_python_basics/test.py @@ -1,6 +1,6 @@ -def main(): - print("Hello, Dev Container World! 🚀") - print("Python Version:", __import__('sys').version) - -if __name__ == "__main__": - main() +def main(): + print("Hello, Dev Container World! 🚀") + print("Python Version:", __import__('sys').version) + +if __name__ == "__main__": + main() diff --git a/python/01_python_tooling/sum_calculator.py b/python/01_python_tooling/sum_calculator.py new file mode 100644 index 0000000..eb0ce7e --- /dev/null +++ b/python/01_python_tooling/sum_calculator.py @@ -0,0 +1,19 @@ +# Datei: sum_calculator.py + +def calculate_cumulative_sum(limit): + """ + Soll die kumulative Summe von 0 bis limit berechnen. + """ + total = 0 + + for i in range(limit): + total += i + return total + +if __name__ == "__main__": + test_number = 5 + + result = calculate_cumulative_sum(test_number) + + print(f"Die Zahl ist: {test_number}") + print(f"Ergebnis der Summe: {result}") diff --git a/python/01_python_tooling/test_sum_calculator.py b/python/01_python_tooling/test_sum_calculator.py new file mode 100644 index 0000000..3d06087 --- /dev/null +++ b/python/01_python_tooling/test_sum_calculator.py @@ -0,0 +1,26 @@ +# Datei: test_sum_calculator.py + +from sum_calculator import calculate_cumulative_sum + +def test_sum_for_zero(): + """Testet den Randfall, wenn die Obergrenze 0 ist.""" + # Arrange: Testdaten + limit = 0 + # Act: Funktion aufrufen + result = calculate_cumulative_sum(limit) + # Assert: Überprüfen des Ergebnisses (0 erwartet) + assert result == 0 + +def test_sum_for_five(): + """Testet den allgemeinen Fall für die Zahl 5 (0+1+2+3+4+5 = 15).""" + # Arrange: Testdaten + limit = 5 + # Act: Funktion aufrufen + result = calculate_cumulative_sum(limit) + # Assert: Überprüfen des Ergebnisses (15 erwartet) + assert result == 15 + +def test_sum_is_correct_type(): + """Testet, ob die Funktion einen Integer zurückgibt.""" + # Arrange, Act, Assert + assert isinstance(calculate_cumulative_sum(3), int) \ No newline at end of file