Questions tagged [pyfakefs]

pyfakefs implements a fake file system that mocks the Python file system modules. If using pyfakefs, the tests operate on a fake file system in memory without touching the real disk. pyfakefs directly supports unittest and pytest, and can be used with other testing frameworks.

Reference:

20 questions
11
votes
1 answer

How to replace file-access references for a module under test

pyfakefs sounds very useful: it "was developed initially as a modest fake implementation of core Python modules to support moderately complex file system interactions, and was introduced Google-wide . . . in September 2006. Since then, it has…
intuited
  • 23,174
  • 7
  • 66
  • 88
3
votes
0 answers

Mocking disk-out-of-space in python unittests

I'm trying to write a unittest to test the behaviour of a function when the disk is full. I need file access functions to behave normally while most of the test runs, so that the file I'm creating is actually created, then at one point I need the…
3
votes
1 answer

Programmatically execute a Python file from within Python in a fresh-looking Python environment

Let's say I have a file script.py located at path = "foo/bar/script.py". I'm looking for a way in Python to programmatically execute script.py from within my main Python program through a function execute_script(). However, I've got a few…
balu
  • 3,500
  • 4
  • 34
  • 35
2
votes
1 answer

importlib_resources backport not compatible with pyfakefs?

I'm refactoring a library to use importlib.resources for python 3.7+. I'm using the importlib_resources backport for python 3.6 compatibility. The code works for pythons 3.6-3.8. However, the pytest tests, using pyfakefs, fail for 3.6. Under testing…
Geoffrey Sametz
  • 618
  • 1
  • 6
  • 17
2
votes
1 answer

Is it possible to mock the platform for reading and writing files in binary and text mode?

I would to test if a system reads and writes files correctly (text mode / binary mode) on multiple platforms, at least on linux and windows. (Using pytest). See https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files It is…
sunew
  • 477
  • 5
  • 11
1
vote
0 answers

Pyfakefs not supporting os.renames?

Here's a simple piece of code I wanted to test: import os def move_all(firstPath, secondPath): for count, filename in enumerate(os.listdir(firstPath)): print('moving '+str(count) + ' '+filename) …
Honkou
  • 11
  • 2
1
vote
1 answer

Why FileNotFoundError on Path.rename while using Pyfakefs?

I wrote a test for a function that renames files from e.g. /videos/vid_youtube.mp4 to /videos/youtube/vid.mp4. The test patches the fs with Pyfakefs. When the code actually renames the file, I get this error. FileNotFoundError: [Errno 2] No such…
progonkpa
  • 3,590
  • 9
  • 31
  • 50
1
vote
1 answer

Patching over local JSON file in unit testing

I have some Python code that loads in a local JSON file: with open("/path/to/file.json") as f: json_str = f.read() # Now do stuff with this JSON string In testing, I want to patch that JSON file to be a JSON file located in my repo's test…
1
vote
1 answer

Keep pathlib.(Posix)Path patched by pyfakefs when subclassing it

I have subclassed PosixPath this way: from pathlib import PosixPath class Node(PosixPath): def __new__(cls, *args, mykwarg=None, **kwargs): self = super().__new__(cls, *args, **kwargs) self._mykwarg = mykwarg return…
zezollo
  • 4,606
  • 5
  • 28
  • 59
1
vote
2 answers

Mocking class methods, still running the original method with some wrapped code

I want to patch a method by running the original method with additional code before and after. In particular, I'm running tests within a pyfakefs in-memory file system, but I want to sometime use the real file system because some packages will not…
kdauria
  • 6,300
  • 4
  • 34
  • 53
1
vote
1 answer

How to use pyfakefs in conjunction with importlib.resources.path

Given some installed package, the following code can be used to print its location on the filesystem: import importlib.resources def print_package_path(pkg): with importlib.resources.path(pkg, "") as path: …
Arne
  • 17,706
  • 5
  • 83
  • 99
1
vote
2 answers

How to test aiofiles with pyfakefs in a synchronous test class

Consider the function in a module. async def _get_user_mapping(): async with aiofiles.open('/etc/subuid') as f: async for line in f: print(line) And a test class from atestfile import _get_user_mapping class…
1
vote
1 answer

Real file system accessed when running lxml with pyfakefs

How can I run lxml with pyfakefs? import os import unittest from lxml import etree from pyfakefs import fake_filesystem_unittest class TestExample(fake_filesystem_unittest.TestCase): def setUp(self): self.setUpPyfakefs() def…
Oskar Persson
  • 6,605
  • 15
  • 63
  • 124
0
votes
0 answers

pyfakefs breaks tests using requests

I have a Pytest test using the pyfakefs fs fixture. def test_my_unit_test(fs): simple_test() It works as expected and simple tests run with the fake filesystem provided by pyfakefs. If I run another test that uses requests, I get this error…
Nicolas Berthier
  • 459
  • 1
  • 8
  • 17
0
votes
1 answer

Pytestfs write then read doesn't return expected value

I'm trying to write a test involving the filesystem. I chose to use pyfakefs and pytest for writing these tests. When I was trying to write and then read from the fake filesystem, I couldn't seem to get any tests to work. So, I wrote a simple test…
Julia Norman
  • 193
  • 1
  • 11
1
2