0

I am trying to write unit tests for my search function which searches through a directory and returns a list of matching files which matches the query. While writing unit tests I realized that if I give it test data, the test data might just take up too much space. How can i get over this problem ? I thought of mocking test data but I am not familiar on how to do this. This is currently how one of my unit tests looks like

import search_function
import unittest


class searchDirTest(unittest.TestCase):

    def setUp(self):
        pass

    def test_file_name_case_sensitive(self):

        res = search_function.search_function('./testData', 'allsmall')
        self.assertEquals(res, ['./testData/allsmall.txt', './testData/testData_subdir/allsmall.txt'])
        self.assertNotEquals(res, ['./testData/ALLSMALL.txt'])
if __name__ == "__main__":
    unittest.main()

Also the search_function takes 2 arguments. The Path and the search query. Any pointers on how to achieve this ?

Kandarp Gandhi
  • 279
  • 2
  • 14
  • Possible duplicate of [Python: Creating a mock or fake directory with files for unittesting](https://stackoverflow.com/questions/37159714/python-creating-a-mock-or-fake-directory-with-files-for-unittesting) The answer to the linked question is a nice solution. Depending on your implementation, try and mock the functions you use to list the files in the given directory. – D Malan Aug 04 '19 at 01:37

1 Answers1

0

I would suggest refactoring your search_function so that it accepts a list of file names and returns all file names that match, something like case_insensitive_file_match(filenames). This is easily testable and the test data is simple to produce

Testing things external to your program should preferably be kept out of unit tests. This includes reading files/calling APIs etc

Iain Shelvington
  • 31,030
  • 3
  • 31
  • 50