Which mock library do you prefer?

Ben Finney ben+python at benfinney.id.au
Wed Mar 3 08:08:20 EST 2010


Albert van der Horst <albert at spenarnc.xs4all.nl> writes:

> Unit testing is a concept that goes well with functions without side
> effects. If you have classes, that doesn't work so well.

How so? Unit tests are ideal for testing classes, in my experience; they
can be inspected and tested as a distinct unit of code.

> For classes use cases are the way to go.

That's a category error. Use cases are good for *functional* testing;
that is, testing of the system as a whole, with the focus on specific
uses to which the system needs to be put.

They're quite orthogonal. Neither interferes with the other. It's best
to have both.

> Think about it. The whole state of an object can affect the way a
> method works. So effectively for a unit test you have to put the
> object in a whole special fully controlled state.

That's right. This is called a “test fixture”, and it's a central
concept of unit testing. There is explicit support in xUnit-style unit
test libraries (like Python's ‘unittest’) for test fixtures.

-- 
 \     “When I was a kid I used to pray every night for a new bicycle. |
  `\    Then I realised that the Lord doesn't work that way so I stole |
_o__)                   one and asked Him to forgive me.” —Emo Philips |
Ben Finney



More information about the Python-list mailing list