阿不

潜水

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  212 随笔 :: 0 文章 :: 3078 评论 :: 107万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

    在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]
    
public class UnitTest1
    
{
        
public UnitTest1()
      
{
            
//
            
// TODO: Add constructor logic here
            
//
        }


        
/// <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
        }


        
/// <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
        }


        [TestMethod]
        
public void TestMethod1()
      
{
            Assert.IsTrue(
true,"this is work");
        }

    }

    熟悉NUnit的一眼就会明白那些Attribute是干嘛用的了。在里面写了一句简单的断言语句。这样就是一个简单的测试实例了。
    接下来运行这个测试实例。找到Test菜单项,选择"Manage and Execute Tests。在这管理器里面可以选择在项目中已经存在的测试实例。并选择是否运行测试。

 
    单击"By Test List"右边那上工具栏按钮。就开始执行测试了。查看测试结果
双击可以看到详细信息。
    刚才说到的还可以断点调试。很简单,只需要在测试实例设置断点,然后点击刚才的运行按扭的下拉框,可以看到有“Debug Checked tests”。这样就可以调试了。很方便!

    就这么简单!

posted on   阿不  阅读(2985)  评论(9编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示