fix/update some docs to work with @pytest.factory instead of pytest_funcarg__ naming.
This commit is contained in:
parent
f4963270c6
commit
f472f21406
|
@ -1,10 +1,11 @@
|
||||||
|
|
||||||
def pytest_funcarg__setup(request):
|
import pytest
|
||||||
return request.cached_setup(
|
|
||||||
setup=lambda: CostlySetup(),
|
@pytest.factory("session")
|
||||||
teardown=lambda costlysetup: costlysetup.finalize(),
|
def setup(testcontext):
|
||||||
scope="session",
|
setup = CostlySetup()
|
||||||
)
|
testcontext.addfinalizer(setup.finalize)
|
||||||
|
return setup
|
||||||
|
|
||||||
class CostlySetup:
|
class CostlySetup:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|
|
@ -5,29 +5,14 @@ serialization via the pickle module.
|
||||||
import py, pytest
|
import py, pytest
|
||||||
|
|
||||||
pythonlist = ['python2.4', 'python2.5', 'python2.6', 'python2.7', 'python2.8']
|
pythonlist = ['python2.4', 'python2.5', 'python2.6', 'python2.7', 'python2.8']
|
||||||
|
@pytest.factory(params=pythonlist)
|
||||||
def pytest_generate_tests(metafunc):
|
def python1(testcontext, tmpdir):
|
||||||
# we parametrize all "python1" and "python2" arguments to iterate
|
|
||||||
# over the python interpreters of our list above - the actual
|
|
||||||
# setup and lookup of interpreters in the python1/python2 factories
|
|
||||||
# respectively.
|
|
||||||
for arg in metafunc.funcargnames:
|
|
||||||
if arg in ("python1", "python2"):
|
|
||||||
metafunc.parametrize(arg, pythonlist, indirect=True)
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("obj", [42, {}, {1:3},])
|
|
||||||
def test_basic_objects(python1, python2, obj):
|
|
||||||
python1.dumps(obj)
|
|
||||||
python2.load_and_is_true("obj == %s" % obj)
|
|
||||||
|
|
||||||
def pytest_funcarg__python1(request):
|
|
||||||
tmpdir = request.getfuncargvalue("tmpdir")
|
|
||||||
picklefile = tmpdir.join("data.pickle")
|
picklefile = tmpdir.join("data.pickle")
|
||||||
return Python(request.param, picklefile)
|
return Python(testcontext.param, picklefile)
|
||||||
|
|
||||||
def pytest_funcarg__python2(request):
|
@pytest.factory(params=pythonlist)
|
||||||
python1 = request.getfuncargvalue("python1")
|
def python2(testcontext, python1):
|
||||||
return Python(request.param, python1.picklefile)
|
return Python(testcontext.param, python1.picklefile)
|
||||||
|
|
||||||
class Python:
|
class Python:
|
||||||
def __init__(self, version, picklefile):
|
def __init__(self, version, picklefile):
|
||||||
|
@ -58,3 +43,8 @@ class Python:
|
||||||
""" % (str(self.picklefile), expression)))
|
""" % (str(self.picklefile), expression)))
|
||||||
print (loadfile)
|
print (loadfile)
|
||||||
py.process.cmdexec("%s %s" %(self.pythonpath, loadfile))
|
py.process.cmdexec("%s %s" %(self.pythonpath, loadfile))
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("obj", [42, {}, {1:3},])
|
||||||
|
def test_basic_objects(python1, python2, obj):
|
||||||
|
python1.dumps(obj)
|
||||||
|
python2.load_and_is_true("obj == %s" % obj)
|
||||||
|
|
|
@ -123,6 +123,11 @@ it is nice to be able to search for ``pytest_funcarg__MYARG`` in
|
||||||
source code and safely find all factory functions for
|
source code and safely find all factory functions for
|
||||||
the ``MYARG`` function argument.
|
the ``MYARG`` function argument.
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
|
||||||
|
With pytest-2.3 you can use the :ref:`@pytest.factory` decorator
|
||||||
|
to mark a function as a funcarg factory.
|
||||||
|
|
||||||
.. _`Convention over Configuration`: http://en.wikipedia.org/wiki/Convention_over_Configuration
|
.. _`Convention over Configuration`: http://en.wikipedia.org/wiki/Convention_over_Configuration
|
||||||
|
|
||||||
Can I yield multiple values from a funcarg factory function?
|
Can I yield multiple values from a funcarg factory function?
|
||||||
|
@ -141,8 +146,12 @@ is not possible:
|
||||||
policy - in real-world examples some combinations
|
policy - in real-world examples some combinations
|
||||||
often should not run.
|
often should not run.
|
||||||
|
|
||||||
Use the `pytest_generate_tests`_ hook to solve both issues
|
However, with pytest-2.3 you can use the :ref:`@pytest.factory` decorator
|
||||||
and implement the `parametrization scheme of your choice`_.
|
and specify ``params`` so that all tests depending on the factory-created
|
||||||
|
resource will run multiple times with different parameters.
|
||||||
|
|
||||||
|
You can also use the `pytest_generate_tests`_ hook to
|
||||||
|
implement the `parametrization scheme of your choice`_.
|
||||||
|
|
||||||
.. _`pytest_generate_tests`: test/funcargs.html#parametrizing-tests
|
.. _`pytest_generate_tests`: test/funcargs.html#parametrizing-tests
|
||||||
.. _`parametrization scheme of your choice`: http://tetamap.wordpress.com/2009/05/13/parametrizing-python-tests-generalized/
|
.. _`parametrization scheme of your choice`: http://tetamap.wordpress.com/2009/05/13/parametrizing-python-tests-generalized/
|
||||||
|
|
|
@ -88,7 +88,7 @@ parametrization, i.e. calling a test multiple times with different value
|
||||||
sets. pytest-2.3 introduces a decorator for use on the factory itself::
|
sets. pytest-2.3 introduces a decorator for use on the factory itself::
|
||||||
|
|
||||||
@pytest.factory(params=["mysql", "pg"])
|
@pytest.factory(params=["mysql", "pg"])
|
||||||
def pytest_funcarg__db(testcontext):
|
def db(testcontext):
|
||||||
... # use testcontext.param
|
... # use testcontext.param
|
||||||
|
|
||||||
Here the factory will be invoked twice (with the respective "mysql"
|
Here the factory will be invoked twice (with the respective "mysql"
|
||||||
|
@ -105,7 +105,7 @@ functions/classes were parametrized via
|
||||||
Of course it's perfectly fine to combine parametrization and scoping::
|
Of course it's perfectly fine to combine parametrization and scoping::
|
||||||
|
|
||||||
@pytest.factory(scope="session", params=["mysql", "pg"])
|
@pytest.factory(scope="session", params=["mysql", "pg"])
|
||||||
def pytest_funcarg__db(testcontext):
|
def db(testcontext):
|
||||||
if testcontext.param == "mysql":
|
if testcontext.param == "mysql":
|
||||||
db = MySQL()
|
db = MySQL()
|
||||||
elif testcontext.param == "pg":
|
elif testcontext.param == "pg":
|
||||||
|
@ -137,7 +137,9 @@ aka::
|
||||||
def pytest_funcarg__db(request):
|
def pytest_funcarg__db(request):
|
||||||
...
|
...
|
||||||
|
|
||||||
It is recommended to use the resource decorator, however.
|
|
||||||
|
But it is then not possible to define scoping and parametrization.
|
||||||
|
It is thus recommended to use the factory decorator.
|
||||||
|
|
||||||
|
|
||||||
solving per-session setup / the new @setup marker
|
solving per-session setup / the new @setup marker
|
||||||
|
|
Loading…
Reference in New Issue