From 25547e3afbfa8be57ae6030eebc181a152d4f29c Mon Sep 17 00:00:00 2001 From: Andreas Zeidler Date: Wed, 30 Jan 2013 17:32:37 +0100 Subject: [PATCH 1/7] pass fixture request object (and convenience shortcut to get fixtures) into doctest files --HG-- branch : doctest-fixtures --- _pytest/doctest.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/_pytest/doctest.py b/_pytest/doctest.py index aaebab78e..e447b8faf 100644 --- a/_pytest/doctest.py +++ b/_pytest/doctest.py @@ -1,6 +1,7 @@ """ discover and run doctests in modules and test files.""" import pytest, py +from _pytest.python import FixtureRequest, FuncFixtureInfo from py._code.code import TerminalRepr, ReprFileLocation def pytest_addoption(parser): @@ -70,9 +71,15 @@ class DoctestItem(pytest.Item): class DoctestTextfile(DoctestItem, pytest.File): def runtest(self): doctest = py.std.doctest + # satisfy `FixtureRequest` constructor... + self.funcargs = {} + self._fixtureinfo = FuncFixtureInfo((), [], {}) + fixture_request = FixtureRequest(self) failed, tot = doctest.testfile( str(self.fspath), module_relative=False, optionflags=doctest.ELLIPSIS, + extraglobs=dict(fixture_request=fixture_request, + get_fixture=fixture_request.getfuncargvalue), raise_on_error=True, verbose=0) class DoctestModule(DoctestItem, pytest.File): From f747d363b0a090d428f77b3b39e6af7d0171d7cc Mon Sep 17 00:00:00 2001 From: Andreas Zeidler Date: Wed, 20 Mar 2013 16:36:48 +0100 Subject: [PATCH 2/7] don't expose the `FixtureRequest` object itself in doctests. in most cases `get_fixture` is sufficient, and you can always call `get_fixture('request')` anyway --HG-- branch : doctest-fixtures --- _pytest/doctest.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/_pytest/doctest.py b/_pytest/doctest.py index e447b8faf..87ecca7b3 100644 --- a/_pytest/doctest.py +++ b/_pytest/doctest.py @@ -78,8 +78,7 @@ class DoctestTextfile(DoctestItem, pytest.File): failed, tot = doctest.testfile( str(self.fspath), module_relative=False, optionflags=doctest.ELLIPSIS, - extraglobs=dict(fixture_request=fixture_request, - get_fixture=fixture_request.getfuncargvalue), + extraglobs=dict(get_fixture=fixture_request.getfuncargvalue), raise_on_error=True, verbose=0) class DoctestModule(DoctestItem, pytest.File): From c4b3a09886b146c8e33fec3871b0459e838a2421 Mon Sep 17 00:00:00 2001 From: Andreas Zeidler Date: Wed, 20 Mar 2013 17:14:28 +0100 Subject: [PATCH 3/7] test `get_fixture` helper for doctests --HG-- branch : doctest-fixtures --- testing/test_doctest.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/testing/test_doctest.py b/testing/test_doctest.py index 771cb52f3..a4532dbeb 100644 --- a/testing/test_doctest.py +++ b/testing/test_doctest.py @@ -124,3 +124,12 @@ class TestDoctests: " 1", "*test_txtfile_failing.txt:2: DocTestFailure" ]) + + def test_txtfile_with_fixtures(self, testdir, tmpdir): + p = testdir.maketxtfile(""" + >>> dir = get_fixture('tmpdir') + >>> type(dir).__name__ + 'LocalPath' + """) + reprec = testdir.inline_run(p, ) + reprec.assertoutcome(passed=1) From 5a3547dd7eebc91dd9b9200bff41f06a5a08aba6 Mon Sep 17 00:00:00 2001 From: Andreas Zeidler Date: Wed, 20 Mar 2013 17:32:48 +0100 Subject: [PATCH 4/7] also provide `get_fixture` helper for module level doctests --HG-- branch : doctest-fixtures --- _pytest/doctest.py | 5 +++++ testing/test_doctest.py | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/_pytest/doctest.py b/_pytest/doctest.py index 87ecca7b3..737ffe011 100644 --- a/_pytest/doctest.py +++ b/_pytest/doctest.py @@ -88,6 +88,11 @@ class DoctestModule(DoctestItem, pytest.File): module = self.config._conftest.importconftest(self.fspath) else: module = self.fspath.pyimport() + # satisfy `FixtureRequest` constructor... + self.funcargs = {} + self._fixtureinfo = FuncFixtureInfo((), [], {}) + fixture_request = FixtureRequest(self) failed, tot = doctest.testmod( module, raise_on_error=True, verbose=0, + extraglobs=dict(get_fixture=fixture_request.getfuncargvalue), optionflags=doctest.ELLIPSIS) diff --git a/testing/test_doctest.py b/testing/test_doctest.py index a4532dbeb..d880fdef2 100644 --- a/testing/test_doctest.py +++ b/testing/test_doctest.py @@ -133,3 +133,14 @@ class TestDoctests: """) reprec = testdir.inline_run(p, ) reprec.assertoutcome(passed=1) + + def test_doctestmodule_with_fixtures(self, testdir, tmpdir): + p = testdir.makepyfile(""" + ''' + >>> dir = get_fixture('tmpdir') + >>> type(dir).__name__ + 'LocalPath' + ''' + """) + reprec = testdir.inline_run(p, "--doctest-modules") + reprec.assertoutcome(passed=1) From fa9bd8443fbfe1046fd11b40cfad53d43038a226 Mon Sep 17 00:00:00 2001 From: Andreas Zeidler Date: Wed, 20 Mar 2013 17:54:38 +0100 Subject: [PATCH 5/7] update the documentation regarding the `get_fixture` helper please note that the japanese translation was done using "google translate" and should probably be checked again... :) --HG-- branch : doctest-fixtures --- doc/en/doctest.txt | 6 ++++++ doc/ja/doctest.txt | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/doc/en/doctest.txt b/doc/en/doctest.txt index 63fb32dc8..c27247ceb 100644 --- a/doc/en/doctest.txt +++ b/doc/en/doctest.txt @@ -50,3 +50,9 @@ then you can just invoke ``py.test`` without command line options:: mymodule.py . ========================= 1 passed in 0.02 seconds ========================= + +It is possible to use fixtures using the ``get_fixture`` helper:: + + # content of example.rst + >>> tmp = get_fixture('tmpdir') + >>> ... diff --git a/doc/ja/doctest.txt b/doc/ja/doctest.txt index 2765d7e7a..a1064a398 100644 --- a/doc/ja/doctest.txt +++ b/doc/ja/doctest.txt @@ -72,3 +72,12 @@ Python モジュール (通常 python テストモジュールを含む) の doc mymodule.py . ========================= 1 passed in 0.02 seconds ========================= + +.. + It is possible to use fixtures using the ``get_fixture`` helper:: + +それは ``get_fixture`` ヘルパーを使ってフィクスチャを使用することが可能である:: + + # content of example.rst + >>> tmp = get_fixture('tmpdir') + >>> ... From da3b42ce46389f9cbac81f3eabf3974e9ce56466 Mon Sep 17 00:00:00 2001 From: Andreas Zeidler Date: Thu, 21 Mar 2013 01:03:59 +0100 Subject: [PATCH 6/7] remove debugging left-overs --HG-- branch : doctest-fixtures --- testing/test_doctest.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/testing/test_doctest.py b/testing/test_doctest.py index d880fdef2..d95bd2fc9 100644 --- a/testing/test_doctest.py +++ b/testing/test_doctest.py @@ -125,7 +125,7 @@ class TestDoctests: "*test_txtfile_failing.txt:2: DocTestFailure" ]) - def test_txtfile_with_fixtures(self, testdir, tmpdir): + def test_txtfile_with_fixtures(self, testdir): p = testdir.maketxtfile(""" >>> dir = get_fixture('tmpdir') >>> type(dir).__name__ @@ -134,7 +134,7 @@ class TestDoctests: reprec = testdir.inline_run(p, ) reprec.assertoutcome(passed=1) - def test_doctestmodule_with_fixtures(self, testdir, tmpdir): + def test_doctestmodule_with_fixtures(self, testdir): p = testdir.makepyfile(""" ''' >>> dir = get_fixture('tmpdir') From dfcb0e322cc025ceb9c2d66c3ef4ebc997d12c15 Mon Sep 17 00:00:00 2001 From: Andreas Zeidler Date: Thu, 21 Mar 2013 12:04:14 +0100 Subject: [PATCH 7/7] rename `get_fixture` to `getfixture` to better match the current API style --HG-- branch : doctest-fixtures --- _pytest/doctest.py | 4 ++-- doc/en/doctest.txt | 4 ++-- doc/ja/doctest.txt | 6 +++--- testing/test_doctest.py | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/_pytest/doctest.py b/_pytest/doctest.py index 737ffe011..6753e5040 100644 --- a/_pytest/doctest.py +++ b/_pytest/doctest.py @@ -78,7 +78,7 @@ class DoctestTextfile(DoctestItem, pytest.File): failed, tot = doctest.testfile( str(self.fspath), module_relative=False, optionflags=doctest.ELLIPSIS, - extraglobs=dict(get_fixture=fixture_request.getfuncargvalue), + extraglobs=dict(getfixture=fixture_request.getfuncargvalue), raise_on_error=True, verbose=0) class DoctestModule(DoctestItem, pytest.File): @@ -94,5 +94,5 @@ class DoctestModule(DoctestItem, pytest.File): fixture_request = FixtureRequest(self) failed, tot = doctest.testmod( module, raise_on_error=True, verbose=0, - extraglobs=dict(get_fixture=fixture_request.getfuncargvalue), + extraglobs=dict(getfixture=fixture_request.getfuncargvalue), optionflags=doctest.ELLIPSIS) diff --git a/doc/en/doctest.txt b/doc/en/doctest.txt index c27247ceb..9f8b8a9db 100644 --- a/doc/en/doctest.txt +++ b/doc/en/doctest.txt @@ -51,8 +51,8 @@ then you can just invoke ``py.test`` without command line options:: ========================= 1 passed in 0.02 seconds ========================= -It is possible to use fixtures using the ``get_fixture`` helper:: +It is possible to use fixtures using the ``getfixture`` helper:: # content of example.rst - >>> tmp = get_fixture('tmpdir') + >>> tmp = getfixture('tmpdir') >>> ... diff --git a/doc/ja/doctest.txt b/doc/ja/doctest.txt index a1064a398..00c9001d2 100644 --- a/doc/ja/doctest.txt +++ b/doc/ja/doctest.txt @@ -74,10 +74,10 @@ Python モジュール (通常 python テストモジュールを含む) の doc ========================= 1 passed in 0.02 seconds ========================= .. - It is possible to use fixtures using the ``get_fixture`` helper:: + It is possible to use fixtures using the ``getfixture`` helper:: -それは ``get_fixture`` ヘルパーを使ってフィクスチャを使用することが可能である:: +それは ``getfixture`` ヘルパーを使ってフィクスチャを使用することが可能である:: # content of example.rst - >>> tmp = get_fixture('tmpdir') + >>> tmp = getfixture('tmpdir') >>> ... diff --git a/testing/test_doctest.py b/testing/test_doctest.py index d95bd2fc9..7b22e906c 100644 --- a/testing/test_doctest.py +++ b/testing/test_doctest.py @@ -127,7 +127,7 @@ class TestDoctests: def test_txtfile_with_fixtures(self, testdir): p = testdir.maketxtfile(""" - >>> dir = get_fixture('tmpdir') + >>> dir = getfixture('tmpdir') >>> type(dir).__name__ 'LocalPath' """) @@ -137,7 +137,7 @@ class TestDoctests: def test_doctestmodule_with_fixtures(self, testdir): p = testdir.makepyfile(""" ''' - >>> dir = get_fixture('tmpdir') + >>> dir = getfixture('tmpdir') >>> type(dir).__name__ 'LocalPath' '''