Unit Test Framework "Aisin. Net unit test tool (assionunit)" development-Step 1

Source: Internet
Author: User
* Introduction: When I was doing a unit test recently, I suddenly had an impulse to do my own unit test framework. This person is required to do it as long as he has an idea (most of them have done half ).

First, sort out the ideas. My thoughts are as follows:
1. Use reflection to dynamically load the Assembly
2. Search for classes and Methods marked with custom features
3. customize an exception mechanism to return test results
4. Development of some auxiliary judgment classes
5. A friendly client tool or vs plug-in is used to start and feedback test results.

The user features are as follows:
1. Test features /// <Summary>
/// Test the class user features, and the classes with this feature participate in Unit Testing
/// </Summary>
[Attributeusage (attributetargets. Class)]
Public class testclassattribute: attribute
{

}

2. Test method features

Code
/// <Summary>
/// Test method user features, and add the features to participate in Unit Testing
/// </Summary>
[Attributeusage (attributetargets. Method)]
Public class testattribute: attribute
{
Private alertstype _ alertstype; // alert type

/// <Summary>
/// No parameters are constructed by default.
/// </Summary>
/// <Param name = "alerytype"> </param>
Public testattribute ()
{
_ Alertstype = alertstype. Red;
}

/// <Summary>
/// Alert type attribute
/// </Summary>
Public alertstype isalerttype
{
Get {return _ alertstype ;}
Set {_ alertstype = value ;}
}

}

Reference in the project to be tested: assionunit. Framework
Add the corresponding features to the test class and Method: Code
/// <Summary>
/// Used to test the basicmodule class
/// </Summary>
[Testclass]
Public class basicmoduletest
{
/// <Summary>
/// Test the Add Method
/// </Summary>
[Test (isalerttype = alertstype. Red)]
Public void add ()
{
Basicmodule Bm = new basicmodule ();
Int result = BM. Add (1, 2 );
Assert. areequal (result, 3 );
}

/// <Summary>
///
/// </Summary>
[Test (isalerttype = alertstype. Yellow)]
Public void Add2 ()
{

}
}

Now assert. areequal has no idea how to implement it. Now we plan to use exception processing.

The code for the client to load the Assembly has been written (but the program is exclusive to the Assembly, I don't know if appdomain can be used to solve it ?)

Code
/// <Summary>
/// Bind the Assembly tree control
/// </Summary>
/// <Param name = "asserblyname"> assembly path name </param>
Private void bindtree ()
{
If (asserblyname. Length <= 0)
Return;

Bool istesting = false; // whether there are test classes and Methods

Syslefttree. nodes. Clear (); // clears the control first.

Assembly asserbly = assembly. loadfrom (asserblyname); // reflection loaded assembly

Treenode rootnode = new treenode ();
Rootnode. Text = asserbly. getname (). Name; // Assembly display name
Rootnode. Checked = true; // select
Syslefttree. nodes. Add (rootnode );

Type [] types = asserbly. gettypes (); // retrieve all types of Arrays

// Loop all classes
For (INT I = 0; I <types. length; I ++)
{
Testclassattribute testclass = iscustomerattribute. gettestclassattribute (types [I]); // query features

// If there are test features
If (testclass! = NULL)
{
Treenode twonode = new treenode ();
Twonode. Text = types [I]. Name; // Class Name
Twonode. Checked = true;
Rootnode. nodes. Add (twonode );

Methodinfo [] Methods = types [I]. getmethods (); // retrieve all method Arrays

// Loop all methods
For (INT m = 0; m <methods. length; m ++)
{
Testattribute test = iscustomerattribute. gettestattribute (methods [m]); // query features

// If the test method features are available
If (test! = NULL)
{
Treenode threenode = new treenode ();
Threenode. Text = methods [M]. Name; // method name

// If it is yellow
If (test. isalerttype = alertstype. Yellow)
Threenode. forecolor = color. Yellow;
Threenode. Checked = true; // selected by default
Twonode. nodes. Add (threenode );

Istesting = true; // test method available, set to true
}
}
}
}

Syslefttree. expandall (); // expand all subtree nodes

// Clear all nodes if no test method is available
If (! Istesting)
Syslefttree. nodes. Clear ();

}

Finally, send a tool UI diagram:

Finally, I am sending this post to welcome you to give me some ideas, suggestions, comments, abuse, attacks, ridicule, and irony ......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.