Ayende @ Rahien

Unnatural acts on source code

An example of bad test, part 2

Let us take a look at this test. When I saw it, the code under test called to me: "Help me, I being intimately harassed here!"

image

The problem is that this tests knows all sorts of unrelated things about the code under test. It knows that it should redirect, it knows where it should redirect, etc.

What is the relation between kicking off the checkout process and redirecting? Orthogonal concerns should be in different tests. I may want to send the users to Promotions, instead of Receipt, in the future. Should this test break?

So let us try this one:

image 

We removed the obvious cases of too-intimate tests. But we still have very bad behavior in the test. Specifically, there is a big issue with how we are asserting that the pipeline was kicked off.

On the surface, it may appear that we are doing state based testing, but what we are actually testing is the behavior of the checkout process itself. This is not something that we want to test in this test. In this test, we just want to verify that the checkout process is started.

This test will fail when we change the checkout process, even though the behavior that we intended to test remained the same.

Comments

Rob Conery
08/17/2008 03:04 AM by
Rob Conery

... and as I mentioned in your last post (and on my video, and on my blog) - this is a spike. This test is coming out. I happened to put it in a unit test as I wanted to make sure the execution was happening as I wanted to. There is a lot wrong here - but it's a SPIKE.

Ayende Rahien
08/17/2008 03:17 AM by
Ayende Rahien

Rob,

This wasn't supposed to be a review of the code.

This was supposed to be a good example to show bad testing practices.

Rob Conery
08/17/2008 06:47 PM by
Rob Conery

I spose my point is that this isn't a test in the literal sense - I just wanted to see if it would work and moreover give people something to comment on.

Cory Foy
08/18/2008 01:52 PM by
Cory Foy

Rob - I wouldn't worry about it too much. I've seen plenty of test cases just like that, and I think it is a great example. It might be good to post a link to what this test case got refactored into so we can all see magic at work. ;)

Avish
08/18/2008 05:56 PM by
Avish

Wait, you mean you're gonna go through all this trouble of whining about how wrong it is, and not tell us the right way to write this test? Come on.

Comments have been closed on this topic.