0

I'm currently having trouble unit testing a function that has multiple prints on multiple rows:

def state_game(grid, score):

    print(grid[0])
    print(grid[1])
    print(grid[2])
    print(grid[3])
    print(f"Your score is {score}")
    return('------------')

What I'm trying to do is test for each row of print whether the line printed on python terminal is correct. So far I've tried this to capture what's printed on terminal :

import unittest
from unittest.mock import patch
"defining grid"
"defining score"
def test_state_game(self, mock_print):
         state_game(grid, score)
         mock_print.assert_called_with(grid[0])
         mock_print.assert_called_with(grid[1])
         mock_print.assert_called_with(grid[2])
         mock_print.assert_called_with(grid[3])
         mock_print.assert_called_with(f"Your score is {score}")

The problem is it seems that when it first tests whether grid[0] is printed on terminal, the test fails showing that expected is grid[0] when "Your score is {score}" has been printed (i.e. the last print of the state_game function). Any idea ?

  • 3
    Does this answer your question? [Python: Write unittest for console print](https://stackoverflow.com/questions/33767627/python-write-unittest-for-console-print) – Mady Daby Apr 20 '21 at 11:32
  • Well no, the solution you proposed is adapted to some other example. The user isn't testing multiple prints on the terminal – Nmd-AlM Apr 20 '21 at 12:09

2 Answers2

1

You could try this:

from unittest import mock

def test_state_game(self):
    # 'module' is where 'state_game' is defined
    with mock.patch("module.print") as print_mock:
        state_game(grid, score)
        print_mock.assert_called_with(grid[0])
        print_mock.assert_called_with(grid[1])
        print_mock.assert_called_with(grid[2])
        print_mock.assert_called_with(grid[3])
        print_mock.assert_called_with(f"Your score is {score}")
Laurent
  • 12,287
  • 7
  • 21
  • 37
  • This raises an assertion error (the same error mentionned in my first comment). I don't know if there is any way of doing what I want to do ... – Nmd-AlM Apr 24 '21 at 09:55
0

If we set a breakpoint in the debugger, and inspect the contents of the print_mock variable, we can see that print_mock.call_args_list[] array contains all of the strings

A little more experimentation give us this answer:

    from unitest import mock

    def test_state_game(self):
        # 'module' is where 'state_game' is defined
        with mock.patch("module.print") as print_mock:
            state_game(grid, score)
            self.assertEqual(print_mock.call_args_list[0].args[0], grid[0])
            self.assertEqual(print_mock.call_args_list[1].args[0], grid[1])
            self.assertEqual(print_mock.call_args_list[2].args[0], grid[2])
            self.assertEqual(print_mock.call_args_list[3].args[0], grid[3])
            self.assertEqual(print_mock.call_args_list[4].args[0], f"Your score is {score}")
SteveS
  • 514
  • 3
  • 16