Skip to content

Commit aa239a0

Browse files
Merge pull request #156 from wesleykendall/pytest-module
Added pytest plugin support
2 parents 8079a26 + 4deb644 commit aa239a0

File tree

7 files changed

+57
-17
lines changed

7 files changed

+57
-17
lines changed

.travis.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,8 @@ python:
2727
install:
2828
- if [[ $TRAVIS_PYTHON_VERSION == 2.6 ]]; then pip install importlib unittest2; fi
2929
- pip install -r requirements.txt
30+
- pip install .
3031

31-
script: ./all_tests.py
32+
script:
33+
- ./all_tests.py
34+
- py.test pytest_plugin_test.py

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ with patch('mymodule.glob', glob):
4343
print(glob.glob('/var/data/xx*'))
4444
```
4545

46+
### Usage as a Pytest Plugin
47+
48+
Installation of pyfakefs also provides a [PyTest](doc.pytest.org) plugin. The plugin makes the `fs`
49+
fixture available for any test. For example:
50+
51+
```
52+
def my_fakefs_test(fs):
53+
# "fs" is the reference to the fake file system
54+
fs.CreateFile('/var/data/xx1.txt')
55+
assert os.path.exists('/var/data/xx1.txt')
56+
```
57+
58+
Similar to the unittest class (`fake_filesystem_unittest.TestCase`), the `fs` fixture stubs
59+
out all file system functions and modules.
60+
4661
## Continuous Integration
4762

4863
pyfakefs is automatically tested with Python 2.6 and above, and it is currently

conftest.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

pyfakefs/pytest_plugin.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""A pytest plugin for using pyfakefs as a fixture
2+
3+
When pyfakefs is installed, the "fs" fixture becomes avaialable.
4+
5+
:Usage:
6+
7+
def my_fakefs_test(fs):
8+
fs.CreateFile('/var/data/xx1.txt')
9+
assert os.path.exists('/var/data/xx1.txt')
10+
"""
11+
import py
12+
import pytest
13+
from pyfakefs.fake_filesystem_unittest import Patcher
14+
15+
16+
Patcher.SKIPMODULES.add(py) # Ignore pytest components when faking filesystem
17+
18+
19+
@pytest.fixture
20+
def fs(request):
21+
""" Fake filesystem. """
22+
patcher = Patcher()
23+
patcher.setUp()
24+
request.addfinalizer(patcher.tearDown)
25+
return patcher.fs

pytest_plugin_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""Tests that the pytest plugin properly provides the "fs" fixture"""
2+
import os
3+
4+
5+
def test_fs_fixture(fs):
6+
fs.CreateFile('/var/data/xx1.txt')
7+
assert os.path.exists('/var/data/xx1.txt')

setup.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@
6666

6767
params = dict(
6868
name=NAME,
69+
entry_points={
70+
'pytest11': ['pytest_fakefs = pyfakefs.pytest_plugin'],
71+
},
6972
version=__version__,
7073
install_requires=REQUIRES,
7174

tox.ini

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
envlist=py26,py27,py32,py33,pypy
33

44
[testenv]
5-
commands=python all_tests.py
5+
commands=
6+
python all_tests.py
7+
py.test pytest_plugin_test.py
68

79
[testenv:py26]
810
deps=unittest2

0 commit comments

Comments
 (0)