-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Dynamically generated test methods not distinguishable on failure #2519
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
Comments
Thanks for writing @volans-.
Hmm so your suggestion would be to show the docstring somewhere with the failure? As mentioned in #445, in general the docstring already appears in the traceback (excuse the dummy example):
So in general adding the docstring would be noise for users in general. Perhaps displaying the docstring when
That actually looks nice, but I don't think it will solve your particular problem (I assume you use the default traceback option).
I believe you are generating the code programatically using function wrappers and such right? Unfortunately that's not trivial to do (in fact I don't think it is possible, but I might be wrong). One solution I can suggest is to add another section to the test report, like pytest does already for "Captured stdout" and "Capture stderr". Suppose this example which I think reproduces your use case: import pytest
def check_greater_than(x, y):
assert x > y
def make_test(x, y, extra):
@pytest.mark.custom_params(x=x, y=y, extra=extra)
def test():
check_greater_than(x, y)
return test
test_1 = make_test(6, 4, 'foo')
test_2 = make_test(1, 4, 'bar')
test_3 = make_test(9, 1, 'fizz')
test_4 = make_test(2, 9, 'foobar') Note that I'm marking the test function with the custom parameters used. Add this to your import pytest
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
outcome = yield
m = item.get_marker('custom_params')
if m:
rep = outcome.get_result()
rep.sections.append(('Custom params', sorted(m.kwargs.items()))) Here's how the test run looks like:
Note that you can add any amount of text as the second value of the tuple in |
@nicoddemus thanks a lot for your quick and detailed reply! Regarding #445 I was thinking maybe to print the docstring only if the name of the function in the report (like Regarding your suggested solution is very interesting, I didn't know I could add output sections (my bad). I've played a bit with it and it works great and definitely solve my issue, again, thanks a lot! Resolving this as there is a fair workaround to it and I guess this is not the best place to continue the discussion about #445. |
I'm dynamically generating test methods in a test class with different parameters, setting the
__name__
and__doc__
of the methods to be able to distinguish them. When I was running the tests withnose
, on failure the name and the docstring of the failed method were printed and I could easily recognize which of the generated test was failing.I'm moving to
pytest
but I'm facing the problem that only the name of the function is printed and not the docstring, that is the one where I was saving all the parameters with which the function was generated, that allowed to clearly distinguish them. I cannot embed all the parameters in the test name of course.Regarding the code that pytest prints on failure, is the generic one so it doesn't help at all in this case.
I have hence no easy way of distinguish which test failed without the use of some hack like printing to stdout/err the info I need, but de facto altering the tests given that those specific ones are testing a command line application and it's output.
One easy solution would be to implement the feature request in #445, printing the docstring of the failed method along with it's name.
The other possibility would be to show the actually generated code instad of the generating one.
The text was updated successfully, but these errors were encountered: