自动测试用工具

写代码的时候,常需要判断一些方法或者技术点是否合适?必须写一些简单的代码,进行测试。每次测试一个技能或者一个方法应用都必须建一个新工程,很麻烦,同事也不利于公众技术问题的总结。

一直以来都想做一个简单的工程文件,其中包括长期以来积累的各类小代码集合。方便知识积累,同时也能提高工作效率。

思路:在一个工程下,每次想创建一个方法(测试),新建一个方法名如TestXX,需要完成的内容全部写进TestXX方法后。启动代码,在CheckedListBox(或者其它ListBox等)下自动显示平时创建的Test名。如下,画面启动左侧显示所有Test方法名。

根据自己的需要,选择并点击该TestXX,立刻执行自己想要的测试代码。

下午花了1个小时,做了个简单的示例。

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using System.Reflection;
using CSharp.Japanese.Kanaxs;

namespace WindowsFormsApplication2
{
public partial class TESTFORM : Form
{
Dictionary<string, EventHandler> dicEventH;
public TESTFORM()
{
InitializeComponent();

InitialControl();
}

/// <summary>
/// get the methods name and the delegate of the method
/// </summary>
/// <param name="type"></param>
private void GetMethods(Type type)
{
dicEventH = new Dictionary<string,EventHandler>();
foreach (var method in type.GetMethods())
{
string tempName = method.Name;
if (tempName.Contains("Test"))
{
//put all the test method name into the chkListBox control.
chkListBox.Items.Add(tempName, false);

//set all the test mothod delegate into listEventH
dicEventH.Add(tempName,(EventHandler)Delegate.CreateDelegate(
typeof(EventHandler), this, method));
}

}
}

//initialize the control
private void InitialControl()
{
chkListBox.Items.Clear();
GetMethods(typeof(TESTFORM));
}

private void chkListBox_SelectedIndexChanged(object sender, EventArgs e)
{
//item is checked and be chosed at the same time
if (chkListBox.CheckedItems.Contains(chkListBox.SelectedItem))
{
var value = dicEventH[chkListBox.SelectedItem.ToString()];
value(sender, e);
}
}

#region added the test method as you like, but keep the name as TestXXX
public void Test1(object sender, EventArgs e)
{
//TEST1
string methodName = string.Format(MethodBase.GetCurrentMethod().Name);
textBox1.AppendText(methodName + "\n");

}

public void Test2(object sender, EventArgs e)
{
//TEST1
string methodName = string.Format(MethodBase.GetCurrentMethod().Name);
textBox1.AppendText(methodName + "\n");

}

public void Test3(object sender, EventArgs e)
{
string methodName = string.Format(MethodBase.GetCurrentMethod().Name);

string msg = "くるり動物病院にとな゜1゜2゙3?4?5";
textBox1.AppendText("Before" + msg + "\n");

string msgC = Strings.StrConv(msg, VbStrConv.Wide, 0).Replace("\\", "¥").Replace("?", " ");
textBox1.AppendText("Strings.StrConv:" + msgC + "\n");
string msgF = KanaEx.ToHankaku(msg);
textBox1.AppendText("KanaEx.ToHankaku:" + msgF + "\n");
}
public void Test4(object sender, EventArgs e)
{
//TEST4
string methodName = string.Format(MethodBase.GetCurrentMethod().Name);
textBox1.AppendText(methodName + "\n");

}

public void TestXX(object sender, EventArgs e)
{
//TESTXX
string methodName = string.Format(MethodBase.GetCurrentMethod().Name);
textBox1.AppendText(methodName + "\n");
//TODO what you want when you want a test
}
#endregion

private void btnClear_Click(object sender, EventArgs e)
{
InitialControl();
textBox1.Clear();
}


}
}

 

posted @ 2016-07-01 16:08  tomclock  阅读(222)  评论(0编辑  收藏  举报