I am writing a unit test for embedded software code in python.
One of the files require a specific file to exist. (e.g. "/dir_name/file_name.txt") otherwise it raises an error.
Normally, this file exists on hardware device and my python code reads this file. When writing unit tests for python code, how can I mock the existence of the file?
tempfile.mkstemp()
does not seem to generate the exact path/file name I want, which is/dir_name/file_name.txt
. It always adds some random letters.
This is with Python3.4. Is it possible to accomplish this with unittest.mock
?
import module_A
. When I am doing thisimport
, it requires the file (or we need to make it believe that the file exists) and that's bit tricky...import module_A
is where it raises an exception if the file does not exist. – Somersomersmodule_A
and the creation of that object requires the file existence (or we need to fool it to make it believe that the file exists) – Somersomerspatch
that global object? I don't really need that global object for the unit test. The global object is for the HW that the python runs on. – Somersomers