Skip to content

Commit 1498560

Browse files
committed
Add dose 12
1 parent 43485b0 commit 1498560

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

code/12/pyfakefs_example.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import pathlib
2+
3+
from pyfakefs.fake_filesystem import FakeFilesystem
4+
5+
6+
def my_functionality() -> None:
7+
with open("foo.txt", "w") as f:
8+
f.write("foo bar baz")
9+
10+
11+
# fs fixture provides a fake filesystem automatically
12+
def test_my_functionality(fs: FakeFilesystem) -> None:
13+
path = pathlib.Path("foo.txt")
14+
assert not path.exists()
15+
16+
my_functionality()
17+
18+
assert path.exists()
19+
with open(path) as f:
20+
content = f.read()
21+
assert content == "foo bar baz"

docs/doses/12.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: 12 - pyfakefs
3+
tags:
4+
- Interesting projects
5+
---
6+
# 12 - pyfakefs
7+
8+
`pyfakefs` implements a fake filesystem which mocks the Python file system modules. Super handy for mocking file I/O in tests and comes with a built-in pytest plugin.
9+
10+
![TODO](../img/12.png)
11+
12+
??? info "Read more"
13+
* pyfakefs GitHub: [https://github.com/jmcgeheeiv/pyfakefs/](https://github.com/jmcgeheeiv/pyfakefs/)
14+
* pyfakefs docs: [http://jmcgeheeiv.github.io/pyfakefs/release/index.html](http://jmcgeheeiv.github.io/pyfakefs/release/index.html)
15+
16+
??? tip "The code"
17+
```python
18+
--8<-- "code/12/pyfakefs_example.py"
19+
```

docs/img/12.png

188 KB
Loading

mkdocs.yml

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ nav:
3939
- doses/9.md
4040
- doses/10.md
4141
- doses/11.md
42+
- doses/12.md
4243

4344
markdown_extensions:
4445
- attr_list:

0 commit comments

Comments
 (0)