parent
46ee6beaa9
commit
1dc9ac62ca
|
@ -5,11 +5,11 @@
|
||||||
In version 1.0 py.test introduces a new mechanism for setting up test
|
In version 1.0 py.test introduces a new mechanism for setting up test
|
||||||
state for use by Python test functions. It is particularly useful
|
state for use by Python test functions. It is particularly useful
|
||||||
for functional and integration testing but also for unit testing.
|
for functional and integration testing but also for unit testing.
|
||||||
Using funcargs you can:
|
Using funcargs you can easily:
|
||||||
|
|
||||||
* write self-contained, simple to read and debug test functions
|
* write self-contained, simple to read and debug test functions
|
||||||
* cleanly encapsulate glue code between your app and your tests
|
* cleanly encapsulate glue code between your app and your tests
|
||||||
* do test scenario setup dependent on command line opts or environment
|
* setup test state depending on command line options or environment
|
||||||
|
|
||||||
Using the funcargs mechanism will increase readability
|
Using the funcargs mechanism will increase readability
|
||||||
and allow for easier refactoring of your application
|
and allow for easier refactoring of your application
|
||||||
|
@ -18,15 +18,15 @@ and its test suites.
|
||||||
The basic funcarg request/provide mechanism
|
The basic funcarg request/provide mechanism
|
||||||
=============================================
|
=============================================
|
||||||
|
|
||||||
All you need to do from a test function or test method
|
To use funcargs you only need to specify
|
||||||
is to specify an argument for your test function:
|
a named argument for your test function:
|
||||||
|
|
||||||
.. sourcecode:: python
|
.. sourcecode:: python
|
||||||
|
|
||||||
def test_function(myarg):
|
def test_function(myarg):
|
||||||
# use myarg
|
# use myarg
|
||||||
|
|
||||||
For each test function that requests the ``myarg``
|
For each test function that requests this ``myarg``
|
||||||
argument a matching so called funcarg provider
|
argument a matching so called funcarg provider
|
||||||
will be invoked. A Funcarg provider for ``myarg``
|
will be invoked. A Funcarg provider for ``myarg``
|
||||||
is written down liks this:
|
is written down liks this:
|
||||||
|
@ -40,11 +40,10 @@ Such a provider method can live on a test class,
|
||||||
test module or on a local or global plugin.
|
test module or on a local or global plugin.
|
||||||
The method is recognized by the ``pytest_funcarg__``
|
The method is recognized by the ``pytest_funcarg__``
|
||||||
prefix and is correlated to the argument
|
prefix and is correlated to the argument
|
||||||
name which follows this prefix. Because it
|
name which follows this prefix. The passed in
|
||||||
has access to the "request" object a provider
|
"request" object allows to interact
|
||||||
method is a uniquely powerful place for
|
with test configuration, test collection
|
||||||
containing setup up of test scenarios and
|
and test running aspects.
|
||||||
test configuration.
|
|
||||||
|
|
||||||
.. _`request object`:
|
.. _`request object`:
|
||||||
|
|
||||||
|
@ -55,11 +54,13 @@ Request objects give access to command line options,
|
||||||
the underlying python function and the test running
|
the underlying python function and the test running
|
||||||
process. Each funcarg provider method receives a ``request`` object
|
process. Each funcarg provider method receives a ``request`` object
|
||||||
that allows interaction with the test method and test
|
that allows interaction with the test method and test
|
||||||
running process. Basic attributes::
|
running process. Basic attributes of request objects:
|
||||||
|
|
||||||
argname: requested argument name
|
``request.argname``: name of the request argument
|
||||||
function: python function object requesting the argument
|
|
||||||
config: access to command line opts and general config
|
``request.function``: python function object requesting the argument
|
||||||
|
|
||||||
|
``request.config``: access to command line opts and general config
|
||||||
|
|
||||||
Request objects have a ``addfinalizer`` method that
|
Request objects have a ``addfinalizer`` method that
|
||||||
allows to **register a finalizer method** which is
|
allows to **register a finalizer method** which is
|
||||||
|
@ -105,6 +106,15 @@ lookup order for provider methods:
|
||||||
4. global plugins
|
4. global plugins
|
||||||
|
|
||||||
|
|
||||||
|
Using multiple funcargs
|
||||||
|
----------------------------------------
|
||||||
|
|
||||||
|
A test function may receive more than one
|
||||||
|
function arguments. For each of the
|
||||||
|
function arguments a lookup of a
|
||||||
|
matching provider will be performed.
|
||||||
|
|
||||||
|
|
||||||
Funcarg Examples
|
Funcarg Examples
|
||||||
=====================
|
=====================
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue