From 64cd1826d2ca3bac2bc2be28fb072dc75b744db8 Mon Sep 17 00:00:00 2001 From: Wes Kendall Date: Thu, 2 Feb 2017 13:16:12 -0800 Subject: [PATCH 1/8] added pytest module --- setup.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/setup.py b/setup.py index b4c3f363..8d82020c 100644 --- a/setup.py +++ b/setup.py @@ -66,6 +66,9 @@ params = dict( name=NAME, + entry_points={ + 'pytest11': ['pytest_fakefs = pyfakefs.pytest_module'], + }, version=__version__, install_requires=REQUIRES, From 0137d1b922bcaf60edd6528567879239fb13d278 Mon Sep 17 00:00:00 2001 From: Wes Kendall Date: Thu, 2 Feb 2017 13:16:54 -0800 Subject: [PATCH 2/8] added pytest module --- pyfakefs/pytest_module.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 pyfakefs/pytest_module.py diff --git a/pyfakefs/pytest_module.py b/pyfakefs/pytest_module.py new file mode 100644 index 00000000..0dedcfc2 --- /dev/null +++ b/pyfakefs/pytest_module.py @@ -0,0 +1,15 @@ +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.fs + patcher.tearDown() From 065d32d906110a3e078c728d05eb1d25512cad5b Mon Sep 17 00:00:00 2001 From: Wes Kendall Date: Thu, 2 Feb 2017 14:45:40 -0800 Subject: [PATCH 3/8] added a test for the pytest module and updated README.md --- .travis.yml | 5 ++++- README.md | 15 +++++++++++++++ conftest.py | 15 --------------- pyfakefs/pytest_module.py | 10 ++++++++++ tox.ini | 4 +++- 5 files changed, 32 insertions(+), 17 deletions(-) delete mode 100644 conftest.py 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_module.py b/pyfakefs/pytest_module.py index 0dedcfc2..29008c19 100644 --- a/pyfakefs/pytest_module.py +++ b/pyfakefs/pytest_module.py @@ -1,3 +1,13 @@ +"""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 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 From 0c179b4d55619eb5c8d89099f6c93f994da08997 Mon Sep 17 00:00:00 2001 From: Wes Kendall Date: Thu, 2 Feb 2017 14:48:18 -0800 Subject: [PATCH 4/8] added the pytest plugin test --- pytest_plugin_test.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 pytest_plugin_test.py 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') From 93dc12d6cd1f187a29d328d52ef424a1eace6c38 Mon Sep 17 00:00:00 2001 From: Wes Kendall Date: Thu, 2 Feb 2017 14:59:18 -0800 Subject: [PATCH 5/8] rename pytest module to pytest plugin --- pyfakefs/{pytest_module.py => pytest_plugin.py} | 0 pytest_plugin_test.py | 13 ++++++++++++- setup.py | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-) rename pyfakefs/{pytest_module.py => pytest_plugin.py} (100%) diff --git a/pyfakefs/pytest_module.py b/pyfakefs/pytest_plugin.py similarity index 100% rename from pyfakefs/pytest_module.py rename to pyfakefs/pytest_plugin.py diff --git a/pytest_plugin_test.py b/pytest_plugin_test.py index 649cbc6b..3d55afa0 100644 --- a/pytest_plugin_test.py +++ b/pytest_plugin_test.py @@ -2,6 +2,17 @@ import os -def test_fs_fixture(fs): +import pytest + + +@pytest.mark.parametrize('f', ['/var/data/xx1.txt', '/var/data/xx1.txt']) +def test_fs_fixture(fs, f): + assert not os.path.exists(f) + fs.CreateFile(f) + assert os.path.exists(f) + + +def test_fs_fixture2(fs): + assert not os.path.exists('/var/data/xx1.txt') fs.CreateFile('/var/data/xx1.txt') assert os.path.exists('/var/data/xx1.txt') diff --git a/setup.py b/setup.py index 8d82020c..0aae7bf8 100644 --- a/setup.py +++ b/setup.py @@ -67,7 +67,7 @@ params = dict( name=NAME, entry_points={ - 'pytest11': ['pytest_fakefs = pyfakefs.pytest_module'], + 'pytest11': ['pytest_fakefs = pyfakefs.pytest_plugin'], }, version=__version__, install_requires=REQUIRES, From 93436193eae7a75a2419649a1016f0eb258ec9e8 Mon Sep 17 00:00:00 2001 From: Wes Kendall Date: Thu, 2 Feb 2017 15:03:41 -0800 Subject: [PATCH 6/8] removed dummy test --- pytest_plugin_test.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/pytest_plugin_test.py b/pytest_plugin_test.py index 3d55afa0..7ff6f6cc 100644 --- a/pytest_plugin_test.py +++ b/pytest_plugin_test.py @@ -2,17 +2,7 @@ import os -import pytest - - -@pytest.mark.parametrize('f', ['/var/data/xx1.txt', '/var/data/xx1.txt']) def test_fs_fixture(fs, f): assert not os.path.exists(f) fs.CreateFile(f) assert os.path.exists(f) - - -def test_fs_fixture2(fs): - assert not os.path.exists('/var/data/xx1.txt') - fs.CreateFile('/var/data/xx1.txt') - assert os.path.exists('/var/data/xx1.txt') From 2675da493f3fe871d62afff97d0565cd49b03cfe Mon Sep 17 00:00:00 2001 From: Wes Kendall Date: Thu, 2 Feb 2017 15:06:57 -0800 Subject: [PATCH 7/8] fixed test --- pytest_plugin_test.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pytest_plugin_test.py b/pytest_plugin_test.py index 7ff6f6cc..649cbc6b 100644 --- a/pytest_plugin_test.py +++ b/pytest_plugin_test.py @@ -2,7 +2,6 @@ import os -def test_fs_fixture(fs, f): - assert not os.path.exists(f) - fs.CreateFile(f) - assert os.path.exists(f) +def test_fs_fixture(fs): + fs.CreateFile('/var/data/xx1.txt') + assert os.path.exists('/var/data/xx1.txt') From 4deb64411308d97eadba55c80b152d0cd32c1a59 Mon Sep 17 00:00:00 2001 From: Wes Kendall Date: Fri, 3 Feb 2017 10:34:44 -0800 Subject: [PATCH 8/8] use addFinalizer instead of yielding --- pyfakefs/pytest_plugin.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pyfakefs/pytest_plugin.py b/pyfakefs/pytest_plugin.py index 29008c19..7c0b2cc1 100644 --- a/pyfakefs/pytest_plugin.py +++ b/pyfakefs/pytest_plugin.py @@ -16,10 +16,10 @@ def my_fakefs_test(fs): Patcher.SKIPMODULES.add(py) # Ignore pytest components when faking filesystem -@pytest.yield_fixture -def fs(): +@pytest.fixture +def fs(request): """ Fake filesystem. """ patcher = Patcher() patcher.setUp() - yield patcher.fs - patcher.tearDown() + request.addfinalizer(patcher.tearDown) + return patcher.fs