Fixtures¶
Useful mocks for os, random and tempfile and time.
-
fixtureresources.fixtures.create_patch(mpatch, request)¶ Helper method for creating patch for the patch functions implementing start and stop.
Args:
mpatch: e.g. return value of mock.patch()
request: pytest request.
-
fixtureresources.fixtures.mock_gettempdir(request)¶ Pytest mock fixture for
tempfile.gettempdir(). The default return value is ‘tmp’.
-
fixtureresources.fixtures.mock_os_path_abspath(request)¶ Pytest mock fixture for
os.path.abspath(). The default return value joins ‘abspath’ with given path.For example:
def test_os_path_abspath(mock_os_path_abspath): assert os.path.abspath('path') == os.path.join('abspath', 'path')
-
fixtureresources.fixtures.mock_os_path_isfile(request)¶ Pytest mock fixture for
os.path.isfile(). The default return value is True.
-
fixtureresources.fixtures.mock_os_walk(request)¶ Pytest mock fixture for
os.walk(). The defaultos.walk()mock directory tree depth is 10 and all directories contains only ‘dir’ directory.For example:
def test_os_walk(mock_os_walk): for _ in range(10): for _, dirs, _ in os.walk('foo/bar'): assert dirs == ['dir']
-
fixtureresources.fixtures.mock_randomchoice(request)¶ Pytest mock fixture for
random.SystemRandom.choice(). Patchedrandom.SystemRandom.choice()returnschoices[call_number % number_of_choices]
For example:
def test_randon(mock_randomchoice): assert (''.join(random.SystemRandom().choice(['a', 'b', 'c']) for _ in range(5)) == 'abcab')
-
fixtureresources.fixtures.mock_time_sleep(request)¶ Pytest mock fixture for
time.sleep().
MockFile¶
-
class
fixtureresources.mockfile.MockFile(filename=None, content='', side_effect=None, args=None)¶ Simple string IO based mock for a file to be used via open.
Example usage:
>>> with MockFile(filename='foo.txt', content='bar') as mockfile: ... with open('foo.txt', 'a') as f: ... f.write('foo') ... with open('foo.txt') as f: ... print(f.read()) ... 3L barfoo >>> def raise_io_error(): ... raise IOError('message') ... >>> with MockFile(filename='filename', ... side_effect=lambda *args: raise_io_error()): ... open('filename') ... Traceback (most recent call last): File "<stdin>", line 2, in <module> File "mockfile.py", line 123, in __enter__ return self.mock_open_file(self.filename, self.args) File "mockfile.py", line 102, in mock_open_file self.side_effect(*args) File "<stdin>", line 2, in <lambda> File "<stdin>", line 2, in raise_io_error IOError: message >>>
Note
Tested only for one file at time and for text files.
-
set_content(content)¶ Set content
-
set_filename(filename)¶ Set filename
-
set_side_effect(side_effect)¶ Set side_effect
-
-
class
fixtureresources.mockfile.SavedIO(content='', saver=None, mode=None)¶