| 1 |
|
| 2 |
.. _`accept example`: |
| 3 |
|
| 4 |
example: specifying and selecting acceptance tests |
| 5 |
-------------------------------------------------------------- |
| 6 |
|
| 7 |
.. sourcecode:: python |
| 8 |
|
| 9 |
# ./conftest.py |
| 10 |
def pytest_option(parser): |
| 11 |
group = parser.getgroup("myproject") |
| 12 |
group.addoption( |
| 13 |
"-A", dest="acceptance", action="store_true", help="run (slow) acceptance tests" |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
def pytest_funcarg__accept(request): |
| 18 |
return AcceptFixture(request) |
| 19 |
|
| 20 |
|
| 21 |
class AcceptFixture: |
| 22 |
def __init__(self, request): |
| 23 |
if not request.config.getoption("acceptance"): |
| 24 |
pytest.skip("specify -A to run acceptance tests") |
| 25 |
self.tmpdir = request.config.mktemp(request.function.__name__, numbered=True) |
| 26 |
|
| 27 |
def run(self, *cmd): |
| 28 |
"""called by test code to execute an acceptance test.""" |
| 29 |
self.tmpdir.chdir() |
| 30 |
return subprocess.check_output(cmd).decode() |
| 31 |
|
| 32 |
|
| 33 |
and the actual test function example: |
| 34 |
|
| 35 |
.. sourcecode:: python |
| 36 |
|
| 37 |
def test_some_acceptance_aspect(accept): |
| 38 |
accept.tmpdir.mkdir("somesub") |
| 39 |
result = accept.run("ls", "-la") |
| 40 |
assert "somesub" in result |
| 41 |
|
| 42 |
If you run this test without specifying a command line option |
| 43 |
the test will get skipped with an appropriate message. Otherwise |
| 44 |
you can start to add convenience and test support methods |
| 45 |
to your AcceptFixture and drive running of tools or |
| 46 |
applications and provide ways to do assertions about |
| 47 |
the output. |
| 48 |
|
| 49 |
.. _`decorate a funcarg`: |
| 50 |
|
| 51 |
example: decorating a funcarg in a test module |
| 52 |
-------------------------------------------------------------- |
| 53 |
|
| 54 |
For larger scale setups it's sometimes useful to decorate |
| 55 |
a funcarg just for a particular test module. We can |
| 56 |
extend the `accept example`_ by putting this in our test module: |
| 57 |
|
| 58 |
.. sourcecode:: python |
| 59 |
|
| 60 |
def pytest_funcarg__accept(request): |
| 61 |
# call the next factory (living in our conftest.py) |
| 62 |
arg = request.getfuncargvalue("accept") |
| 63 |
# create a special layout in our tempdir |
| 64 |
arg.tmpdir.mkdir("special") |
| 65 |
return arg |
| 66 |
|
| 67 |
|
| 68 |
class TestSpecialAcceptance: |
| 69 |
def test_sometest(self, accept): |
| 70 |
assert accept.tmpdir.join("special").check() |
| 71 |
|
| 72 |
Our module level factory will be invoked first and it can |
| 73 |
ask its request object to call the next factory and then |
| 74 |
decorate its result. This mechanism allows us to stay |
| 75 |
ignorant of how/where the function argument is provided - |
| 76 |
in our example from a `conftest plugin`_. |
| 77 |
|
| 78 |
Side note: the temporary directories used here are instances of |
| 79 |
the `py.path.local`_ class which provides many of the os.path |
| 80 |
methods in a convenient way. |
| 81 |
|
| 82 |
.. _`py.path.local`: ../path.html#local |
| 83 |
.. _`conftest plugin`: customize.html#conftestplugin |