I just started a new project, and now with ASP.NET MVC being designed in an extremely composable way, I figured it might be a good time to start with unit testing. Most of my code is fresh, and I'm writing the tests before I'm writing the actual production code.
My frustration, though, is that I spend a lot more time fixing mistakes in my tests than in fixing anything wrong with my production tests.
My typical workflow ends up being something like this:
If you think about it, this is somewhat to be expected: unit tests involve producing output by hand and so is error-prone; code written in a strict language and with good coding practices has behavior that is specified very much automatically.
Of course, there are odd times when my production code is the actual cause of a test failing, but it's just really comparatively rare.
There's no reason to eliminate unit tests entirely, of course; there are times when I simply don't trust my own code at all. On the other hand, I'm starting to feel like it's not all that valuable -- especially the test-first philosophy.
Anyone else feel this way?
I feel you there, here are a couple of things I have found useful when writing unit tests to make them more solid:
TDD and unit testing is definitly something that starts off a real pain in the butt, but its one of those things that gets much easier and faster to do the more you keep at it.
Richard Dingwall's blog has quite a few articles on best practices for unit testing and TDD, many actually regarding unit testing with MVC.
First thing, TDD takes time to get adapted to.
We as a team started with TDD a year ago. Initially, we started with first writing the code(class/a single module) and then writing tests covering that code.
We then progressed to writing tests in parallel with the code(write a method/group of methods and then write test cases coverring those methods).During this period, we used coverage tools to check our coverage.
Now after doing this for a year, the team is well versed with test-first approach. @Scozzard has mentioned valid points about using AAA / mocking frameworks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With