The art of Unit Testing with Examples in .NET

    • .NET
    • TDD
    • Mock
    • nUnit
    • Book Review
    • Manning
    • Moq
  • modified:
  • reading: 4 minutes

osherove_coverFirst time when I familiarized with unit testing was 5 or 6 years ago. It was start of my developing career. I remember that somebody told me about code coverage. At that time I didn’t write any Unit tests. Guy, who was my team lead, told me “Do you see operator if with three conditions? You should check all of these conditions”. So, after that I had written some code, I should go to interface and try to invoke all code which I wrote from user interface. Nice? At current time I know little more about tests and unit testing. I have not participated in projects, designed by Test Driven Development (TDD). Basics of my knowledge are a spying code of my colleagues, some articles and screencasts. I had decide that I should know much more, and became a real professional of unit testing, this is why I had start to read book The art of Unit Testing with Examples in .NET. More than, in my current job place looks like I’m just one who writing unit tests for my code. I should show good examples of my tests.

The book was written about half year ago. Most of samples was written with C#. I saw a lot of recommendation for reading this book.

Author of this book is Roy Osherove. He is famous blogger, also he is lecturing TDD master classes, sometimes he is working consultant for IT companies, if they want to implement TDD in their projects (he wrote not one time about this, bring successful and unsuccessful cases). He is working as a main architect in company Typemock. Typemock developing paid instruments for tests/unit testing and code analysis.

The book will be useful for developers of any platforms. Like I said before most of examples was written with C# (some of examples with Java). In book author use NUnit framework for unit tests, Rhino Mocks for Mock and Stub objects in Unit tests (before that I didn’t know that Stub and Mock are different, I knew only about Mocks).

So, what you will find in the book? You can download a few chapters from official book’s site, so before buy the book you can read first is it good for you or not. First chapter of book is “Hello world!”. It is about what you need to write tests, what kind of frameworks, tools, and of course first test is there. Next chapter about frameworks which author use in his book: NUnit and Rhino Mocks. He used Rhino Mocks at book as I understand because he got statistic that Rhino Mocks is frequently used framework for creating Mocks and Stubs. I think that today situation little different, I like Moq. And NUnit today is still most used unit testing framework. One time I used MbUnit, because it has more features, but NUnit today has the same features.

It the book you will find information about how to write tests, how you should design them. And also you will find antipatterns: I found out all of them, I had used them in my code. :(

Also I found answer for my old question. I wrote some article about testing applications with NHibernate, and I got comment about why you are testing ORM? I thought that this man know more than me in unit testing, so really maybe I shouldn’t test ORM Repositories. At that time I had really specific case, so I said that I need it, and said why I need these tests. I didn’t know why this guy asked me this question. Really this is my integration test. With this test I check that database schema is equal to xml-mapping, mapping is equal to classes, and if I use special queries I check them too with my tests. It is really helpful. In the book I found answer for my question – tests for ORM is really helpful, author if this book always write tests for his Data Access Layer, and he said why.

Also in the book you will find how to introduce TDD in your company, how to work with Legacy code (this is really important when you introduce TDD in exist project, not new). Also a lot of questions and answers for guy, who want to introduce TDD in his group/company, he will get a lot of questions for his colleagues (money, time, knowledge, etc). 

At the end of book author made an inventory of frameworks, which you can use for testing. Of course in his list you will find paid frameworks, which was written by Typemock, where author are working.

Really I recommend this book for all companies, which use TDD in their practice and which write unit tests, so for all companies. My recommendation for team leads and managers, don’t say “You must write tests”, read book first, or ask your colleagues to read the book, make a lectures, trainings, and you will see the result. 

See Also