Pytest: Dependency between tests

Created on 17 Mar 2017  Â·  10Comments  Â·  Source: pytest-dev/pytest

Is there any way currently to declare some dependency between tests?

In an ideal world, I'd love something like this:

import pytest

def test_foo():
    assert False

def test_bar():
    assert True

@pytest.depends_on(test_foo, test_bar)
def test_baz():
    pass

Where test_baz would fail because at least one of test it depends on failed (test_foo).

If that doesn't exist yet, would you think it could be a pytest feature? Or a good purpose for a plugin?

Most helpful comment

There is a package pytest-dependency that does exactly what you/we want.

All 10 comments

currently its not supported, but its a mechanism thats often needed for system level testing in order to safe time

its really hard to do a feature like this properly atm, because we would need to integrate with parameterization and test items correctly - internal refactoring are needed to give this a solid base

as for expressing it, my proposal for the initial prototype would be

import pytest

def test_foo():
    assert False

def test_bar():
    assert True

@pytest.mark.test_depends_on(test_foo, test_bar)
def test_baz():
    pass

Has there been any movement or work put into making this feature a reality? It'd be extremely helpful for integration/system level testing like you commented before.

@BlacKCaT27 I don't think anybody is working on this.

A workaround I suggest is to name the test functions differently and call them from a proper test. For example:

import pytest

def check_foo():
    assert False

def check_bar():
    assert True

def test_baz():
    check_foo()
    check_bar()

Thanks for the rapid reply. That's a great workaround I'll be sure to use.

Also, could you confirm something for me? I've noticed (and seen others online mention) that pytest appears to run all tests in a module from top to bottom by default. Is that guaranteed behavior or just a happenstance of how pytest currently discovers tests from a module?

py.test tries to order based on source location ,but will reshuffle based on fixture scope to avoid unnecessary setup/teardown,

Ah, clever! Good to know. Thanks!

On Mon, Jul 24, 2017 at 10:27 AM, Ronny Pfannschmidt <
[email protected]> wrote:

py.test tries to order based on source location ,but will reshuffle based
on fixture scope to avoid unnecessary setup/teardown,

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/pytest-dev/pytest/issues/2317#issuecomment-317440076,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AE_54UtgDQrK8ycGkuw_k1x9Os5VzhgJks5sRKnvgaJpZM4MgXUo
.

FWIW there's also the pytest-ordering plugin.

There is a package pytest-dependency that does exactly what you/we want.

Should we close this? So far we have one workaround and two plugins; IMHO we don't need to integrate this into the core.

Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SwampFalc picture SwampFalc  Â·  3Comments

Caipers picture Caipers  Â·  3Comments

nicoddemus picture nicoddemus  Â·  3Comments

RonnyPfannschmidt picture RonnyPfannschmidt  Â·  3Comments

dmtucker picture dmtucker  Â·  3Comments