一直想有一种工具,可以对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 theSimpleLibrary
namespace. We imported theMbUnit.Framework
namespace. We made theFibonacciTest
class public. You may need to explicitly do so in your language of choice. We added a new public method calledFibonacci_of_number_greater_than_one
, which doesn't return a value. We decorated theFibonacci_of_number_greater_than_one
method with the[Test]
attribute. We added a call to theAssert.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 toFibonacci.Calculate(6)
is equal to 8. TheAssert
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 Menu
→All Programs
→Gallio
→Icarus GUI Test Runner
6.
7.
Press the Start button.
See that the test 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…