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

Example of how to handle an exception #16

Open
jruere opened this issue Jun 6, 2014 · 7 comments
Open

Example of how to handle an exception #16

jruere opened this issue Jun 6, 2014 · 7 comments

Comments

@jruere
Copy link

jruere commented Jun 6, 2014

Hi! I couldn't find an example on how to handle an exception.

Is it already somewhere?

@mdwhatcott
Copy link
Owner

@jruere - It's been a while since I've worked on this project. Can you paste in an example of what you're trying to accomplish?

@jruere
Copy link
Author

jruere commented Jun 6, 2014

def do(a):
    return 1 / a

Test that for a = 0, an exception is raised.

@mdwhatcott
Copy link
Owner

You can assert that an assertion was raised:

this(function_that_raises_exception).should.raise_an(Exception)

https://github.com/mdwhatcott/pyspecs/blob/master/tests/test_should.py#L108

Conversely, you should be able to chain .NOT to get the inverse effect:

this(function_that_finishes_successfully).should.NOT.raise_an(Exception)

https://github.com/mdwhatcott/pyspecs/blob/master/tests/test_should.py#L20

@jruere
Copy link
Author

jruere commented Jun 6, 2014

And how to I pass arguments? Should I use a lambda?

You have not worked lately on the project because you are using something else?

@mdwhatcott
Copy link
Owner

Yes, if the function receives arguments, wrap it in a lambda or a separate niladic function.

I'm not doing very much python work right now, other than quick scripts that don't stick around long. I'm doing a lot more in Go (I also created GoConvey, also a testing tool, but with a lot more going on). Honestly, if I was doing serious python work (in python 3+) I would probably use Contexts--it's basically what I was trying to come up with in the beginning, but he did a better job :)

@jruere
Copy link
Author

jruere commented Jun 6, 2014

Thanks for the answer.

Context is nice but it has a few things I dislike:

  1. it is really magical: method names, "triangulation". Makes me uneasy.
  2. I cannot see how to nest contexts. It appears to be one level only.
  3. 1 "test" per class.
  4. it doesn't use/integrate with an existing testing framework

PyVows is good although rather verbose and uses gevent for some reason...

I'll stick with pyspecs for a while longer. Thanks for this!

@mdwhatcott
Copy link
Owner

You're welcome. Glad you are finding this project useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants