2008年5月29日 星期四

Test Driven Development with NUnit

Test Driven Development (TDD) [1] is a methodology to develop software system. The methodology is an evolutionary approach to development which combines test-first development where you write a test before you write just enough production code to fulfill that test and refactoring.
NUnit is a tool for .Net Platform to execute the TDD. You can download the install package from Nunit website [2].

The following section describe the basic steps for NUnit[2] integration.
  1. Import NUnit.framework.dll into project reference reference.
  2. Create test class with "using NUnit.Framework;"
  3. Add [TestFixture] attribute to identify the class is a test class
  4. Add [SetUp] attribute to a function which is used to setup the environment before test. (The test function will be called before each test function)
  5. Add [Test] attribute to a function which is used to identify the function is a unit test function.
  6. Run the test with NUnit Test Runner user interface. The runner programmer will ask you where is your test DLL, before you start to run the test.
In our project, we want to test our data access object(DAO). So, in the spring context will be initial in the setup function.

  1. #region declare dields
  2. private IApplicationContext ctx;
  3. #endregion

  1. [SetUp]
  2. public void SetUp()
  3. {
  4. BasicConfigurator.Configure();
  5. ctx =
  6. new XmlApplicationContext("assembly://PROJECT_NAME/NAME_SPACE/ApplicationContext.xml");
  7. }


Reference
  1. http://www.agiledata.org/essays/tdd.html
  2. http://www.nunit.org/index.php

沒有留言: