Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First TDD, Simple 2-tier C# Project - what do I unit test?

This is probably a stupid question but my googling isn't finding a satisfactory answer. I'm starting a small project in C#, with just a business layer and a data access layer - strangely, the UI will come later, and I have very little (read:no) concept / control over what it will look like.

I would like to try TDD for this project. I'm using Visual Studio 2008 (soon to be 2010), I have ReSharper 5, and nUnit.

Again, I want to do Test-Driven Development, but not necessarily the entire XP system. My question is - when and where do I write the first unit test?

Do I only test logic before I write it, or do I test everything? It seems counter-productive to test things that have no reason to fail (auto-properties, empty constructors)...but it seems like the "No new code without a failing test" maxim requires this.

Links or references are fine (but preferably to online resources, not books - I would like to get started ASAP).

Thanks in advance for any guidance!

like image 598
Joel Avatar asked Jan 23 '23 00:01

Joel


2 Answers

It seems counter-productive to test things that have no reason to fail (auto-properties, empty constructors)...

It is...There's no logic in an empty constructor or auto-property to test.

when do I write the first unit test?

Before you write your first line of testable code. Think about the behavior you want your method to perform. Write your test based on the desired behavior. That test (and all the others that follow) embody your program's specifications.

where do I write the first unit test?

In the first Test Class you create.

This is probably the best online resource:

Introduction to Test Driven Design (TDD)
http://www.agiledata.org/essays/tdd.html

like image 130
Robert Harvey Avatar answered Feb 20 '23 15:02

Robert Harvey


There is a shift in mindset that you have to dive into with TDD. If you're not doing TDD, you'd usually write some code, then write a unit test to make sure the code does what you expect, and handles a few different corner cases. With TDD, you actually write a test first, that typically uses classes and methods that you haven't even written yet.

Once you write your test, and you're satisfied that the test is a good example of how your code should be used, you start to write the actual classes and method to make the test pass.

It's kind of hard at first, because you won't have intellisense help, and your code won't build until you actually implement the production code, but by writing the test first, you are forced to think about how your code will be used before you even write it.

like image 28
Andy White Avatar answered Feb 20 '23 15:02

Andy White