2010-11-18 22:04:50 +08:00
|
|
|
|
|
|
|
.. _`accept example`:
|
|
|
|
|
|
|
|
example: specifying and selecting acceptance tests
|
|
|
|
--------------------------------------------------------------
|
|
|
|
|
|
|
|
.. sourcecode:: python
|
|
|
|
|
|
|
|
# ./conftest.py
|
|
|
|
def pytest_option(parser):
|
|
|
|
group = parser.getgroup("myproject")
|
2018-06-03 11:29:28 +08:00
|
|
|
group.addoption(
|
|
|
|
"-A", dest="acceptance", action="store_true", help="run (slow) acceptance tests"
|
|
|
|
)
|
|
|
|
|
2010-11-18 22:04:50 +08:00
|
|
|
|
|
|
|
def pytest_funcarg__accept(request):
|
2012-10-05 20:24:44 +08:00
|
|
|
return AcceptFixture(request)
|
2010-11-18 22:04:50 +08:00
|
|
|
|
2018-06-03 11:29:28 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class AcceptFixture(object):
|
2010-11-18 22:04:50 +08:00
|
|
|
def __init__(self, request):
|
2018-06-03 11:29:28 +08:00
|
|
|
if not request.config.getoption("acceptance"):
|
2010-11-18 22:04:50 +08:00
|
|
|
pytest.skip("specify -A to run acceptance tests")
|
|
|
|
self.tmpdir = request.config.mktemp(request.function.__name__, numbered=True)
|
|
|
|
|
|
|
|
def run(self, cmd):
|
|
|
|
""" called by test code to execute an acceptance test. """
|
|
|
|
self.tmpdir.chdir()
|
|
|
|
return py.process.cmdexec(cmd)
|
|
|
|
|
|
|
|
|
|
|
|
and the actual test function example:
|
|
|
|
|
|
|
|
.. sourcecode:: python
|
|
|
|
|
|
|
|
def test_some_acceptance_aspect(accept):
|
|
|
|
accept.tmpdir.mkdir("somesub")
|
|
|
|
result = accept.run("ls -la")
|
|
|
|
assert "somesub" in result
|
|
|
|
|
|
|
|
If you run this test without specifying a command line option
|
|
|
|
the test will get skipped with an appropriate message. Otherwise
|
|
|
|
you can start to add convenience and test support methods
|
2012-10-05 20:24:44 +08:00
|
|
|
to your AcceptFixture and drive running of tools or
|
2010-11-18 22:04:50 +08:00
|
|
|
applications and provide ways to do assertions about
|
|
|
|
the output.
|
|
|
|
|
|
|
|
.. _`decorate a funcarg`:
|
|
|
|
|
|
|
|
example: decorating a funcarg in a test module
|
|
|
|
--------------------------------------------------------------
|
|
|
|
|
2011-02-17 21:46:40 +08:00
|
|
|
For larger scale setups it's sometimes useful to decorate
|
2010-11-18 22:04:50 +08:00
|
|
|
a funcarg just for a particular test module. We can
|
|
|
|
extend the `accept example`_ by putting this in our test module:
|
|
|
|
|
|
|
|
.. sourcecode:: python
|
|
|
|
|
|
|
|
def pytest_funcarg__accept(request):
|
|
|
|
# call the next factory (living in our conftest.py)
|
|
|
|
arg = request.getfuncargvalue("accept")
|
|
|
|
# create a special layout in our tempdir
|
|
|
|
arg.tmpdir.mkdir("special")
|
|
|
|
return arg
|
|
|
|
|
2018-06-03 11:29:28 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestSpecialAcceptance(object):
|
2010-11-18 22:04:50 +08:00
|
|
|
def test_sometest(self, accept):
|
|
|
|
assert accept.tmpdir.join("special").check()
|
|
|
|
|
|
|
|
Our module level factory will be invoked first and it can
|
|
|
|
ask its request object to call the next factory and then
|
|
|
|
decorate its result. This mechanism allows us to stay
|
|
|
|
ignorant of how/where the function argument is provided -
|
|
|
|
in our example from a `conftest plugin`_.
|
|
|
|
|
|
|
|
sidenote: the temporary directory used here are instances of
|
|
|
|
the `py.path.local`_ class which provides many of the os.path
|
|
|
|
methods in a convenient way.
|
|
|
|
|
|
|
|
.. _`py.path.local`: ../path.html#local
|
|
|
|
.. _`conftest plugin`: customize.html#conftestplugin
|