Reputation: 1573
I'm trying to write a unit test with unittest
which tests that the behaviour of the below function only opens files that have a filename pattern of test_*.json
.
import glob
import json
def get_tests():
tests = []
for test in list(glob.iglob(f'**/test_*.json', recursive=True)):
with open(test) as f:
contents = f.read()
to_json = json.loads(contents)
tests.append(to_json)
return tests
I've seen this SO post How do I mock an open used in a with statement (using the Mock framework in Python)? which is to use patch
and mock_open
, however, trying to follow the top answer, it's mainly focussed on opening a mock file, rather than testing the function that OP has written testme
with a mock file. Also, I'm unsure in how to tie in filenames of a specific pattern either with mock file.
I want the unit tests to prove that it only opens files of that pattern, and not any other file.
Upvotes: 0
Views: 35