[TIP] RFF: Article on python test design pattern - DAI

Aaron Maxwell amax at redsymbol.net
Wed Dec 9 22:31:49 PST 2009

On Tuesday 08 December 2009 09:58:54 am Gary Bernhardt wrote:
> I liked this article! I'm glad to see discussion of advanced testing,
> dependency injection, and *actual* unit testing in the Python world.
> Please keep it up! :)

Thank you!

> One thing that I'd point out is that you *are* mocking when you do
> this; it's just that Python allows you to hand-roll mocks very easily.

Agree totally.

> My main complaint is that the assert isn't the last line; I can't see,
> at a glance, what's being tested. In other words, the test doesn't
> follow arrange-act-assert. Consistent use of AAA test organization
> makes it easier to scan tests quickly, which matters a lot with a
> large suite!

Yes, it does.  I don't see a way to change that, either, with this pattern.

> class TestUrl:
>     def test_build_url(self):
>         urlopen = Dingus()
>         RemoteResource('testpath.json', urlopen)
>         assert urlopen.calls('()', 'http://example.com/testpath.json')

This is nice because it's very easily understandable, even if you're not 
familiar with Dingus.

> No SUT changes are needed, and it's the same number of lines, but it's
> more expressive in my opinion, mostly because it follows the
> arrange-act-assert pattern. There's exactly one line per step: arrange
> the collaborators, perform the action under test, then assert on the
> collaboration.

Aaron Maxwell

More information about the testing-in-python mailing list