Skip to content
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

Show repr() of exceptions passed to @parametrize #351

Closed
pytestbot opened this issue Sep 5, 2013 · 6 comments
Closed

Show repr() of exceptions passed to @parametrize #351

pytestbot opened this issue Sep 5, 2013 · 6 comments
Labels
type: enhancement new feature or API change, should be merged into features branch

Comments

@pytestbot
Copy link
Contributor

Originally reported by: Sam Thursfield (BitBucket: ssssam, GitHub: ssssam)


I have a number of test cases that test raising and handling exceptions, and I use @parametrize for this as follows:

@pytest.mark.parametrize(('exception'),
                         chain(runtime_exceptions, programmer_errors),
                         ids = [repr(e) for e in chain(runtime_exceptions,
                                                       programmer_errors)])
def test_meta_command_failure(self, exception, ...):

It would be nice if Py.test automatically included the exception names in the test names so that I didn't need the extra 'ids' parameter. I've attached an initial attempt at a patch that makes this happen.


@pytestbot
Copy link
Contributor Author

Original comment by Brianna Laugher (BitBucket: pfctdayelise, GitHub: pfctdayelise):


@ssssam where is the patch? I don't see an attachment...

@pytestbot
Copy link
Contributor Author

Original comment by Sam Thursfield (BitBucket: samthursfield, GitHub: samthursfield):


You're correct! Sorry. I've just tried to attach it again, but bitbucket sends me to a blank page and doesn't seem to actually attach the patch. I've put in pastebin instead here: http://pastebin.com/jVE2bagb

@pytestbot
Copy link
Contributor Author

Original comment by Brianna Laugher (BitBucket: pfctdayelise, GitHub: pfctdayelise):


So...this will work, but there are a kind of unending number of classes which might be appropriate to add here. Some classes might be better with str, some might be better with repr. And for individuals working in their own codebases it would be nice to add their relevant classes as well.

It is possible to make a plugin to modify the ids as you prefer, but it means repeating all the code in Metafunc.parametrize, which is not very sustainable.

Better options might be:

  • Ability to specify ids as a callable. Then you could write your own 'idmaker' function, which could potentially be different per test.
  • Otherwise, some hook in the idmaker function itself, so you could write (eg) a 'pytest_idmaker' function which gets called first, if it returns None it does the default thing.

I would probably lean towards the latter, thoughts, @hpk42 ?

@pytestbot
Copy link
Contributor Author

Original comment by holger krekel (BitBucket: hpk42, GitHub: hpk42):


I agree with @pfctdayelise on that the patch is not generic enough. And i tend to rather prefer the option one that she suggests, after which the original example would look like this:

#!python
@pytest.mark.parametrize(('exception'),
                         chain(runtime_exceptions, programmer_errors),
                         ids = lambda param: repr(param))
def test_meta_command_failure(self, exception, ...):
    ....

@pytestbot
Copy link
Contributor Author

Original comment by Sam Thursfield (BitBucket: samthursfield, GitHub: samthursfield):


Thanks for the feedback! Allowing ids to be a function makes sense and is a neat solution.

Sadly I'm not working with py.test right now so I don't think I'll get time to come up with another patch. I'm happy if you want to close the issue for now.

@pytestbot
Copy link
Contributor Author

Original comment by Floris Bruynooghe (BitBucket: flub, GitHub: flub):


Merged in pfctdayelise/pytest/issue351 (pull request #161)

Fixes issue351: Add ability to specify parametrize ids as a
callable, to generate custom test ids. + tests, docs

Hg branch merge

@pytestbot pytestbot added the type: enhancement new feature or API change, should be merged into features branch label Jun 15, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement new feature or API change, should be merged into features branch
Projects
None yet
Development

No branches or pull requests

1 participant