在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
    
    创建一个测试工程。
    打开UnitTest1.cs  它已经为我们生成了一个测试的Sample框架了.
 [TestClass]
[TestClass] public class UnitTest1
    public class UnitTest1 {
    { public UnitTest1()
        public UnitTest1() {
      { //
            // // TODO: Add constructor logic here
            // TODO: Add constructor logic here //
            // }
        }
 /// <summary>
        /// <summary> /// Initialize() is called once during test execution before
        /// Initialize() is called once during test execution before /// test methods in this test class are executed.
        /// test methods in this test class are executed. /// </summary>
        /// </summary> [TestInitialize()]
        [TestInitialize()] public void Initialize()
        public void Initialize() {
      { //  TODO: Add test initialization code
            //  TODO: Add test initialization code }
        }
 /// <summary>
        /// <summary> /// Cleanup() is called once during test execution after
        /// Cleanup() is called once during test execution after /// test methods in this class have executed unless the
        /// test methods in this class have executed unless the /// corresponding Initialize() call threw an exception.
        /// corresponding Initialize() call threw an exception. /// </summary>
        /// </summary> [TestCleanup()]
        [TestCleanup()] public void Cleanup()
        public void Cleanup() {
      { //  TODO: Add test cleanup code
            //  TODO: Add test cleanup code }
        }
 [TestMethod]
        [TestMethod] public void TestMethod1()
        public void TestMethod1() {
      { Assert.IsTrue(true,"this is work");
            Assert.IsTrue(true,"this is work"); }
        } }
    }    熟悉NUnit的一眼就会明白那些Attribute是干嘛用的了。在里面写了一句简单的断言语句。这样就是一个简单的测试实例了。
    接下来运行这个测试实例。找到Test菜单项, 选择"Manage and Execute Tests。在这管理器里面可以选择在项目中已经存在的测试实例。并选择是否运行测试。
选择"Manage and Execute Tests。在这管理器里面可以选择在项目中已经存在的测试实例。并选择是否运行测试。
 
 
    单击"By Test List"右边那上工具栏按钮。就开始执行测试了。查看测试结果 双击可以看到详细信息。
双击可以看到详细信息。
    刚才说到的还可以断点调试。很简单,只需要在测试实例设置断点,然后点击刚才的运行按扭的下拉框,可以看到有“Debug Checked tests”。这样就可以调试了。很方便!
    就这么简单!
 
                    
                 

 
    
 
       
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号