pytest-dependency icon indicating copy to clipboard operation
pytest-dependency copied to clipboard

Remove parametrization when storing dependency results

Open DevilXD opened this issue 4 years ago • 3 comments

Utilizing the same approach as in #33, but up to date and more efficient. This closes #9.

EDIT: Forgot to add that I replaced setdefault with that if statement there on purpose, because that's more efficient than constructing an instance of DependencyitemStatus() and then discarding it every time that line runs. Ref: https://stackoverflow.com/questions/22797251/why-does-setdefault-evaluate-default-when-key-is-set

DevilXD avatar Jun 07 '20 07:06 DevilXD

Can this please be merged? :(

GalOzRlz avatar Oct 28 '21 06:10 GalOzRlz

Many thanks for your contribution and please apologize for the very late reply! I needed some time to really look into this and didn't got around to do that earlier.

Unfortunately, your change breaks documented use cases. Please note that this is not just another artificial example for the documentation. It is modeled after a real application that is used in production.

I can also provide an even simpler example to demonstrate the problem. Consider:

import pytest

@pytest.mark.parametrize("x", [
    pytest.param(0),
    pytest.param(1, marks=pytest.mark.xfail(reason="deliberate fail")),
])
@pytest.mark.dependency()
def test_a(x):
    assert x == 0

@pytest.mark.parametrize("x", [
    pytest.param(0, marks=pytest.mark.dependency(depends=["test_a[0]"])),
    pytest.param(1, marks=pytest.mark.dependency(depends=["test_a[1]"])),
])
def test_b(x):
    pass

I guess the idea is obvious: test_b for any parameter value should depend on test_a for the same parameter value. If you run this using the last release version of pytest-dependency, it works as expected:

$  python -m pytest --verbose test_params.py 
============================= test session starts ==============================
platform linux -- Python 3.10.1, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- /usr/bin/python
cachedir: .pytest_cache
rootdir: /home/abuild/test
plugins: dependency-0.5.1
collected 4 items                                                              

test_params.py::test_a[0] PASSED                                         [ 25%]
test_params.py::test_a[1] XFAIL (deliberate fail)                        [ 50%]
test_params.py::test_b[0] PASSED                                         [ 75%]
test_params.py::test_b[1] SKIPPED (test_b[1] depends on test_a[1])       [100%]

=================== 2 passed, 1 skipped, 1 xfailed in 0.03s ====================

test_a[1] fails and thus test_b[1] is skipped.

When I run the same example with your version, I get:

$  python -m pytest --verbose test_params.py 
============================= test session starts ==============================
platform linux -- Python 3.10.1, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- /usr/bin/python
cachedir: .pytest_cache
rootdir: /home/abuild/test
plugins: dependency-0.5.2.dev16+g0881560
collected 4 items                                                              

test_params.py::test_a[0] PASSED                                         [ 25%]
test_params.py::test_a[1] XFAIL (deliberate fail)                        [ 50%]
test_params.py::test_b[0] SKIPPED (test_b[0] depends on test_a[0])       [ 75%]
test_params.py::test_b[1] SKIPPED (test_b[1] depends on test_a[1])       [100%]

=================== 1 passed, 2 skipped, 1 xfailed in 0.03s ====================

Now, also test_b[0] is skipped, although its dependency test_a[0] had succeeded. This is not what we want.

The ability to distinguish in the dependencies the individual instances for different parameter values of a test is an important feature of pytest-dependency that must be preserved.

RKrahl avatar Jan 08 '22 14:01 RKrahl

Is it better now?

DevilXD avatar Jan 08 '22 21:01 DevilXD