diff --git a/.travis.yml b/.travis.yml index ebb97f57..4550adb6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/README.md b/README.md index d1530217..c3318781 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/conftest.py b/conftest.py deleted file mode 100644 index d6a8b714..00000000 --- a/conftest.py +++ /dev/null @@ -1,15 +0,0 @@ -import py -import pytest -from pyfakefs.fake_filesystem_unittest import Patcher - - -Patcher.SKIPMODULES.add(py) # Ignore pytest components when faking filesystem - - -@pytest.yield_fixture -def fs(): - """ Fake filesystem. """ - patcher = Patcher() - patcher.setUp() - yield - patcher.tearDown() diff --git a/pyfakefs/pytest_plugin.py b/pyfakefs/pytest_plugin.py new file mode 100644 index 00000000..7c0b2cc1 --- /dev/null +++ b/pyfakefs/pytest_plugin.py @@ -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 diff --git a/pytest_plugin_test.py b/pytest_plugin_test.py new file mode 100644 index 00000000..649cbc6b --- /dev/null +++ b/pytest_plugin_test.py @@ -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') diff --git a/setup.py b/setup.py index b4c3f363..0aae7bf8 100644 --- a/setup.py +++ b/setup.py @@ -66,6 +66,9 @@ params = dict( name=NAME, + entry_points={ + 'pytest11': ['pytest_fakefs = pyfakefs.pytest_plugin'], + }, version=__version__, install_requires=REQUIRES, diff --git a/tox.ini b/tox.ini index 0da79b73..6878f08a 100644 --- a/tox.ini +++ b/tox.ini @@ -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