Re: unittest: assertRaises() with an instance instead of a type

2012-04-02 Thread Steve Howell
On Mar 28, 6:55 pm, Ben Finney ben+pyt...@benfinney.id.au wrote: Steven D'Aprano steve+comp.lang.pyt...@pearwood.info writes: (By the way, I have to question the design of an exception with error codes. That seems pretty poor design to me. Normally the exception *type* acts as equivalent to

Re: unittest: assertRaises() with an instance instead of a type

2012-03-30 Thread Ethan Furman
Steven D'Aprano wrote: To the degree that the decision of how finely to slice tests is a matter of personal judgement and/or taste, I was wrong to say that is not the right way. I should have said that is not how I would do that test. I believe that a single test is too coarse, and three or

Re: unittest: assertRaises() with an instance instead of a type

2012-03-29 Thread Steven D'Aprano
On Thu, 29 Mar 2012 12:55:13 +1100, Ben Finney wrote: Steven D'Aprano steve+comp.lang.pyt...@pearwood.info writes: (By the way, I have to question the design of an exception with error codes. That seems pretty poor design to me. Normally the exception *type* acts as equivalent to an error

Re: unittest: assertRaises() with an instance instead of a type

2012-03-29 Thread Peter Otten
Ben Finney wrote: Steven D'Aprano steve+comp.lang.pyt...@pearwood.info writes: (By the way, I have to question the design of an exception with error codes. That seems pretty poor design to me. Normally the exception *type* acts as equivalent to an error code.) Have a look at Python's

Re: unittest: assertRaises() with an instance instead of a type

2012-03-29 Thread Ulrich Eckhardt
Am 28.03.2012 20:07, schrieb Steven D'Aprano: First off, that is not Python code. catch Exception gives a syntax error. Old C++ habits... :| Secondly, that is not the right way to do this unit test. You are testing two distinct things, so you should write it as two separate tests:

Re: unittest: assertRaises() with an instance instead of a type

2012-03-29 Thread Peter Otten
Ulrich Eckhardt wrote: True. Normally. I'd adapting to a legacy system though, similar to OSError, and that system simply emits error codes which the easiest way to handle is by wrapping them. If you have err = some_func() if err: raise MyException(err) the effort to convert it to exc

Re: unittest: assertRaises() with an instance instead of a type

2012-03-29 Thread Ulrich Eckhardt
Am 28.03.2012 20:26, schrieb Terry Reedy: On 3/28/2012 8:28 AM, Ulrich Eckhardt wrote: with self.assertRaises(MyException(SOME_FOO_ERROR)): foo() I presume that if this worked the way you want, all attributes would have to match. The message part of builtin exceptions is allowed to

tabs/spaces (was: Re: unittest: assertRaises() with an instance instead of a type)

2012-03-29 Thread Ulrich Eckhardt
Am 28.03.2012 20:26, schrieb Terry Reedy: On 3/28/2012 8:28 AM, Ulrich Eckhardt wrote: [...] # call testee and verify results try: ...call function here... except exception_type as e: if not exception is None: self.assertEqual(e, exception) Did you use tabs? They do not get preserved

Re: tabs/spaces (was: Re: unittest: assertRaises() with an instance instead of a type)

2012-03-29 Thread Roy Smith
In article 0ved49-hie@satorlaser.homedns.org, Ulrich Eckhardt ulrich.eckha...@dominolaser.com wrote: I didn't consciously use tabs, actually I would rather avoid them. That said, my posting looks correctly indented in my sent folder and also in the copy received from my newsserver. What

Re: unittest: assertRaises() with an instance instead of a type

2012-03-29 Thread Terry Reedy
On 3/29/2012 3:28 AM, Ulrich Eckhardt wrote: Equality comparison is by id. So this code will not do what you want. Exception('foo') == Exception('foo') False Yikes! That was unexpected and completely changes my idea. Any clue whether this is intentional? Is identity the fallback when no

Re: unittest: assertRaises() with an instance instead of a type

2012-03-29 Thread Ethan Furman
Steven D'Aprano wrote: On Wed, 28 Mar 2012 14:28:08 +0200, Ulrich Eckhardt wrote: Hi! I'm currently writing some tests for the error handling of some code. In this scenario, I must make sure that both the correct exception is raised and that the contained error code is correct: try:

Re: unittest: assertRaises() with an instance instead of a type

2012-03-29 Thread Steven D'Aprano
On Thu, 29 Mar 2012 09:08:30 +0200, Ulrich Eckhardt wrote: Am 28.03.2012 20:07, schrieb Steven D'Aprano: Secondly, that is not the right way to do this unit test. You are testing two distinct things, so you should write it as two separate tests: [..code..] If foo does *not* raise an

Re: unittest: assertRaises() with an instance instead of a type

2012-03-29 Thread Steven D'Aprano
On Thu, 29 Mar 2012 08:35:16 -0700, Ethan Furman wrote: Steven D'Aprano wrote: On Wed, 28 Mar 2012 14:28:08 +0200, Ulrich Eckhardt wrote: Hi! I'm currently writing some tests for the error handling of some code. In this scenario, I must make sure that both the correct exception is raised

Re: unittest: assertRaises() with an instance instead of a type

2012-03-28 Thread Ben Finney
Steven D'Aprano steve+comp.lang.pyt...@pearwood.info writes: (By the way, I have to question the design of an exception with error codes. That seems pretty poor design to me. Normally the exception *type* acts as equivalent to an error code.) Have a look at Python's built-in OSError. The

unittest: assertRaises() with an instance instead of a type

2012-03-28 Thread Ulrich Eckhardt
Hi! I'm currently writing some tests for the error handling of some code. In this scenario, I must make sure that both the correct exception is raised and that the contained error code is correct: try: foo() self.fail('exception not raised') catch MyException as e:

Re: unittest: assertRaises() with an instance instead of a type

2012-03-28 Thread Steven D'Aprano
On Wed, 28 Mar 2012 14:28:08 +0200, Ulrich Eckhardt wrote: Hi! I'm currently writing some tests for the error handling of some code. In this scenario, I must make sure that both the correct exception is raised and that the contained error code is correct: try: foo()

Re: unittest: assertRaises() with an instance instead of a type

2012-03-28 Thread Terry Reedy
On 3/28/2012 8:28 AM, Ulrich Eckhardt wrote: Hi! I'm currently writing some tests for the error handling of some code. In this scenario, I must make sure that both the correct exception is raised and that the contained error code is correct: try: foo() self.fail('exception not raised') catch