Git Product home page Git Product logo

Comments (2)

RKrahl avatar RKrahl commented on May 24, 2024

This is not supported out of the box. But you can implement it using a small helper function:

import pytest
from pytest_dependency import depends

def depends_or(request, other, scope='module'):
    """Add dependency on any of the other tests.

    Call pytest.skip() unless a successful outcome of any of the tests
    in `other` has been registered previously.  This helper is similar
    to `pytest_dependency.depends()`.  It takes the same arguments.
    But while `pytest_dependency.depends()` combines the tests in
    `other` in an and-like manner, it skips the current test unless
    all other tests did succeed, this function combines them in an
    or-like manner, it runs the current test if at least one of the
    other tests did succeed.
    """
    item = request.node
    for o in other:
        try:
            depends(request, [o], scope)
        except pytest.skip.Exception:
            continue
        else:
            return
    pytest.skip("%s depends on any of %s" % (item.name, ", ".join(other)))


@pytest.mark.dependency()
def test_ap():
    pass

@pytest.mark.dependency()
@pytest.mark.xfail(reason="deliberate fail")
def test_ax():
    assert False

@pytest.mark.dependency()
def test_bp():
    pass

@pytest.mark.dependency()
@pytest.mark.xfail(reason="deliberate fail")
def test_bx():
    assert False

@pytest.mark.dependency()
def test_c(request):
    depends_or(request, ["test_ax", "test_bx"])    
    pass

@pytest.mark.dependency()
def test_d(request):
    depends_or(request, ["test_ax", "test_bp"])    
    pass

@pytest.mark.dependency()
def test_e(request):
    depends_or(request, ["test_ap", "test_bx"])    
    pass

@pytest.mark.dependency()
def test_f(request):
    depends_or(request, ["test_ap", "test_bp"])    
    pass

Tests test_c, test_d, test_e, and test_f all depend on either test_a or test_b in the manner you described. I included succeeding and failing incarnations of test_a and test_b to demonstrate the effect. Only test_c will be skipped, because both tests in its dependency list fail, the other ones are run, because they have at least on succeeding test in their dependency list.

Warning: as usual, the documentation of internal data structures in pytest is scarce. It is not documented at all that skipping of tests internally work by raising a Skipped exception and this exception class is exposed in the pytest.skip.Exception attribute of pytest.skip(). As usual I needed to find out by reading the pytest source code. There is thus no guarantee that this is stable across pytest versions and I did not check whether it works with all versions supported by pytest-dependency.

from pytest-dependency.

rnetser avatar rnetser commented on May 24, 2024

Great, will try it out. Thank you

from pytest-dependency.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.