From 6f2fad886cdcd954ec9450a29124f704bc659d73 Mon Sep 17 00:00:00 2001 From: Serhii Horodilov Date: Sun, 15 Oct 2023 23:59:35 +0300 Subject: [PATCH] Add test case for perform quiz function --- tests/quiz/conftest.py | 10 ++++++++++ tests/quiz/func_test.py | 16 +++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/tests/quiz/conftest.py b/tests/quiz/conftest.py index 6610946..bb940fe 100644 --- a/tests/quiz/conftest.py +++ b/tests/quiz/conftest.py @@ -39,3 +39,13 @@ def questions(): "answer": "opt 3", }, ] + + +@pytest.fixture +def correct_answers(): + return "1", "2", "4", "3", "3" + + +@pytest.fixture +def incorrect_answers(): + return "2", "1", "2", "1", "2" diff --git a/tests/quiz/func_test.py b/tests/quiz/func_test.py index 38b3219..7d4f824 100644 --- a/tests/quiz/func_test.py +++ b/tests/quiz/func_test.py @@ -9,8 +9,12 @@ def test_load_questions_from_file(questions): assert quiz.load_questions_from_file(fixture) == questions -def test_perform_quiz(questions): - ... # TODO +def test_perform_quiz(questions, correct_answers, incorrect_answers): + with patch("builtins.input", side_effect=correct_answers): + assert quiz.perform_quiz(questions) == len(questions) + + with patch("builtins.input", side_effect=incorrect_answers): + assert quiz.perform_quiz(questions) == 0 def test_display_question(question): @@ -18,9 +22,11 @@ def test_display_question(question): def test_gather_answer(question): - with patch("builtins.input", return_value="1"): - answer = quiz.gather_answer(question) - assert answer == 0 + with patch("builtins.input", side_effect=["1", "3", "4", "2"]): + assert quiz.gather_answer(question) == 1 + assert quiz.gather_answer(question) == 3 + assert quiz.gather_answer(question) == 4 + assert quiz.gather_answer(question) == 2 def test_gather_answer_repeat():