Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
Resources » Articles » .NET Framework »
TDD - Test-Driven Development
|
Introduction
Unit testing plays a major part in development of a stable and bug free code. Developers mostly perform unit testing, after the code was designed and written. Though is the basic approach followed since long, this has got its own drawbacks too. This is because of many reasons, and the most common reason is short of time!
Test-driven development (TDD) gives a better look for this and tries to resolve the problem of Unit Testing to produce better quality code. What it does is, gives a Framework which writes tests for a code before actual code is written.
Why Unit Testing is important? Unit testing is nothing but a assurance for the code, i.e. it should perform a fixed operation and produce fixed results when a fixed input is passed to the code.
In practical terms, Unit Test is a program that test the public interfaces of all of the classes in the solution. One can categorize Unit Testing as "testing which makes sure that, the methods written are performing as expected."
NUnit Framework - a better IDE for testing.
Testing a code, having a complex architecture like multiple inheritance is a big pain. Also, writing test code in such environment imposes a more complex class inheritance and interfacing. .NET introduced a new concept of programming using "attributes" to resolve this issue.
Attributes: They allow you to add "Information" to your code. They don't affect the running of code, but rather provide "more" information about the code.
This is the way NUnit works.
The test will be carried out based on the attributes in the compiled code. Thus NO NEED to have a inheritance from the common base class for the test code.
NUNit comes with many of the useful attributes that can be used while creating unit tests. Using NUnit attributes, its easy to define test fixtures, test methods, setup and teardown methods. NUnit also give a better way for handling expected exceptions or to skip the test.
In this article, i will try to introduce some of the vary common attributes a programmer can use to have a good TDD.
1. TestFixture Attribute
The TestFixture attribute is used to indicate that a class contains test methods. This attribute simple tells that "This is a Test Class", when attach this to a class in the project.
Following is the code which illustrates the usage of this attribute.
namespace TestExample { using System; using NUnit.Framework;
[TestFixture] public class TestsClass { } }
NOTE: The class, should not have constructor explicitly written, that use the TestFixture attribute.
2. Test Attribute
The Test attribute is used to indicate that a method within a test fixture should be run as a Test. The method must be public, returning void, and should not take any parameters.
Following is the code which illustrates the usage of this attribute.
namespace TestExample { using System; using NUnit.Framework;
[TestFixture] public class TestsClass { [Test] public void FirstTest() { // Test code here. } } }
3. Setup & Teardown Attributes
When it is required by the test, to have some pre-conditions and some post-conditions, while running the test, these tow attributes can be used.
Writing a private method and calling it from each test method is also a way, but using Setup and Teardown attributes is more easy! These attributes indicate that a method should be executed at "SetUp" or at "Teardown" of every test method in the Test Fixture. The most common use for these attributes is when you need to have some objects initialized before testing and de-initializing after testing.
Following is the code which illustrates the usage of this attribute.
namespace TestExample { using System; using NUnit.Framework;
[TestFixture] public class TestClass { private bool _FirstTime
[SetUp] public void Setup() { _FirstTime = true; }
[TearDown] public void TearDown() { _FirstTime = false; }
[Test] public void FirstTest() { // Test code here. } } }
4. ExpectedException Attribute and Ignore Attribute
NUnit framework provides tow more useful attributes which are used in testing a huge code. ExpectedException : This is better than using try - catch block. Use this attribute when you know that the Test is going to throw an exception.
Ignore: As the name suggests, this is an attribute simply to "Ignore" the test, while running tests for the entire solution. This is better than commenting out the entire test code which we want to "Ignore".
Actual TDD - Development using Test-Driven approach
Phuee..at last we are at that stage where we can say that, "We know how to write a Unit Test code" :)
But..just like programming, knowing the NUnit syntax is not enough.
To use TDD more efficiently, lets have a check list for "HOw we should have a TDD?"
1. Write a test first. 2. Try to run the test. (oops..it will not compile, as the code we want to test is not written) 3. Write a dummy code which will compile the test code too :) 4. Again try and run the test - this should fail. 5. Now, try to implement the code to make the test pass. 6. Run the test. It should pass.
This means we have written a Code, which is a good code ;)
Well, sounds a bit off-track, but this is really a good practice. I can understand that, writing a test case even before we have the actual code is bit unusual. But this helps in reducing efforts of designing and a programmer can pin point to "writing a exact method" rather than thinking on "what will be the structure of my class?"
As we have already seen, writing a test case for an existing code is some thing which we all programmers try to avoid :) but if we go by this method, TDD, we will always have a code which is tested at the same time when the code is being written!
Summary
TDD - A Test-driven development is a better way to have a improved quality code. This is useful as we have to think twice before designing the code, and also this helps in having a testing code at the end of the day.
|
Responses
|
| Author: Irfan Patel 19 Aug 2004 | Member Level: Silver Points : 0 | Thats a great article!! Kudos !! A very well written article on one of the most difficult topics to write. Keep up the good work !!
- Irfan Patel (MCSD) Technical Architect.
|
|