在Vs2005中加入了单元测试工具,使用与NUnit差不多。但功能更加丰富了,而且使用更加方便,有利于项目的协调工作。而且还支持调试测试,(不知道NUnit支不支持,我是没用过。)方便我们调试出错代码。
Vs2005 Test tool 与NUnit 特性类对应
NUnit Vs2005 Tools
NUnit.Framework Microsoft.VisualStudio.QualityTools.UnitTesting.Framework
TestFixtureAttribute TestClassAttribute
TestFixtureSetUpAttribute ClassInitializeAttribute
TestFixtureTearDownAttribute ClassCleanupAttribute
SetUpAttribute TestInitializeAttribute
TearDownAttribute TestCleanupAttribute
创建一个测试工程。![](https://www.cnblogs.com/images/cnblogs_com/hjf1223/testproject.jpg)
打开UnitTest1.cs 它已经为我们生成了一个测试的Sample框架了.
[TestClass]
public class UnitTest1
{
public UnitTest1()
{
//
// TODO: Add constructor logic here
//
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
/// <summary>
/// Initialize() is called once during test execution before
/// test methods in this test class are executed.
/// </summary>
[TestInitialize()]
public void Initialize()
{
// TODO: Add test initialization code
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
/// <summary>
/// Cleanup() is called once during test execution after
/// test methods in this class have executed unless the
/// corresponding Initialize() call threw an exception.
/// </summary>
[TestCleanup()]
public void Cleanup()
{
// TODO: Add test cleanup code
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[TestMethod]
public void TestMethod1()
{
Assert.IsTrue(true,"this is work");
}
}
Vs2005 Test tool 与NUnit 特性类对应
NUnit Vs2005 Tools
NUnit.Framework Microsoft.VisualStudio.QualityTools.UnitTesting.Framework
TestFixtureAttribute TestClassAttribute
TestFixtureSetUpAttribute ClassInitializeAttribute
TestFixtureTearDownAttribute ClassCleanupAttribute
SetUpAttribute TestInitializeAttribute
TearDownAttribute TestCleanupAttribute
创建一个测试工程。
![](https://www.cnblogs.com/images/cnblogs_com/hjf1223/testproject.jpg)
打开UnitTest1.cs 它已经为我们生成了一个测试的Sample框架了.
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
熟悉NUnit的一眼就会明白那些Attribute是干嘛用的了。在里面写了一句简单的断言语句。这样就是一个简单的测试实例了。
接下来运行这个测试实例。找到Test菜单项,选择"Manage and Execute Tests。在这管理器里面可以选择在项目中已经存在的测试实例。并选择是否运行测试。
单击"By Test List"右边那上工具栏按钮。就开始执行测试了。查看测试结果双击可以看到详细信息。
刚才说到的还可以断点调试。很简单,只需要在测试实例设置断点,然后点击刚才的运行按扭的下拉框,可以看到有“Debug Checked tests”。这样就可以调试了。很方便!
就这么简单!