From c5537a297ea7a764e0815fdc5c585076add7f30c Mon Sep 17 00:00:00 2001 From: Hong-Phuc Bui <hong-phuc.bui@htwsaar.de> Date: Fri, 16 May 2025 11:25:29 +0200 Subject: [PATCH] auto-test --- /dev/null | 16 -------- ub-3/tictactoe.py | 28 +++++++------- ub-3/tictactoe_test.py | 34 +++++++++++++++++ ub-3/.gitignore | 5 ++ ub-3/anleitung.md | 17 ++++++++ 5 files changed, 70 insertions(+), 30 deletions(-) diff --git a/ub-3/.gitignore b/ub-3/.gitignore new file mode 100644 index 0000000..7d7c70d --- /dev/null +++ b/ub-3/.gitignore @@ -0,0 +1,5 @@ +__pycache__/ +.pytest_cache/ +.coverage +htmlcov/ + diff --git a/ub-3/anleitung.md b/ub-3/anleitung.md new file mode 100644 index 0000000..3633556 --- /dev/null +++ b/ub-3/anleitung.md @@ -0,0 +1,17 @@ +# automatisierter Test in Python + + +## Python Package + + +``` +pip install -U pytest +pip install -U pytest-cov +``` + + +## Test + +``` +pytest --cov=tictactoe --cov-report html tictactoe_test.py +``` diff --git a/ub-3/tictactoe.py b/ub-3/tictactoe.py index dc96c9b..167ff15 100644 --- a/ub-3/tictactoe.py +++ b/ub-3/tictactoe.py @@ -101,22 +101,22 @@ - -print_tafel(tafel) -aktuelle_spieler = 1 - -(spiel_ende, gewinner) = (False, 0) - -while not spiel_ende : - (spiel_ende, gewinner) = spiel_bewerten(tafel) - user_input(aktuelle_spieler, tafel) +if __name__ == "__main__": print_tafel(tafel) - if aktuelle_spieler == 1: - aktuelle_spieler = 2 - else: - aktuelle_spieler = 1 + aktuelle_spieler = 1 -print("Spiel beendet!") + (spiel_ende, gewinner) = (False, 0) + + while not spiel_ende : + (spiel_ende, gewinner) = spiel_bewerten(tafel) + user_input(aktuelle_spieler, tafel) + print_tafel(tafel) + if aktuelle_spieler == 1: + aktuelle_spieler = 2 + else: + aktuelle_spieler = 1 + + print("Spiel beendet!") diff --git a/ub-3/tictactoe.test.py b/ub-3/tictactoe.test.py deleted file mode 100644 index 04a165d..0000000 --- a/ub-3/tictactoe.test.py +++ /dev/null @@ -1,16 +0,0 @@ -from tictactoe import pruefe_comination, has_spieler_gewonnen, PL1, PL2, EMPTY - - -test_board = [ - [PL1, PL1, PL1], - [EMPTY, PL2, EMPTY], - [EMPTY, PL2, EMPTY] -] - - -result = pruefe_comination( (0,0,"hvd"), test_board ) - -print(result) - -result = has_spieler_gewonnen(PL1, test_board) -print(result) diff --git a/ub-3/tictactoe_test.py b/ub-3/tictactoe_test.py new file mode 100644 index 0000000..bb9825d --- /dev/null +++ b/ub-3/tictactoe_test.py @@ -0,0 +1,34 @@ +from tictactoe import pruefe_combination, has_spieler_gewonnen, PL1, PL2, EMPTY + + +def test_pruefe_combination_horizontal(): + test_board = [ + [PL1, PL1, PL1], + [EMPTY, PL2, EMPTY], + [EMPTY, PL2, EMPTY] + ] + + result = pruefe_combination( (0,0,"hvd"), test_board ) + assert result + + +def test_pruefe_combination_vertikal(): + test_board = [ + [PL1, EMPTY, EMPTY], + [PL1, PL2, EMPTY], + [PL1, PL2, EMPTY] + ] + + result = pruefe_combination( (0,0,"hvd"), test_board ) + assert result + + +def test_pruefe_combination_diagonal(): + test_board = [ + [PL1, EMPTY, EMPTY], + [PL2, PL1, EMPTY], + [PL1, EMPTY, PL1] + ] + + result = pruefe_combination( (0,0,"hvd"), test_board ) + assert result -- Gitblit v1.10.0-SNAPSHOT