fakefs | Use it in your tests | Unit Testing library
kandi X-RAY | fakefs Summary
kandi X-RAY | fakefs Summary
A fake filesystem. Use it in your tests.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Appends a path to the absolute path .
- Converts a regexp pattern into a regexp .
- Returns the full path from a directory
- Expands the given string into a string
- Search for a directory recursively
- Move a file or directory to the destination directory .
- Copies the file in the destination directory .
- Copy the source files to another file .
- Returns a new path for the given path .
- Recursively creates all possible directories .
fakefs Key Features
fakefs Examples and Code Snippets
Community Discussions
Trending Discussions on fakefs
QUESTION
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.
...ANSWER
Answered 2022-Apr-02 at 14:13The problem here is most likely the static initialization of Dirs.VIDEOS
. This is initialized at load time as a pathlib.Path
, and won't be patched later at the time you setup pyfakefs
(the same problem would happen if you where to use unittest.patch
for patching).
There are two ways to fix this:
- adapt the code to not initialize the path statically
This could be done by statically defining thestr
path, and converting it to aPath
at run time, or by using a method to get the path instead of an attribute (e.g.Dirs.VIDEO()
instead of Dirs.VIDEO`). - adapt the test to reload the tested code
If reloading the tested code afterpyfakefs
has been initialized, it will be correctly patched.pyfakefs
provides an argument insetUpPyfakefs
that does that:
QUESTION
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 conditions, the path returned from use of importlib_resources is mangled (but under "real-world" conditions they return correctly).
A minimal example: I have the following library structure:
...ANSWER
Answered 2020-Feb-05 at 19:28The fake filesystem in pyfakefs is empty at test start (except for the temp file path needed by the tempfile
module), so if you want to access any files in the real filesystem in your test, you have to map them into the fake filesystem:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install fakefs
On a UNIX-like operating system, using your system’s package manager is easiest. However, the packaged Ruby version may not be the newest one. There is also an installer for Windows. Managers help you to switch between multiple Ruby versions on your system. Installers can be used to install a specific or multiple Ruby versions. Please refer ruby-lang.org for more information.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page