You want to write unit test for any class. Following are the attributes you need to write a unit test.
Mandatory:
1 - TestClass - This attribute identifies that attributed class is a test class.
2 - TestMethod - This attribute identifies that the attributed method is a test method in the test class.
3 - TestInitialize: This attributed method runs before the every test method.
4 - TestCleanup: This attributed method runs after the every test method.
5 - ClassInitialize: This attributed method runs once when the unit test class is initialized. Normally it can be used when we perform DB seeding or mocking objects globally.
6 - ClassCleanup: This attributed method runs once when the unit test class has run all the test methods. It can be used when the data inserted needs to be rollback.
7 - TestCategory: When the group of classes need to executed simultaneously or in a one go then on the class this attributed is binded with any name. e.g. Financials or something else.
How to group module unit test classes.
8 -ExpectedException: When any function raise any exception then this attribute can be used.
Here's a snippet of a code that will show the above attributes.
//A business class that performs add operation.
public class Maths
{
public int Add(int a, int b)
{
return checked(a + b);
}
}
// A test class that checks the functionality of the business class Maths.
[TestClass]
public class UnitTest1
{
Maths obj = new Maths();
[TestInitialize] // you can use ClassInitialize attribute.
public void Setup()
{
System.Diagnostics.Debug.WriteLine("In a child debug ");
}
[TestCleanup] // you can use ClassCleanup attribute.
public void Cleanup()
{
obj.Dispose();
}
//// A test method.
[TestMethod]
public void TestMethod12()
{
Assert.IsTrue(obj.Add(2, 3) == 5);
}
/// A test method that shows exception checking.
[TestMethod]
[ExpectedException(typeof(OverflowException))]
public void TestOverflowException()
{
int i = obj.Add(int.MaxValue, int.MaxValue);
}
}
Verification of the Function return values.
There are some classes in the unit testing framework from which you can test your expectations or actual result.
1 - Assert - Description of the methods in this class are here
2 - CollectionAssert - Description of the methods in this class are here
You can check here to get more detail and more attributes about this testing framework.
Mandatory:
1 - TestClass - This attribute identifies that attributed class is a test class.
2 - TestMethod - This attribute identifies that the attributed method is a test method in the test class.
3 - TestInitialize: This attributed method runs before the every test method.
4 - TestCleanup: This attributed method runs after the every test method.
5 - ClassInitialize: This attributed method runs once when the unit test class is initialized. Normally it can be used when we perform DB seeding or mocking objects globally.
6 - ClassCleanup: This attributed method runs once when the unit test class has run all the test methods. It can be used when the data inserted needs to be rollback.
7 - TestCategory: When the group of classes need to executed simultaneously or in a one go then on the class this attributed is binded with any name. e.g. Financials or something else.
How to group module unit test classes.
8 -ExpectedException: When any function raise any exception then this attribute can be used.
Here's a snippet of a code that will show the above attributes.
//A business class that performs add operation.
public class Maths
{
public int Add(int a, int b)
{
return checked(a + b);
}
}
// A test class that checks the functionality of the business class Maths.
[TestClass]
public class UnitTest1
{
Maths obj = new Maths();
[TestInitialize] // you can use ClassInitialize attribute.
public void Setup()
{
System.Diagnostics.Debug.WriteLine("In a child debug ");
}
[TestCleanup] // you can use ClassCleanup attribute.
public void Cleanup()
{
obj.Dispose();
}
//// A test method.
[TestMethod]
public void TestMethod12()
{
Assert.IsTrue(obj.Add(2, 3) == 5);
}
/// A test method that shows exception checking.
[TestMethod]
[ExpectedException(typeof(OverflowException))]
public void TestOverflowException()
{
int i = obj.Add(int.MaxValue, int.MaxValue);
}
}
Verification of the Function return values.
There are some classes in the unit testing framework from which you can test your expectations or actual result.
1 - Assert - Description of the methods in this class are here
2 - CollectionAssert - Description of the methods in this class are here
You can check here to get more detail and more attributes about this testing framework.