0

I am currently trying to make a Test Case for my program, but I am facing a difficulty. The way, I structured my code there is a function that takes the input, and according to that input it builds up a graph, and then there is a function that calculates something about the graph. I am also required to have this type of input for the job I am applying to. ( I can't do a simple unit test with a graph as an input).

The test file so far looks like this:

import unittest
from main_file import calculate, create_grid

class TestCase1(unittest.TestCase):

        def test1(self):

            data = create_grid()
            self.assertEqual(2, calculate(data))

The way it works right now, I input the needed data by myself with create_grid(). Is there a way I can emulate the computer/program to do it by itself (with specific values, as this is what I want). Thank you very much!

EDIT1:

This is the function code

def create_grid():
    rows, cols = [int(x) for x in input("Enter two numbers here: " + "\n").split()]

    for _ in range(rows):
        row = list(map(str, input().split()))
        grid.append(row)
    return grid

1 Answer 1

1
from unittest.mock import patch
from main_file import calculate, create_grid

class TestCase1(unittest.TestCase):

    def test1(self):
        with patch('builtins.input', side_effect=[1,2,3]):
            data = create_grid()
            self.assertEqual(2, calculate(data))
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the answer! Do you have any idea how to make it accept 2 inputs? I have edited my main post with the code for the function, so you can get a better idea. Thanks again!
@GerganZhekov First, you don't implement grid list inside create_grid. Second, why you need call input twice?
basically this is the task I am supposed to create. An input is supposed to look like this: 2 2 A B A B. This creates a dictionary grid (2 rows with A B) and represents a grid. As I said, this is the requirement from the task I am currently doing. And then the main function calculate() discovers the largest connected component in this grid (which currently works fine).
You can use the side_effect parameter instead return_value with an iterable to provide return values. Edit answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.