用Codesmith生成Nunit测试工程的模板
昨天晚上刚把自己项目的一个类库写完,松了口气,在做一下Nunit的测试就OK了。
接着就打开CodeSmith,准备用CodeSmith把Nunit单元测试的代码框架用它生成一下。以前草草的写了一个模板,就是把测试工程的类、命名空间和几个方法生成一下。
刚生成出来,忽然想到我的类里那么多方法,一个一个再写写,太累了(CodeSmith的模板里,我原来的方法名称是填写的),能用反射吗?这样就方便了(没办法,就是懒啊)。马上着手查了查CodeSmith的示例,发现.net的代码可以直接写在里面,试了试,居然也有智能提示,哈哈,太好了
于是,先写了个函数:
<script runat="template">
'获取Methed成员的集合
Public Function GetMetheds(ByVal strAssembly As String, ByVal strType As String) As System.Reflection.MethodInfo()
Dim SampleAssembly As System.Reflection.Assembly
SampleAssembly = System.Reflection.Assembly.loadfrom(strAssembly & ".dll")
Dim myType() As Type = SampleAssembly.GetTypes
Dim dllType As Type
For Each dllType In myType
If dllType.Name = strType.Trim Then
return dllType.GetMethods
End If
Next
End Function
public Function GetDate()as String
return datetime.Now.ToString("yyyy-MM-dd")
End Function
</script>
'获取Methed成员的集合
Public Function GetMetheds(ByVal strAssembly As String, ByVal strType As String) As System.Reflection.MethodInfo()
Dim SampleAssembly As System.Reflection.Assembly
SampleAssembly = System.Reflection.Assembly.loadfrom(strAssembly & ".dll")
Dim myType() As Type = SampleAssembly.GetTypes
Dim dllType As Type
For Each dllType In myType
If dllType.Name = strType.Trim Then
return dllType.GetMethods
End If
Next
End Function
public Function GetDate()as String
return datetime.Now.ToString("yyyy-MM-dd")
End Function
</script>
这样,代码区域就可以用循环生成测试方法了:
注:方法的命名规则是--类的方法名+Test
<% Dim mi As System.Reflection.MemberInfo %>
<% for each mi in GetMetheds(MyAssembly,MyType) %>
<Test()> Public Sub <%= mi.Name %>Test()
End Sub
<% Next %>
<% for each mi in GetMetheds(MyAssembly,MyType) %>
<Test()> Public Sub <%= mi.Name %>Test()
End Sub
<% Next %>
运行了一下,咦,多了好多方法:Dispose,GetHashCode..........
看来是继承自基类的方法,看来得过滤掉,修改了一下:
<% Dim mi As System.Reflection.MemberInfo %>
<% dim aMark as string()={"Dispose","GetHashCode","Equals","ToString","GetType"} %>
<% for each mi in GetMetheds(MyAssembly,MyType) %>
<% If Array.IndexOf(aMark, mi.Name) = -1 Then %>
<Test()> Public Sub <%= mi.Name %>Test()
End Sub
<% End If %>
<% Next %>
<% dim aMark as string()={"Dispose","GetHashCode","Equals","ToString","GetType"} %>
<% for each mi in GetMetheds(MyAssembly,MyType) %>
<% If Array.IndexOf(aMark, mi.Name) = -1 Then %>
<Test()> Public Sub <%= mi.Name %>Test()
End Sub
<% End If %>
<% Next %>
哈哈,这下生成的就舒服多了。
以下是以Jmail的dll文件中POP3CLASS为例:
Imports NUnit.Framework
Namespace Exing.TestCase
''' -----------------------------------------------------------------------------
''' Project : TestCase
''' Class : TestCase.POP3Class
'''
''' -----------------------------------------------------------------------------
''' <summary>
''' Jmail测试
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' [铱星] 2005-09-26 Created
''' </history>
''' -----------------------------------------------------------------------------
<TestFixture()> Public Class POP3ClassTest
<SetUp()> Public Sub init()
End Sub
<Test()> Public Sub DownloadUnreadMessagesTest()
End Sub
<Test()> Public Sub DeleteMessagesTest()
End Sub
<Test()> Public Sub GetMessageUIDTest()
End Sub
<Test()> Public Sub DownloadMessagesTest()
End Sub
<Test()> Public Sub DownloadHeadersTest()
End Sub
<Test()> Public Sub DeleteSingleMessageTest()
End Sub
<Test()> Public Sub DownloadSingleHeaderTest()
End Sub
<Test()> Public Sub GetLastUnreadMessageTest()
End Sub
<Test()> Public Sub get_MessagesTest()
End Sub
<Test()> Public Sub get_SizeTest()
End Sub
<Test()> Public Sub get_CountTest()
End Sub
<Test()> Public Sub set_LoggingTest()
End Sub
<Test()> Public Sub get_LoggingTest()
End Sub
<Test()> Public Sub get_LogTest()
End Sub
<Test()> Public Sub DisconnectTest()
End Sub
<Test()> Public Sub ConnectTest()
End Sub
<Test()> Public Sub CreateObjRefTest()
End Sub
<Test()> Public Sub InitializeLifetimeServiceTest()
End Sub
<Test()> Public Sub GetLifetimeServiceTest()
End Sub
<TearDown()> Public Sub TearDown()
End Sub
End Class
End Namespace
Namespace Exing.TestCase
''' -----------------------------------------------------------------------------
''' Project : TestCase
''' Class : TestCase.POP3Class
'''
''' -----------------------------------------------------------------------------
''' <summary>
''' Jmail测试
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' [铱星] 2005-09-26 Created
''' </history>
''' -----------------------------------------------------------------------------
<TestFixture()> Public Class POP3ClassTest
<SetUp()> Public Sub init()
End Sub
<Test()> Public Sub DownloadUnreadMessagesTest()
End Sub
<Test()> Public Sub DeleteMessagesTest()
End Sub
<Test()> Public Sub GetMessageUIDTest()
End Sub
<Test()> Public Sub DownloadMessagesTest()
End Sub
<Test()> Public Sub DownloadHeadersTest()
End Sub
<Test()> Public Sub DeleteSingleMessageTest()
End Sub
<Test()> Public Sub DownloadSingleHeaderTest()
End Sub
<Test()> Public Sub GetLastUnreadMessageTest()
End Sub
<Test()> Public Sub get_MessagesTest()
End Sub
<Test()> Public Sub get_SizeTest()
End Sub
<Test()> Public Sub get_CountTest()
End Sub
<Test()> Public Sub set_LoggingTest()
End Sub
<Test()> Public Sub get_LoggingTest()
End Sub
<Test()> Public Sub get_LogTest()
End Sub
<Test()> Public Sub DisconnectTest()
End Sub
<Test()> Public Sub ConnectTest()
End Sub
<Test()> Public Sub CreateObjRefTest()
End Sub
<Test()> Public Sub InitializeLifetimeServiceTest()
End Sub
<Test()> Public Sub GetLifetimeServiceTest()
End Sub
<TearDown()> Public Sub TearDown()
End Sub
End Class
End Namespace
唉,以前怎么没早想到啊,白死了那么多脑细胞..........