« The Hat| Main | The Hat - Result »

F#, TDD, xUnit - 2

| | Comments (0)

Continuing with the F# TDD fun...

So far everything's hunky-dorey, but F# is all about terse code and writing functions that then grow into systems. While xUnit lets us test a little easier, we're still stuck with a lot of overhead. Can we tighten this up a bit?

First let's tweak a couple of the xUnit methods a bit so they read better and we can use piping.

It's a nit, but little rewrites like this can help

One of the things I like about F# is watching how good programs "collapse". You start with some muddled code and then over the period of the project it just all kind of falls together into smaller and smaller pieces.

So will F# testing be the same way?

Well, by having our functions read better, we can pipeline them, saving a little space.

The pipelining here is still very readable, yet only takes up one line

It's looking a lot better, but there's still a lot of cruft involved with declaring an object and methods and such to put the tests in. This is a hangover from C# and the rest of the OO world where everything seems like it needs to be an object.

It'd be cool if we could just plop the test right out there like any other function, and it turns out with xUnit we can.

Now we're getting somewhere
A human-readable one-line test

Interesting tidbit here: while the test works fine in the xUnit command line and GUI, the open-source nifty-swifty Gallilo can't find these tests at all. Seems like the folks writing Gallilo want all their tests to be stuck in objects, dang-it.

This is a lot tighter than we had before, but there's one more step. What if we wanted to use more data-driven testing techniques? Wouldn't it be cool to have a set of data that was used to test the function, instead of having to explicitly write a new method for each scenario? So If I wanted to test the isPrimeNumber method with 8, 1, 4, and 5, wouldn't it be better to just list these out (or better yet, stick them in a little database) and have the test loop through and try each one out?

Never fear! We can do that too, although I got wrapped around the axle with typing trying to do it. The code looks a little messier -- there's probably something really simple I'm missing: all of the casting is butt-ugly.

Now we're getting somewhere
Now I can start separating my test data from my tests
This data can be inlined (as shown here) or put into an Excel, CSV, or database file

I'm really unhappy with all the casting going on, but this is the most succinct testing code yet. When the tester runs, it goes through each of the InlineData elements and calls the function using their data. So it's like having 5 tests in one. Note we have both positive and negative tests running, and if we had used an Excel file, we could do a huge amount of testing in just a line or two.

UPDATE: I was going to leave the article as it was, but I couldn't take this thing but so far. After all, test names should indicate the type of test being performed, but not the data. So the next and final step is to split up your data by test type -- let's say testing odd numbers and testing evens. That way when the test fails you'll know exactly what isn't working.

You might split up into odds and evens, or edge cases
However you split them, the test name should show exactly what you're testing

Leave a comment

About this Entry

This page contains a single entry by DanielBMarkham published on September 4, 2009 8:25 PM.

The Hat was the previous entry in this blog.

The Hat - Result is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Social Widgets

Share Bookmark this on Delicious

Information you might find handy
(other sites I have worked on)

Recently I created a list of books that hackers recommend to each other -- what are the books super hackers use to help guide them form their own startups and make millions? hn-books might be a site you'd like to check out.
On the low-end of the spectrum, I realized that a lot of people have problems logging into Facebook, of all things. So I created a micro-site to help folks learn how to log-in correctly, and to share various funny pictures and such that folks might like to share with their friends. It's called (appropriately enough) facebook login help