Here is a simplified version of the problem I am facing: Let's say I have a function that accepts a path to a directory and then removes all of its content except (optionally) a designated "keep file",
import os
KEEP_FILE_CONSTANT = '.gitkeep'
def clear_directory(directory: str, retain: bool = True) -> bool:
try:
filelist = list(os.listdir(directory))
for f in filelist:
filename = os.path.basename(f)
if retain and filename == KEEP_FILE_CONSTANT:
continue
os.remove(os.path.join(directory, f))
return True
except OSError as e:
print(e)
return False
I am trying to write a unit test for this function that verifies the os.remove
was called. This is currently how I am testing it:
import pytest
from unittest.mock import ANY
@pytest.mark.parametrize('directory', [
('random_directory_1'),
('random_directory_2'),
# ...
])
@patch('module.os.remove')
def test_clear_directory(delete_function, directory):
clear_directory(directory)
delete_function.assert_called()
delete_function.assert_called_with(ANY)
Ideally, what I would like to assert in the test is the delete_function
was called with an argument containing directory
, i.e. something like,
delete_function.assert_called_with(CONTAINS(directory))
or something of that nature. I have been looking at PyHamcrest, specifically the contains_string function, but I am not sure how to apply it here or if it's even possible.
Is there any way to implement a CONTAINS matcher for this use case?