run in this way,   no why,   only for you heart
CSDN博客(点击进入) CSDN
51CTO(点击进入) 51CTO

.Net 单元测试

很早以前,在学java就了解过单元测试,一直没有具体做过,最近有幸使用C#接触了单元测试,在此记录一下。


1 VS2017新建了一个最简单的web项目,在HomeController添加一个测试方法:

	public int UnitMethodTest(int a, int b)
    {
         int c = a + b;
         return c;
    }

2 添加一个单元测试项目:
在这里插入图片描述
3 单元测试项目包引入待测试项目,在测试方法中测试HomeController中的UnitMethodTest()方法:

	public void TestMethod1()
     {
         System.Diagnostics.Debug.WriteLine("-----单元测试 方法TestMethod1 开始-----");
         HomeController homeController = new HomeController();
         int c = homeController.UnitMethodTest(1,2);
         System.Diagnostics.Debug.WriteLine("测试结果:"+c);
         System.Diagnostics.Debug.WriteLine("-----单元测试 方法TestMethod1 结束-----");
     }

4 单元测试里的System.Diagnostics.Debug.WriteLine()只有在Debug时,会在输出窗口输出相应信息

5 如何运行单元测试?
   5.1 测试资源管理
      在测试资源管理中,单元测试所有项目方法都会展现,可以全部运行/调试,也可以调试单独一个方法。
在这里插入图片描述
   5.2 直接测试单独方法
      选中单元测试中的方法,右击菜单中,选择运行测试/调试测试。

6 Demo演示:
在这里插入图片描述

源码: https://github.com/wangqilong1225/C-Sharp-Test/tree/master/UnitTestDemo

posted @ 2019-07-31 23:14  _小龙人  阅读(89)  评论(0编辑  收藏  举报