在SharpDevelop2.2中进行单元测试

本文介绍如何在SharpDevelop2.2中使用NUnit进行单元测试,包括创建测试用例、添加引用、运行测试及查看结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SharpDevelop2.2中进行单元测试

陈能技

<chsdate isrocdate="False" islunardate="False" day="4" month="1" year="2008"><span lang="EN-US"><font face="Times New Roman" size="3">2008-1-4</font></span></chsdate>

SharpDevelop2.2内置了NUnit,安装完后,可在下面目录找到NUnit

D:/Program Files/SharpDevelop/2.2/bin/Tools/NUnit

并且在SharpDevelop中可建立专门的Unit Test文件

<shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape id="_x0000_i1025" style="WIDTH: 360.75pt; HEIGHT: 177.75pt" type="#_x0000_t75"><imagedata o:title="" src="file:///D:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtml1/01/clip_image001.png"></imagedata></shape>

默认建立如下所示的Nunit单元测试代码框架:

/*

* Created by SharpDevelop.

* User: user

* Date: <chsdate isrocdate="False" islunardate="False" day="4" month="1" year="2008"><span lang="EN-US" style='FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: "Courier New"; mso-font-kerning: 0pt'>2008-1-4</span></chsdate>

* Time: 9:12

*

* To change this template use Tools | Options | Coding | Edit Standard Headers.

*/

using System;

using NUnit.Framework;

using NUnit.Framework.SyntaxHelpers;

namespace Test1

{

[TestFixture]

public class Test1

{

[Test]

public void TestMethod()

{

// TODO: Add your test.

}

}

}

假设我在MainForm中有如下需要测试的代码:

public int add(int i,int j)

{

return i+j;

}

则可在Test1方便地添加如下所示的单元测试代码:

[TestFixture]

public class Test1

{

[Test]

public void TestMethod()

{

// TODO: Add your test.

MainForm f = new MainForm();

Assert.AreEqual(2,f.add(1,1),"1+1=2");

}

}

但是,如果直接就这样编译会报错,提示找不到NUnit,需要在Project中的Reference添加对nunit.framework.dll的引用。

<shape id="_x0000_i1026" style="WIDTH: 111pt; HEIGHT: 143.25pt" type="#_x0000_t75"><imagedata o:title="" src="file:///D:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtml1/01/clip_image003.png"><font size="3"></font></imagedata></shape>

可手工添加,也可通过选择“View | Tools | Unit Test”,在出现的“Unit Test”管理界面中按“add reference to Nunit to current project”添加。

运行单元测试,可通过在测试方法中点右键,选择“current method | unit tests | run in unit test pad”运行,也可通过选择“View | Tools | Unit Test”,在出现的“Unit Test”管理界面中运行。

<shape id="_x0000_i1027" style="WIDTH: 307.5pt; HEIGHT: 223.5pt" type="#_x0000_t75"><imagedata o:title="" src="file:///D:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtml1/01/clip_image005.png"><font size="3"></font></imagedata></shape>

<shape id="_x0000_i1028" style="WIDTH: 377.25pt; HEIGHT: 218.25pt" type="#_x0000_t75"><imagedata o:title="" src="file:///D:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtml1/01/clip_image007.png"><font face="Times New Roman" size="3"></font></imagedata></shape>

运行结果信息显示在OutPut界面中:

"D:/Program Files/SharpDevelop/2.2/bin/../bin/Tools/NUnit/nunit-console.exe" "D:/OpenSourceTestTool/SharpDevelop/Test1/bin/Debug/Test1.exe" /results="C:/Documents and Settings/user/Local Settings/Temp/tmp24B.tmp" /fixture="Test1.Test1" /testMethodName="Test1.Test1.TestMethod"

NUnit version <chsdate isrocdate="False" islunardate="False" day="30" month="12" year="1899"><span lang="EN-US" style='FONT-SIZE: 10pt; FONT-FAMILY: "Courier New"; mso-font-kerning: 0pt'>2.4.2</span></chsdate>

Copyright (C) 2002-2007 Charlie Poole.

Copyright (C) 2002-2004 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov.

Copyright (C) 2000-2002 Philip Craig.

All Rights Reserved.

Runtime Environment -

OS Version: Microsoft Windows NT 5.1.2600 Service Pack 2

CLR Version: 2.0.50727.42 ( Net 2.0.50727.42 )

.

Tests run: 1, Failures: 0, Not run: 0, Time: 0.351 seconds

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值