ASP.NE单元测试Mbunit WaitN Gallio

本文介绍如何使用MbUnit框架为ASP.NET应用创建单元测试。通过示例展示了从搭建测试环境到编写并运行第一个单元测试的全过程。

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

一直想有一种工具,可以对ASP.NET的页面进行单元测试。在网上找了很久,终于找到Mbunit和WaitN结合据说可以实现这种效果。

首先去Gallio的网站下载了最新版本。下载后安装。缺省安装目录是C:/Program Files/Gallio/。。。

开始第一步,建立测试工程。原文见http://www.gallio.org/wiki/doku.php?id=getting_started:my_first_tests

You will see step by step how to create and run a simple test project using Gallio and its default test framework MbUnit v3. It is assumed that you already know how to create projects and add references in Visual Studio, but no prior knowledge of unit testing or test frameworks is assumed. The screenshots were taken in Visual Studio 2010, but the steps are the same for Visual Studio 2005 and 2008.

1.

Let's start by creating a new class library project in Visual Studio called SimpleLibrary.

 

 

2.

For simplicity's sake, we will implement an unsophisticated class that computes some Fibonacci numbers. Delete the default class (normally Class1) added by Visual Studio and add a new one called Fibonacci. It will be a pretty simple public class with only one method called Calculate, as shown here:

namespace SimpleLibrary
{
   public class Fibonacci
   {
      public static int Calculate(int x)
      {
         if (x <= 0)
            return 0;
 
         if (x == 1)
            return 1;
 
         return Calculate(x - 1) + Calculate(x - 2);
      }
   }
}

3.

We will write some tests to verify that this method is working properly. Many people like to write the tests first, in what is called Test-Driven Development, usually abreviated TDD. If we were working in a TDD way we would have created a test, made it fail and only then we would have implemented the code required to make it pass (in this case the Calculate method's body), just to start over in what is known as the “red, green, refactor” cycle. Since the chosen development methodology doesn't affect the way we use the framework, we won't follow anyone in particular.

Tests should never be put in your production code, but in separate projects/assemblies. Add a new class library project to the SimpleLibrary solution called SimpleLibrary.Tests, delete the default class and add a new one called FibonacciTest. In the Gallio source code the convention is to name a test project after the project it's testing plus the suffix .Tests, and to name a unit test class after the class it's testing plus the suffix Test. This is a popular convention, but as you may guess everyone has its own preferences.

 

 

Now, still in the test project, you need to add a reference to the SimpleLibrary project, and also to the Gallio.dll and MbUnit.dll assemblies. If you have installed the Gallio bundle by using the MSI package, then the assemblies have been registered in the GAC.

Otherwise, you will find the assemblies in the bin folder of the Gallio installation directory, usually at C:/Program Files/Gallio/Bin.

 

Note that the common practice is to have this assembly as well as other referenced assemblies in a custom folder in your solution, mainly for location independency and versioning issues.

At this point, your test project should now reference all the necessary assemblies.

 

 

4.

With the project structure and references in place we can now write the first unit test. Open FibonacciTest.cs if not done already, and type the following code:

using System;
using SimpleLibrary;
using MbUnit.Framework;
 
namespace SimpleLibrary.Test
{
   public class FibonacciTest
   {
      [Test]
      public void Fibonacci_of_number_greater_than_one()
      {
         int result = Fibonacci.Calculate(6);
         Assert.AreEqual(8, result);
      }
   }
}

Notice the things we did in the previous code:

  • We imported the SimpleLibrary namespace.
  • We imported the MbUnit.Framework namespace.
  • We made the FibonacciTest class public. You may need to explicitly do so in your language of choice.
  • We added a new public method called Fibonacci_of_number_greater_than_one, which doesn't return a value.
  • We decorated the Fibonacci_of_number_greater_than_one method with the [Test] attribute.
  • We added a call to the Assert.Equal method.

You may be wondering what's the call to the Assert.AreEqual method supposed to do. Not too much in fact: it only checks that the return value of the call to Fibonacci.Calculate(6) is equal to 8. The Assert class is very important because it contains a lot of helpful methods that make writing tests easier.

Build the entire solution. The two projects should compiled without any error.

 

5.

Now we have our first unit test, but how do we execute it? Gallio comes bundled with many different runners, that is, programs or plugins for other programs that allows you to execute tests. This time we will use Icarus, the graphical runner, because it's a standalone application (meaning that you don't need to install anything else to run it). The Gallio installer creates a shortcut for it in the programs folder of the Start menu.

Start Icarus by activating its shortcut in Start MenuAll ProgramsGallioIcarus GUI Test Runner

6.

Once Icarus is running, click Add Files… in the toolbar.

Add Files

Browse to the project's folder and look for the SimpleLibrary.Test.dll assembly (probably located in bin/Debug). The test tree is populated as shown in this screenshot:

 

7.

Press the Start button.

Press Start

See that the test passes.

The Tests Passes

But what does it mean for a test to pass? It means that it was executed, all its assertions were true and no exception was thrown. This is the basic structure of a test in the so called state-based testing: you create one on more objects, call a method and assert over the state of it after doing it.

Congratulations! You have successfully run your first unit test with MbUnit. Let's now focus on some more cases…

内容概要:该研究通过在黑龙江省某示范村进行24小时实地测试,比较了燃煤炉具与自动/手动进料生物质炉具的污染物排放特征。结果显示,生物质炉具相比燃煤炉具显著降低了PM2.5、CO和SO2的排放(自动进料分别降低41.2%、54.3%、40.0%;手动进料降低35.3%、22.1%、20.0%),但NOx排放未降低甚至有所增加。研究还发现,经济性和便利性是影响生物质炉具推广的重要因素。该研究不仅提供了实际排放数据支持,还通过Python代码详细复现了排放特征比较、减排效果计算和结果可视化,进一步探讨了燃料性质、动态排放特征、碳平衡计算以及政策建议。 适合人群:从事环境科学研究的学者、政府环保部门工作人员、能源政策制定者、关注农村能源转型的社会人士。 使用场景及目标:①评估生物质炉具在农村地区的推广潜力;②为政策制定者提供科学依据,优化补贴政策;③帮助研究人员深入了解生物质炉具的排放特征和技术改进方向;④为企业研发更高效的生物质炉具提供参考。 其他说明:该研究通过大量数据分析和模拟,揭示了生物质炉具在实际应用中的优点和挑战,特别是NOx排放增加的问题。研究还提出了多项具体的技术改进方向和政策建议,如优化进料方式、提高热效率、建设本地颗粒厂等,为生物质炉具的广泛推广提供了可行路径。此外,研究还开发了一个智能政策建议生成系统,可以根据不同地区的特征定制化生成政策建议,为农村能源转型提供了有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值