Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@ python:
install:
- if [[ $TRAVIS_PYTHON_VERSION == 2.6 ]]; then pip install importlib unittest2; fi
- pip install -r requirements.txt
- pip install .

script: ./all_tests.py
script:
- ./all_tests.py
- py.test pytest_plugin_test.py
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ with patch('mymodule.glob', glob):
print(glob.glob('/var/data/xx*'))
```

### Usage as a Pytest Plugin

Installation of pyfakefs also provides a [PyTest](doc.pytest.org) plugin. The plugin makes the `fs`
fixture available for any test. For example:

```
def my_fakefs_test(fs):
# "fs" is the reference to the fake file system
fs.CreateFile('/var/data/xx1.txt')
assert os.path.exists('/var/data/xx1.txt')
```

Similar to the unittest class (`fake_filesystem_unittest.TestCase`), the `fs` fixture stubs
out all file system functions and modules.

## Continuous Integration

pyfakefs is automatically tested with Python 2.6 and above, and it is currently
Expand Down
15 changes: 0 additions & 15 deletions conftest.py

This file was deleted.

25 changes: 25 additions & 0 deletions pyfakefs/pytest_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""A pytest plugin for using pyfakefs as a fixture

When pyfakefs is installed, the "fs" fixture becomes avaialable.

:Usage:

def my_fakefs_test(fs):
fs.CreateFile('/var/data/xx1.txt')
assert os.path.exists('/var/data/xx1.txt')
"""
import py
import pytest
from pyfakefs.fake_filesystem_unittest import Patcher


Patcher.SKIPMODULES.add(py) # Ignore pytest components when faking filesystem


@pytest.fixture
def fs(request):
""" Fake filesystem. """
patcher = Patcher()
patcher.setUp()
request.addfinalizer(patcher.tearDown)
return patcher.fs
7 changes: 7 additions & 0 deletions pytest_plugin_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""Tests that the pytest plugin properly provides the "fs" fixture"""
import os


def test_fs_fixture(fs):
fs.CreateFile('/var/data/xx1.txt')
assert os.path.exists('/var/data/xx1.txt')
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@

params = dict(
name=NAME,
entry_points={
'pytest11': ['pytest_fakefs = pyfakefs.pytest_plugin'],
},
version=__version__,
install_requires=REQUIRES,

Expand Down
4 changes: 3 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
envlist=py26,py27,py32,py33,pypy

[testenv]
commands=python all_tests.py
commands=
python all_tests.py
py.test pytest_plugin_test.py

[testenv:py26]
deps=unittest2