samedi 25 mai 2019

Creating test cases for my code (and mocking the inputs)

I created a code that can detect the largest connected component of a grid. the code works fine, but I am having difficulties with creating the test cases for the code.

For example, this is what a Test Case should do:

Testing the input:

3 3
R R G
G B R
R G B

This would give a result of "2", as R is the largest connected component (not counting the diagonals). However I am having difficulties with creating the test cases.

I read a lot of information online and from what I understand, I have to use 'unittest.mock' to mock the different inputs.

For reference, this is the full code of the program:

https://gist.github.com/gerganzh/8e76f3877a5c51c458ca54f782a08305

And this is what I have so far on the unit testing:

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

        class TestCase1(unittest.TestCase):

           @patch('create_grid.input')
           def test1(mock_input):
                mock_input.return_value = [
                '3 3',
                'R R B',
                'G G R',
                'R B G',
             ]

             self.assertEqual(calculate(), "2")

I know, I am far away from the correct way to do it, but from what I understand it should be something similar to this. I would really appreciate if someone can give me a tip!

Aucun commentaire:

Enregistrer un commentaire