-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a helper to return the names of parametrized test instances #9
Comments
There is no direct way to depend on all parameterized test instances of one test for the moment. What you can do is to collect all parameter values in a list variable and then construct the list of the names of the test instances from this list. Like this: import pytest
paramvalues = [True, False]
@pytest.mark.parametrize("x", paramvalues)
@pytest.mark.dependency()
def test_a(x):
if x:
pass
else:
assert False
@pytest.mark.dependency(depends=["test_a[%s]" % str(v) for v in paramvalues])
def test_b():
pass Would that be sufficient for your use case? |
@somnambWl, I need to add another remark on your example: you pass the same parameter value import pytest
def instances(name, params):
if len(set(params)) == len(params):
return ["%s[%s]" % (name, str(v)) for v in params]
else:
return ["%s[%d%s]" % (name, v[0], str(v[1])) for v in enumerate(params)]
paramvalues = [True, True]
@pytest.mark.parametrize("x", paramvalues)
@pytest.mark.dependency()
def test_a(x):
if x:
pass
else:
assert False
@pytest.mark.dependency(depends=instances("test_a", paramvalues))
def test_b():
pass On the other hand, I'm not sure if there are use cases where it actually makes sense to pass the same parameter value more the once to the same test. |
Passing same value twice was not intended I just wanted first test to pass twice. For me and for now, your workaround is sufficient, thank you :) I am using list of strings as parameter, but with
I can see names of tests which pytest uses and adjust your solution. |
As a conclusion, I'm considering to add a helper function import pytest
from pytest_dependency import get_param_instances
@pytest.mark.parametrize("x", [True, True])
@pytest.mark.dependency()
def test_a(x):
if x:
pass
else:
assert False
@pytest.mark.dependency(depends=get_param_instances("test_a"))
def test_b():
pass The benefit would be that you don't need to know how the test instances are named internally in pytest, you don't need to care about indexing in the case of double values in the parameters, and you don't need to store the parameter values. The problem is that this helper function would need to get the instance names from the internal data structures of pytest and for the moment, I have no idea where and how these are stored. The documentation on the internal data structures in pytest is … sparse. Note however that this Issue does not have high priority and might not make it into the next release version. |
How about something like The change needed to the original post would be adding a "*" after test_a so it would look like @pytest.mark.dependency(depends=["test_a*"])
def test_b():
pass |
It suffers from the same consistency issue as the proposal in #19. Consider: import pytest
def instances(name, params):
def vstr(val):
if isinstance(val, (list, tuple)):
return "-".join([str(v) for v in val])
else:
return str(val)
return ["%s[%s]" % (name, vstr(v)) for v in params]
@pytest.mark.parametrize("x,y", [
pytest.mark.dependency()((0,0)),
pytest.mark.dependency()(pytest.mark.xfail((0,1))),
pytest.mark.dependency()((1,0)),
pytest.mark.dependency()((1,1))
])
def test_a(x,y):
assert y <= x
@pytest.mark.dependency(depends=instances("test_a", [(0,0),(0,1),(1,0),(1,1)]))
def test_b():
pass Here |
the get_param_instances function would greatly help my project. I believe the documentation now is better w/ regards to hooks. I will try to take a look myself. |
Is there a way how can I mark parametrized test so that second, dependent, test will run only if first test passed on all parameters?
I know that I can mark each parameter, but this would be easier if the test should pass on all parameters and there is a lot of them.
Now this passes on first two tests (i.e. test_a) and second (test_b) is skipped.
The text was updated successfully, but these errors were encountered: