I'm creating files with pathlib.Path.touch
with the following method that I'd like to assert in a unittest using unitest.mock.patch
decorator:
sessions.py
def generate_folders(paths: list):
for p in paths:
p.mkdir()
Path(p / "output").mkdir()
Path(p / "output" / "output_file").touch()
Path(p / "session_file").touch()
test
@patch("os.mkdir")
@patch("pathlib.Path.touch")
def test_folder_creation(self, touch_mock, mkdir_mock):
tags = ["concepts", "models", "animations", "vfx", "sfx", "vo", "music"]
paths = sessions.generate_paths(category="character", name="player", tags=tags)
sessions.generate_folders(paths)
for p in paths:
mkdir_mock.assert_any_call(p, 511)
mkdir_mock.assert_any_call(p / "output", 511)
touch_mock.assert_any_call(Path(p / "output" / "output_file"), 511)
touch_mock.assert_any_call(Path(p / "session_file"), 511)
I can assert folder creation by os.mkdir
without any problem and I can also assert that pathlib.Path.touch
has been called. However, asserting specific calls with assert_any_call()
isn't possible. My test throws this output:
ssF...
======================================================================
FAIL: test_folder_creation (sessions.tests.sessions_tests.TestSessionsCreation.test_folder_creation)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\juank\AppData\Local\Programs\Python\Python313\Lib\unittest\mock.py", line 1424, in patched
return func(*newargs, **newkeywargs)
File "C:\Users\juank\dev\projects\python\gamedev_eco\sessions\tests\sessions_tests.py", line 51, in test_folder_creation
touch_mock.assert_any_call(Path(p / "output" / "output_file"), 511)
~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\juank\AppData\Local\Programs\Python\Python313\Lib\unittest\mock.py", line 1048, in assert_any_call
raise AssertionError(
'%s call not found' % expected_string
) from cause
AssertionError: touch(WindowsPath('C:/Users/juank/dev/projects/python/gamedev_eco/sessions/character/player/concepts/output/output_file'), 511) call not found
----------------------------------------------------------------------
Ran 6 tests in 0.004s
FAILED (failures=1, skipped=2)
What confuses me about this test result is that in spite of checking that method input values are the same with pdb
debugger, still the test fails. I also double-check that the decorators were working on os.mkdir
and pathlib.Path.touch
respectively.
On the other hand I tried patching touch
with with
but the test doesn't pass either. I can't tell if this issue is related to my code or to my test so I'd like to ask the community for hints please. What am I doing wrong? What am I missing?
I'm creating files with pathlib.Path.touch
with the following method that I'd like to assert in a unittest using unitest.mock.patch
decorator:
sessions.py
def generate_folders(paths: list):
for p in paths:
p.mkdir()
Path(p / "output").mkdir()
Path(p / "output" / "output_file").touch()
Path(p / "session_file").touch()
test
@patch("os.mkdir")
@patch("pathlib.Path.touch")
def test_folder_creation(self, touch_mock, mkdir_mock):
tags = ["concepts", "models", "animations", "vfx", "sfx", "vo", "music"]
paths = sessions.generate_paths(category="character", name="player", tags=tags)
sessions.generate_folders(paths)
for p in paths:
mkdir_mock.assert_any_call(p, 511)
mkdir_mock.assert_any_call(p / "output", 511)
touch_mock.assert_any_call(Path(p / "output" / "output_file"), 511)
touch_mock.assert_any_call(Path(p / "session_file"), 511)
I can assert folder creation by os.mkdir
without any problem and I can also assert that pathlib.Path.touch
has been called. However, asserting specific calls with assert_any_call()
isn't possible. My test throws this output:
ssF...
======================================================================
FAIL: test_folder_creation (sessions.tests.sessions_tests.TestSessionsCreation.test_folder_creation)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\juank\AppData\Local\Programs\Python\Python313\Lib\unittest\mock.py", line 1424, in patched
return func(*newargs, **newkeywargs)
File "C:\Users\juank\dev\projects\python\gamedev_eco\sessions\tests\sessions_tests.py", line 51, in test_folder_creation
touch_mock.assert_any_call(Path(p / "output" / "output_file"), 511)
~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\juank\AppData\Local\Programs\Python\Python313\Lib\unittest\mock.py", line 1048, in assert_any_call
raise AssertionError(
'%s call not found' % expected_string
) from cause
AssertionError: touch(WindowsPath('C:/Users/juank/dev/projects/python/gamedev_eco/sessions/character/player/concepts/output/output_file'), 511) call not found
----------------------------------------------------------------------
Ran 6 tests in 0.004s
FAILED (failures=1, skipped=2)
What confuses me about this test result is that in spite of checking that method input values are the same with pdb
debugger, still the test fails. I also double-check that the decorators were working on os.mkdir
and pathlib.Path.touch
respectively.
On the other hand I tried patching touch
with with
but the test doesn't pass either. I can't tell if this issue is related to my code or to my test so I'd like to ask the community for hints please. What am I doing wrong? What am I missing?
1 Answer
Reset to default 0There's no way to call touch
like pathlib.Path.touch("my_path")
according to the documentation hence there's no way to assert it with touch_mock.assert_any_call(Path(p /"output"/"output_file"))
, it's just pointless. I'll only assert that the method has been called.