Nant和Nunit构建C#代码

开发 后端
本文介绍Nant和Nunit构建C#代码,想用Nant和Nunit构建C#代码,可写好build文件后运行UnitTest时遇到了麻烦。

以前没使用Nant和Nunit构建C#代码的自动化构建,今天自己写了一个C#程序,想用Nant和Nunit构建C#代码。可写好build文件后运行UnitTest时遇到了麻烦。命令行提示如下:
Could not load file or assembly 'nunit.framework, Version=2.4.3.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77' or one of its dependencies. The system cannot find the file specified.
查了一下资料解决了这个问题。

解决方法是:

一、在该程序的config文件(如果程序名是money.dll,则该文件名为money.dll.config)中加入如下代码: 

  1. <?xmlversionxmlversion="1.0"encoding="utf-8"?> 
  2. <configuration> 
  3. <runtime> 
  4. <assemblyBindingxmlnsassemblyBindingxmlns="urn:schemas-microsoft-com:asm.v1"> 
  5. <dependentAssembly> 
  6. <assemblyIdentitynameassemblyIdentityname="nunit.framework"
    publicKeyToken="96d09a1eb7f44a77"culture="Neutral"/> 
  7. <bindingRedirectoldVersionbindingRedirectoldVersion="2.0.6.0"newVersion="2.4.3.0"/> 
  8. <bindingRedirectoldVersionbindingRedirectoldVersion="2.1.4.0"newVersion="2.4.3.0"/> 
  9. </dependentAssembly> 
  10. </assemblyBinding> 
  11. </runtime> 
  12. </configuration> 

二、使用VS2005提供的gacutil把nant.core.dll 和 nant.framework.dll注册一下。

具体做法是:

1、在window开始菜单用运行VS所带的Visual Studio 2005 Command Prompt。

2、切换到nunit的bin目录下

3、顺序运行下列命令
gacutil /i nunit.core.dll
// 注册core

gacutil /i nunit.framework.dll
//注册framework

gacutil /l
//查看是否注册上

三、大功告成。

现在运行 nant unittest 就完事大吉。

附 nant 的 build 文件如下:

  1. <?xmlversionxmlversion="1.0"?> 
  2. <projectnameprojectname="CSharpMoney"default="ut"> 
  3. <propertynamepropertyname="output.dir"value="../bin"/> 
  4. <propertynamepropertyname="output.dll"value="../bin/cs-money.dll"/> 
  5. <propertynamepropertyname="reports.dir"value="../reports"/> 
  6. <targetnametargetname="clean"> 
  7. <deletedirdeletedir="${output.dir}"/> 
  8. <deletedirdeletedir="${reports.dir}"/> 
  9. </target> 
  10. <targetnametargetname="copyfile"depends="clean"> 
  11. <mkdirdirmkdirdir="${output.dir}"unless="${directory::exists(output.dir)}"/> 
  12. <copyfilecopyfile="../nunit/bin/nunit.framework.dll"todir="${output.dir}"  
  13. if="${file::exists('nunit/bin/nunit.framework.dll')}"/> 
  14. </target> 
  15. <targetnametargetname="build"depends="copyfile"> 
  16. <csctargetcsctarget="library"output="${output.dll}"debug="true"> 
  17. <sources> 
  18. <includenameincludename="*.cs"/> 
  19. </sources> 
  20. <references> 
  21. <includenameincludename="../nunit/bin/nunit.framework.dll"/> 
  22. </references> 
  23. </csc> 
  24. </target> 
  25. <targetnametargetname="ut"depends="build"> 
  26. <mkdirdirmkdirdir="${reports.dir}"/> 
  27. <execprogramexecprogram="..NUnitunit-console.exe"> 
  28. <argvalueargvalue="${output.dll}"/> 
  29. <argvalueargvalue="/config=cs-money.dll.config"/> 
  30. <argvalueargvalue="/xml=${reports.dir}TestReport-Unit.xml"/> 
  31. <argvalueargvalue="/nologo"/> 
  32. <argvalueargvalue="/noshadow"/> 
  33. </exec> 
  34. </target> 
  35. </project> 

以上介绍Nant和Nunit构建C#代码

【编辑推荐】

  1. C#调用Windows API函数
  2. 详解C#调用Outlook API
  3. C#连接Access、SQL Server数据库
  4. 介绍C#调用API的问题
  5. C#调用Excel与附加代码
责任编辑:佚名 来源: 百度空间
相关推荐

2009-08-14 14:19:50

Enhanced LiC#构建

2009-08-19 15:30:40

C#复合控件构建

2009-08-18 10:54:17

C#事件和委托

2011-05-18 18:05:47

C#C++

2011-05-18 17:56:38

C#C++

2009-08-13 17:04:09

C#语言C#程序

2009-08-27 16:11:03

C# delegateC# event

2009-08-06 16:30:58

C#代码和验证码图片

2009-08-26 10:34:15

C#类型C#变量

2009-08-19 16:50:32

Visual C#C#语言特性

2009-09-02 17:12:06

C#关机代码

2011-04-13 17:31:16

C#.NET

2009-08-13 17:36:54

编译C#代码

2009-08-06 09:32:03

C#代码协同执行

2009-09-01 10:37:51

C#项目代码C#代码规范

2011-07-06 09:46:56

C#

2011-07-06 09:44:34

C#

2009-08-14 16:54:19

C# Hello Wo

2009-08-19 15:38:59

C#代码

2009-09-07 06:48:13

C#透明窗体
点赞
收藏

51CTO技术栈公众号