[TIP] Unit testing functions which call super

Ned Batchelder ned at nedbatchelder.com
Wed Aug 13 14:13:07 PDT 2014


On 8/13/14 4:15 PM, Marcin Tustin wrote:
>
> Yes: the reason is that I'm not writing an integration test, I'm 
> writing a test to test this specific method. We need unit tests to 
> test our individual units, and integration tests to test them 
> together. It's not wise to rely only on integration tests (nor only on 
> unit tests).
>

I say let the test call super(), and call it a unit test.  These terms 
can't be taken too literally.  Other than it being "the wrong kind" of 
test, what actual bad thing will happen if you call super() in your unit 
test?

This seems like a lot of effort to subvert Python's class machinery.  
You'll end up with a mess, and probably more follow-on problems than you 
solve.

Probably the best approach is to refactor your code so that its more 
easily tested: if you are interested in what foofunc does without 
consideration of the base class, then create a function that doesn't 
call the base class.  Test that function.  Then you can call that 
function from another which does call the base class.

--Ned.
>
> I actually do share the concern about over mocking (it's easy to mock 
> so much that you test nothing), but it's important not to throw the 
> baby out with the bathwater. Integration tests by their nature lack 
> granularity to pinpoint problems (and yes, I realize it's possible and 
> desirable to set up integration tests so that do point to where a 
> detected problem originates, but it's still not a substitute for 
> appropriate unit testing).
>
> *From:*David Stanek [mailto:dstanek at dstanek.com]
> *Sent:* Wednesday, August 13, 2014 2:35 PM
> *To:* Marcin Tustin
> *Cc:* testing-in-python at lists.idyll.org
> *Subject:* Re: [TIP] Unit testing functions which call super
>
> On Wed, Aug 13, 2014 at 2:00 PM, Marcin Tustin 
> <Marcin.Tustin at dealertrack.com <mailto:Marcin.Tustin at dealertrack.com>> 
> wrote:
>
> Hi All,
>
> I've come across a method which I needed to add a unit test for, which 
> calls super, e.g.
>
>
> Is there any reason you can't call the parent's super?  In my view of 
> testing you want to create an object, feed it input, and then check 
> the output/side effect. I would stay away from mocking internals like 
> this.
>
> -- 
> David
> blog: http://www.traceback.org
> twitter: http://twitter.com/dstanek
>
> www: http://dstanek.com
>
>
>
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20140813/2c546cff/attachment.htm>


More information about the testing-in-python mailing list