[C#]_[使用NUnit对业务逻辑代码进行单元测试]


Nunit是xUnit系列中应用于C#的测试框架,类似JUnit,Gtest等测试框架。可对类,接口的业务逻辑进行单元测试,目前如果不会单元测试的开发人员就太不专业了。


场景:

1.当编写网站代码时,写了独立的查询类库,但是这个类库的逻辑如何进行测试呢,比较慢的方式就是人工点击鼠标,通过网页进行测试。但是这样的效率实在太低了,特别是在出现问题时,还得不停的修改代码-》运行-》点击页面。如此反复,很浪费开发者的时间,特别是改了一些逻辑还得反复测,代码相当不可靠。

2.如果有单元测试的话,就可以利用已经写好的测试案例进行回归测试。比如你写好一些边界条件的测试数据,改了代码只需要重新跑一次单元测试就行了。

3.好处已经足够多了。。如果用熟了,效率提高的不是一点半点。如果觉得时间宝贵的同学建议用上单元测试吧。

4.虽然VS2010也自带了微软的测试框架,但是我不建议使用,因为这些都是对IDE耦合太高了,换了IDE可能整个测试用例都用不了。


步骤1:

下载Nunit

http://www.nunit.org/,下载zip包,建议使用绿色版本的zip,好处就是不要污染电脑的注册表等。

解压之后放在:

E:\software\NUnit-2.6.2\

把E:\software\NUnit-2.6.2\bin添加到用户的环境变量path,这样可以直接通过命令行运行可执行文件.nunit-console.exe

通过命令行运行的好处就是可以加入持续集成,也就是自动化测试,对代码持续构建和持续测试,还有就是可以通过ide直接调用exe.


步骤2:

新建类库QX_Users.编写类库文件QueryHelper.cs,我这里改了使用.net frameworkd 2.0,生成QX_Users.dll库。

using System;
using System.Collections.Generic;
using System.Text;

namespace QxsLogic
{
    public class QueryHelper
    {
       public List<String> QueryUsers(String userId) 
        {
            String sql = "select name from users";
            Console.WriteLine(sql);
            List<String> lists = new List<string>();
            lists.Add("infoworld");
            lists.Add("英雄");
            lists.Add(userId);
            return lists;
        }
    }
}

步骤3:

在QX_Users目录下的新建tests目录,这样可以直接方便维护测试代码和项目代码了,建议使用这种项目结构,多增加一个tests目录.


步骤4:新建项目Test_QX_Users和测试代码TestQueryHelper.cs,建议使用这种命令,很直观知道测试的是哪个类。

直接通过浏览方式添加对NUnit目录下的nunit.framework.dll的引用。生成Test_QX_Users.dll库。

using System;
using System.Collections.Generic;
using System.Text;
using QxsLogic;

namespace Test_QX_Users
{
    using NUnit.Framework;
    
    [TestFixture]
    public class TestQueryHelper
    {
        [Test]
        public void TestQueryUsers()
        {
            QueryHelper query = new QueryHelper();
            String id = "hero";
            List<String> lists = query.QueryUsers(id);
            foreach (String name in lists)
            {
                Console.WriteLine(name);
            }
            Assert.Greater(lists.Count,0);
            Assert.AreEqual(id,lists[lists.Count-1]);
            Console.WriteLine("finish Test");
        }
    }
}

步骤5:在Test_QX_Users项目录下新建一个批处理(这个位置可以不污染测试代码),命名nunit-test.bat,建议使用这种命名,可以知道使用哪个测试框架:

nunit-console Test_QX_Users\bin\Debug\Test_QX_Users.dll


步骤6:运行批处理nunit-test.bat,Nunit默认会在当前运行目录输出一个TestResult.xml测试报告,用在持续集成里再好不过了。

C:\Users\apple\Documents\Visual Studio 2010\Projects\ClassLibrary1\ClassLibrary1
\tests\Test_QX_Users>nunit-test.bat

C:\Users\apple\Documents\Visual Studio 2010\Projects\ClassLibrary1\ClassLibrary1
\tests\Test_QX_Users>nunit-console Test_QX_Users\bin\Debug\Test_QX_Users.dll
NUnit-Console version 2.6.2.12296
Copyright (C) 2002-2012 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 6.1.7601 Service Pack 1
  CLR Version: 2.0.50727.5466 ( Net 3.5 )

ProcessModel: Default    DomainUsage: Single
Execution Runtime: net-3.5
.select name from users
infoworld
英雄
hero
finish Test

Tests run: 1, Errors: 0, Failures: 0, Inconclusive: 0, Time: 0.1430082 seconds
  Not run: 0, Invalid: 0, Ignored: 0, Skipped: 0


TestResult.xml

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!--This file represents the results of running a test suite-->
<test-results name="C:\Users\apple\Documents\Visual Studio 2010\Projects\ClassLibrary1\ClassLibrary1\tests\Test_QX_Users\Test_QX_Users\bin\Debug\Test_QX_Users.dll" total="1" errors="0" failures="0" not-run="0" inconclusive="0" ignored="0" skipped="0" invalid="0" date="2012-12-15" time="11:16:31">
  <environment nunit-version="2.6.2.12296" clr-version="2.0.50727.5466" os-version="Microsoft Windows NT 6.1.7601 Service Pack 1" platform="Win32NT" cwd="C:\Users\apple\Documents\Visual Studio 2010\Projects\ClassLibrary1\ClassLibrary1\tests\Test_QX_Users" machine-name="APPLE-PC" user="apple" user-domain="apple-PC" />
  <culture-info current-culture="zh-CN" current-uiculture="zh-CN" />
  <test-suite type="Assembly" name="C:\Users\apple\Documents\Visual Studio 2010\Projects\ClassLibrary1\ClassLibrary1\tests\Test_QX_Users\Test_QX_Users\bin\Debug\Test_QX_Users.dll" executed="True" result="Success" success="True" time="0.143" asserts="0">
    <results>
      <test-suite type="Namespace" name="Test_QX_Users" executed="True" result="Success" success="True" time="0.132" asserts="0">
        <results>
          <test-suite type="TestFixture" name="TestQueryHelper" executed="True" result="Success" success="True" time="0.058" asserts="0">
            <results>
              <test-case name="Test_QX_Users.TestQueryHelper.TestQueryUsers" executed="True" result="Success" success="True" time="0.052" asserts="2" />
            </results>
          </test-suite>
        </results>
      </test-suite>
    </results>
  </test-suite>
</test-results>


备注:项目完整代码可以从这里下载:

http://download.csdn.net/detail/infoworld/4890295

NUnit的vs插件:

http://visualstudiogallery.msdn.microsoft.com/c8164c71-0836-4471-80ce-633383031099



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Peter(阿斯拉达)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值