This article will show you a simple unit test framework.
You can get the basic theory and ideas of Nunit test framework.
Download all source code from here!
TestClassAttribute.cs
/*
* Created By HOME
* User: BPLOVEGCY
* Date: 2007-6-12
* Time: 11:04
*
* http://bplovegcy.blogspot.com/
*
*/
using System;
using System.Reflection;
using System.Collections.Generic ;
namespace UnitTest.Framework
{
// All test attributes will be used in test
/// <summary>
/// Can only be applied to class
/// </summary>
[AttributeUsage(AttributeTargets.Class, Inherited = false )]
public class TestClassAttribute:Attribute{}
/// <summary>
/// Can only be applied to Method
/// </summary>
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
public class TestMethodAttribute:Attribute{}
/// <summary>
/// Can only be applied to Method
/// </summary>
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
public class TestClassInitializationAttribute:Attribute{}
/// <summary>
/// Can only be applied to Method
/// </summary>
[AttributeUsage( AttributeTargets.Method, Inherited = false)]
public class TestClassCleanAttribute:Attribute{}
/// <summary>
/// Can only be applied to Method
/// </summary>
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
public class TestSetupAttribute:Attribute{}
/// <summary>
/// Can only be applied to Method
/// </summary>
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
public class TestCleanAttribute:Attribute{}
}
* Created By HOME
* User: BPLOVEGCY
* Date: 2007-6-12
* Time: 11:04
*
* http://bplovegcy.blogspot.com/
*
*/
using System;
using System.Reflection;
using System.Collections.Generic ;
namespace UnitTest.Framework
{
// All test attributes will be used in test
/// <summary>
/// Can only be applied to class
/// </summary>
[AttributeUsage(AttributeTargets.Class, Inherited = false )]
public class TestClassAttribute:Attribute{}
/// <summary>
/// Can only be applied to Method
/// </summary>
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
public class TestMethodAttribute:Attribute{}
/// <summary>
/// Can only be applied to Method
/// </summary>
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
public class TestClassInitializationAttribute:Attribute{}
/// <summary>
/// Can only be applied to Method
/// </summary>
[AttributeUsage( AttributeTargets.Method, Inherited = false)]
public class TestClassCleanAttribute:Attribute{}
/// <summary>
/// Can only be applied to Method
/// </summary>
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
public class TestSetupAttribute:Attribute{}
/// <summary>
/// Can only be applied to Method
/// </summary>
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
public class TestCleanAttribute:Attribute{}
}
TestObject.cs
/*
* Created By HOME
* User: BPLOVEGCY
* Date: 2007-6-12
* Time: 11:20
*
* http://bplovegcy.blogspot.com/
*
*/
using System;
namespace UnitTest.Framework
{
/// <summary>
/// Only an object on which all test attributes are applied
/// </summary>
[TestClass()]
public class TestObject
{
public TestObject()
{
}
[TestMethod()]
public void Test1()
{
Print( "Calling Test1");
}
[TestMethod()]
public void Test2()
{
Print( "Calling Test2" );
}
[TestMethod()]
public void Test3()
{
Print("Calling Test3 ");
}
[TestClassInitialization]
public void ClassInit()
{
Print("Calling TestClassInitialization ");
}
void Print( string message)
{
Console.WriteLine(message);
}
[TestClassClean]
public void ClassClean()
{
Print( "Calling ClassClean");
}
[TestSetup]
public void SetupTest()
{
Print("Calling SetupTest ");
}
[TestClean]
public void CleanTest()
{
Print("Calling CleanTest ");
}
}
}
* Created By HOME
* User: BPLOVEGCY
* Date: 2007-6-12
* Time: 11:20
*
* http://bplovegcy.blogspot.com/
*
*/
using System;
namespace UnitTest.Framework
{
/// <summary>
/// Only an object on which all test attributes are applied
/// </summary>
[TestClass()]
public class TestObject
{
public TestObject()
{
}
[TestMethod()]
public void Test1()
{
Print( "Calling Test1");
}
[TestMethod()]
public void Test2()
{
Print( "Calling Test2" );
}
[TestMethod()]
public void Test3()
{
Print("Calling Test3 ");
}
[TestClassInitialization]
public void ClassInit()
{
Print("Calling TestClassInitialization ");
}
void Print( string message)
{
Console.WriteLine(message);
}
[TestClassClean]
public void ClassClean()
{
Print( "Calling ClassClean");
}
[TestSetup]
public void SetupTest()
{
Print("Calling SetupTest ");
}
[TestClean]
public void CleanTest()
{
Print("Calling CleanTest ");
}
}
}
TestRun.cs
/*
* Created By HOME
* User: BPLOVEGCY
* Date: 2007-6-12
* Time: 11:20
*
* http://bplovegcy.blogspot.com/
*
*/
using System;
using System.Reflection;
using System.Collections.Generic ;
namespace UnitTest.Framework
{
/// <summary>
/// The Class to parse the assembly, retrieve the test classes and run test methods
/// </summary>
public class RunTest
{
static void Main()
{
Run();
Console.Read();
}
/// <summary>
/// Run the test methods in the current assembly
/// </summary>
public static void Run()
{
Run(typeof(RunTest).Assembly);
}
/// <summary>
/// Run test methods in the target assembly
/// </summary>
/// <param name="target"></param>
public static void Run(Assembly target)
{
// retrieve all types and get all test classes
foreach (Type t in target.GetTypes()){
if (t.GetCustomAttributes(typeof (TestClassAttribute), false).Length > 0){
RunTestClass(t);
}
}
}
/// <summary>
/// Run the test methods in the target test class
/// </summary>
/// <param name="testClass"></param>
public static void RunTestClass(Type testClass)
{
// create an instance of the test class
object testClassObject = Activator.CreateInstance(testClass);
// run test class initialization method
RunMethod(GetMethod(testClass, typeof(TestClassInitializationAttribute)), testClassObject);
// run all test methods
foreach (MethodInfo testMethod in GetMethods(testClass, typeof(TestMethodAttribute))){
// run test set up method
RunMethod(GetMethod(testClass, typeof(TestSetupAttribute)), testClassObject);
// run test method
RunMethod(testMethod, testClassObject);
// run test clean
RunMethod(GetMethod(testClass, typeof(TestCleanAttribute)), testClassObject);
}
// run test class clean
RunMethod(GetMethod(testClass, typeof (TestClassCleanAttribute)), testClassObject);
}
/// <summary>
/// Invoke method on the target object
/// </summary>
/// <param name="method"></param>
/// <param name="target"></param>
private static void RunMethod(MethodInfo method, Object target)
{
if (method == null)
return;
method.Invoke(target, null);
}
/// <summary>
/// Get the objecttype's method with the attribute type
/// </summary>
/// <param name="objectType"></param>
/// <param name="attributeType"></param>
/// <returns></returns>
private static MethodInfo GetMethod(Type objectType, Type attributeType)
{
foreach (MethodInfo method in objectType.GetMethods()){
if (method.GetCustomAttributes(attributeType, false).Length > 0)
return method;
}
return null;
}
/// <summary>
/// Get all objecttype's method with the attribute type
/// </summary>
/// <param name="objectType"></param>
/// <param name="attributeType"></param>
/// <returns></returns>
private static MethodInfo[] GetMethods(Type objectType, Type attributeType)
{
List<MethodInfo> methods = new List <MethodInfo>();
foreach (MethodInfo method in objectType.GetMethods()){
if (method.GetCustomAttributes(attributeType, false).Length > 0)
methods.Add(method);
}
return methods.ToArray();
}
}
}
* Created By HOME
* User: BPLOVEGCY
* Date: 2007-6-12
* Time: 11:20
*
* http://bplovegcy.blogspot.com/
*
*/
using System;
using System.Reflection;
using System.Collections.Generic ;
namespace UnitTest.Framework
{
/// <summary>
/// The Class to parse the assembly, retrieve the test classes and run test methods
/// </summary>
public class RunTest
{
static void Main()
{
Run();
Console.Read();
}
/// <summary>
/// Run the test methods in the current assembly
/// </summary>
public static void Run()
{
Run(typeof(RunTest).Assembly);
}
/// <summary>
/// Run test methods in the target assembly
/// </summary>
/// <param name="target"></param>
public static void Run(Assembly target)
{
// retrieve all types and get all test classes
foreach (Type t in target.GetTypes()){
if (t.GetCustomAttributes(typeof (TestClassAttribute), false).Length > 0){
RunTestClass(t);
}
}
}
/// <summary>
/// Run the test methods in the target test class
/// </summary>
/// <param name="testClass"></param>
public static void RunTestClass(Type testClass)
{
// create an instance of the test class
object testClassObject = Activator.CreateInstance(testClass);
// run test class initialization method
RunMethod(GetMethod(testClass, typeof(TestClassInitializationAttribute)), testClassObject);
// run all test methods
foreach (MethodInfo testMethod in GetMethods(testClass, typeof(TestMethodAttribute))){
// run test set up method
RunMethod(GetMethod(testClass, typeof(TestSetupAttribute)), testClassObject);
// run test method
RunMethod(testMethod, testClassObject);
// run test clean
RunMethod(GetMethod(testClass, typeof(TestCleanAttribute)), testClassObject);
}
// run test class clean
RunMethod(GetMethod(testClass, typeof (TestClassCleanAttribute)), testClassObject);
}
/// <summary>
/// Invoke method on the target object
/// </summary>
/// <param name="method"></param>
/// <param name="target"></param>
private static void RunMethod(MethodInfo method, Object target)
{
if (method == null)
return;
method.Invoke(target, null);
}
/// <summary>
/// Get the objecttype's method with the attribute type
/// </summary>
/// <param name="objectType"></param>
/// <param name="attributeType"></param>
/// <returns></returns>
private static MethodInfo GetMethod(Type objectType, Type attributeType)
{
foreach (MethodInfo method in objectType.GetMethods()){
if (method.GetCustomAttributes(attributeType, false).Length > 0)
return method;
}
return null;
}
/// <summary>
/// Get all objecttype's method with the attribute type
/// </summary>
/// <param name="objectType"></param>
/// <param name="attributeType"></param>
/// <returns></returns>
private static MethodInfo[] GetMethods(Type objectType, Type attributeType)
{
List<MethodInfo> methods = new List <MethodInfo>();
foreach (MethodInfo method in objectType.GetMethods()){
if (method.GetCustomAttributes(attributeType, false).Length > 0)
methods.Add(method);
}
return methods.ToArray();
}
}
}
--
Happy day, happy life!
No comments:
Post a Comment