Click here to Skip to main content
15,905,967 members
Articles / All Topics

Beginning Unit Testing...

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
16 Aug 2011CPOL4 min read 14.3K   2
Beginning Unit Testing...

I finally decided to get back into Unit Testing...

I have worked once in the past for a short period of time (only a couple of months) with unit testing, and my experience is good with it. The problem is that where I currently work, my team isn't really into it, and aren't too keen on trying either.

Since I haven't used it in a while, it feels to me as if I lost my unit testing mojo, and need to learn everything from scratch again.
At first, I though the best way to start is to actually write unit tests for projects I already wrote. This isn't TDD at all, but it's still something, and I feel as if I'm getting the "infrastructure" ready for the rest of the process...

Another thing I just thought about doing today was giving a lecture at work to my fellow team mates on unit testing, hoping that they will see the good side to it, and they'll want to know a little more on the subject!

Sometimes the best way to learn is by teaching! :)

I started building a demo project that I will show as part of a presentation I'm working on to go with my lecture. The project is just a simple calculator.

I am gathering all the points I need to discuss on this lecture, but trying to keep it simple and as convincing as possible to people who don't think it's useful or don't know what it is yet. Seems like quite a challenge, but I'm up for it!

So far this is what I got: (these are the points that are going into my presentation)

Let's say we're building a new class as part of a winform that does some calculation. How do we usually go about testing this?
We would press f5 (debug) and play around with the UI a bit to see that everything's working...

Why is this bad?

  1. We have to do this again and again every time we change a little piece of code since we can't be sure it still works.
  2. If someone else on the team changes our code, we'll need to run the tests again.
  3. Someone might change a piece of code, play around with the UI and still not know about special corner-cases we were looking to handle in a specific way.
  4. Time consuming!
  5. In this case, it's a winform and might be easy to test manually, but if we're just building a web service for example, I usually see people building these funny "tester" projects that they can send some sample input and see what happens.

Why does writing Unit Tests make life easier?

  1. By writing unit tests, we are writing code that will check our functionality instead of doing it manually.
  2. We can run the tests every change we make, and this will be much faster than manually testing.
  3. If we write the tests properly and cover all the code we wrote, then someone else can change the code around or add some other functionality with the knowledge he didn't screw our code over!
  4. Just by looking at the unit tests, we can spot the corner-cases we should look out for.

What do we test and what not to test ?

  1. Test all public methods in a class. If the tests are written good enough, then the private methods will be covered within these tests.
  2. Make sure you're not writing tests to check the .NET Framework - this means you don't need to go crazy and start writing tests for each .NET method!
  3. Tests shouldn't need configuration. You should always be able to run the tests immediately. This means you need to avoid having your tests writing out to log files, db, network streams, etc. Instead, create mocks of these interfaces so they don't actually do this during testing.

Well, I feel like I'm new to this as well, so I'm looking for some feedback on these points I have for my presentation so far...

During the presentation, I also want to show the simple demo test project I created, and how to write basic tests for it. I will show common cases when changing code can cause different behavior that might not be spotted at first by manually testing...

Seeya'...

P.S. - Anyone interested in learning more about unit testing should check out these blogs: Ayende & Roy Osherove

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Israel Israel
Started programming e-commerce sites with PHP & MySQL at the age of 14. Worked for me well for about 5 years.

Transfered to C# & asp.net, while serving in the IDF.
Worked on the 'Core Performance' Team at ShopYourWay.com (Sears Israel)
Currently working at Logz.io

Check out my blog!
or my twitter

Comments and Discussions

 
QuestionNegative Testing Pin
butchzn16-Aug-11 2:56
butchzn16-Aug-11 2:56 
AnswerRe: Negative Testing Pin
Gilly Barr17-Aug-11 4:58
Gilly Barr17-Aug-11 4:58 
Of course!
Thanks for putting in your 2 cents Smile | :)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.