I am new to unit testing. I want to know to what level do I need to do unit testing. I mainly work on web applications. I am currently working through an MVC book and they showed a couple of unit tests for grid paging, and populating a menu control. Does one have to test business object properties, methods, data layer, stored procedures, etc? I don’t want to look paranoid and test everything. If it is best practices then I will do it.
For example, if a first name property is supposed to be 50 characters long, and may contain only alpha characters then do I need to test both?
I am also using the Yahoo User Interface library, do I need to test this as well?
What UI unit testing tools are available? I have heard that there are frameworks that allow you to mock a whole even if it is not there and then you can test the page this way? What frameworks for UI testing are available?
I would like to hear as much opinions as possible? Any suggested books for beginners with many samples would be appreciated.
Thanks
As well as agreeing with the other answers here I’ll also add a pragmatic point that I’ve found extremely useful.
When learning to write tests and getting used to the testing paradigm it’s often hard to know where to begin and what to test first. Testing everything is very much an ideal that can be acheived however there’s a pragmatic aspect to how much you can do right away.
Given that you’re working on a web project, one thing I’ve found very helpful is to start testing code that requires a lot of effort to test in the usual way.
An example of this that I have from a recent project is integrating some blogging mechanisms into our site based on a feed from blogger. In order to test my code I would have to build the blog project, then build the web project that references the blog project (which takes about 3 minutes in Visual Studio), then navigate to the page in question (waiting for the code to go through it’s sequence of events upon first run after build) and then go about testing the actual thing I’m interested in.
To make this process a lot easier you can begin writing tests for these kinds of scenarios. I’m a fan of TDD and testing in general and I’ve found that this kind of approach really gives the developer a more grounded idea of how and why testing can be useful. You really get to see a benefit right away by reducing the amount of time and steps you have in order to test some functionality. Managers can also highly appreciate this. (It’s a bit less fluffy than some of the more idealistic concepts and banter surrounding testing).
This approach also gets you into the fun aspect of writing tests. As with anything, if you start off by finding out how you can enjoy the process, you’re more likely to continue doing it.
I’ve worked with many an evangelist who snubbed out any fun or interest a new developer might have found through testing and made the whole process very religious. Make testing fun and you’re more likely to want to test more of your code.