Click here to Skip to main content
15,908,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Can anyone explain with a simple example how to write unit test method after creating test project? I do find some article on different websites on how to create test project in VS 2008 but i could not find any example how to write a unit test method.

Thanks.
Posted
Comments
aayu 1-Oct-10 3:05am    
me to same problem if you get the example do post

These could be helpful.

http://msdn.microsoft.com/en-us/library/ms379625(VS.80).aspx[^]

http://msdn.microsoft.com/en-us/library/ms182524(v=VS.90).aspx[^]

If you are very new to Unit testing. Go get NUnit. It has lot of samples to work with basics of Unit Testing. But, it uses NUnit testing Framework. But, good resource to pick the things pretty fast.
Later, migrate to whatever testing framework you want to.
 
Share this answer
 
v2
Comments
aayu 1-Oct-10 3:35am    
second link is not working
 
Share this answer
 
C#
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class MyTests
{
  // test framework code...
  public TestContext TestContext { get; set; }

  // utility code...

  // a test method
  [TestMethod]
  public void MyTest()
  {
    // setup, e.g. prepare test data

    // execute, e.g. trigger the funtion to test and acquire the actual result

    // check, e.g. Assert.AreEqual(a, b, "your message on what went wrong");

    // cleanup, e.g. remove prepared test data

  }
  // more test methods...
}


Run it from the code by the context menu --> Run Test.

There are more attributed methods like [TestInitialize] and [TestCleanup], etc. that help fine tuning the test environment.

Tip: Define code snippets to create test methods and other code fragments to ease daily work.

Regards

Andi
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900