zoukankan      html  css  js  c++  java
  • VS2015做单元测试

    1.安装测试插件

    2.新建测试用例

    这里就用课堂练习找水王   作例子

    写一个类waterKing.h和waterKing.cpp

     1 //idList.h
     2 #pragma once
     3 #include<iostream>
     4 using namespace std;
     5 class idList
     6 {
     7 private:
     8     int id[1000];        //发帖ID
     9     int waterKing;      //当前水王
    10     int value = 0;      //当前水王水的可能性
    11     int num;            //帖子数
    12 public:
    13     idList(int num,int id[]);
    14     ~idList();
    15     int searchwaterKing();
    16 };
      1 //idList.cpp
      2 #include "idList.h"
      3 
      4 idList::idList(int num, int id[])
      5 {
      6     if (num <= 0)
      7     {
      8         cout << "非法的输入";
      9         exit(1);
     10     }
     11     this->num = num;
     12     for (int i = 0; i < num; i++) {
     13         this->id[i] = id[i];
     14     }
     15 }
     16 
     17 
     18 idList::~idList()
     19 {
     20 }
     21 
     22 int idList::searchwaterKing()     //返回-1代表没有水王,否则返回水王的ID
     23 {
     24     if (num % 2)    //奇数
     25     {
     26         for (int i = 1; i < num; i += 2)
     27         {
     28             if (value > 0)     //当前水王存在
     29             {
     30                 if (id[i] == id[i + 1])
     31                 {
     32                     if (id[i] == waterKing)
     33                     {
     34                         value += 2;
     35                     }
     36                     else
     37                     {
     38                         value -= 2;
     39                     }
     40                 }
     41             }
     42             else             //当前水王不存在
     43             {
     44                 if (id[i] == id[i + 1])
     45                 {
     46                     waterKing = id[i];
     47                     value += 2;
     48                 }
     49             }
     50         }
     51 
     52         if (value > 0)
     53         {
     54             if (id[0] == waterKing)
     55             {
     56                 value++;
     57             }
     58             else
     59             {
     60                 value--;
     61             }
     62         }
     63         else
     64         {
     65             waterKing = id[0];
     66             value = 1;
     67         }
     68 
     69     }
     70     else         //偶数
     71     {
     72         for (int i = 0; i < num; i += 2)
     73         {
     74             if (value > 0)     //水王存在
     75             {
     76                 if (id[i] == id[i + 1])
     77                 {
     78                     if (id[i] == waterKing)
     79                     {
     80                         value += 2;
     81                     }
     82                     else
     83                     {
     84                         value -= 2;
     85                     }
     86                 }
     87             }
     88             else            //水王不存在
     89             {
     90                 if (id[i] == id[i + 1])
     91                 {
     92                     waterKing = id[i];
     93                     value += 2;
     94                 }
     95             }
     96         }
     97     }
     98     if (value > 0)
     99     {
    100         return waterKing;
    101     }
    102     else
    103     {
    104         return -1;
    105     }
    106 }

    写完代码之后记得编译一下,会在项目DEBUG目录下生成OBJ文件

    3. 右击解决方案->"添加"->"新建项目"->"测试"->"托管测试项目"

    新建之后再解决方案下就会出现一个刚刚新建的测试项目

    在UnitTest.cpp下添上引用

    在下面的[TestMethod]里编写自己的测试代码

     1      [TestMethod]
     2         void TestMethod1()
     3         {
     4             int list[] = {1};
     5             idList test1(sizeof(list)/sizeof(int), list);
     6             int result = test1.searchwaterKing();
     7             Assert::AreEqual(result, 1);
     8         };
     9 
    10         [TestMethod]
    11         void TestMethod2()
    12         {
    13             int list[] = { 1,2,1 };
    14             idList test1(sizeof(list) / sizeof(int), list);
    15             int result = test1.searchwaterKing();
    16             Assert::AreEqual(result, 1);
    17         };
    18 
    19         [TestMethod]
    20         void TestMethod3()
    21         {
    22             int list[] = { 1,1,1,2,3 };
    23             idList test1(sizeof(list) / sizeof(int), list);
    24             int result = test1.searchwaterKing();
    25             Assert::AreEqual(result, 1);
    26         };
    27 
    28         [TestMethod]
    29         void TestMethod4()   //该测试应该是不相等的
    30         {
    31             int list[] = { 1,1,2,3 };
    32             idList test1(sizeof(list) / sizeof(int), list);
    33             int result = test1.searchwaterKing();
    34             Assert::AreNotEqual(result, -1);
    35         };
    36 
    37         [TestMethod]
    38         void TestMethod5()   //该测试应该是不相等的
    39         {
    40             int list[] = { 1,2,3,4,5 };
    41             idList test1(sizeof(list) / sizeof(int), list);
    42             int result = test1.searchwaterKing();
    43             Assert::AreNotEqual(result, -1);
    44         };

    右击测试项目->属性->链接器->输入->附加依赖项->编辑

    添加自己刚刚编译生成的OBJ

    然后就可以点击菜单栏的测试菜单,进行相关测试了

               

    Assert::XXX的用法不是很了解,网上一些.NET的测试教程很多,

    原文链接 http://www.cnblogs.com/xiaoyongwu/p/5289964.html

  • 相关阅读:
    LeetCode:230. 二叉搜索树中第K小的元素
    LeetCode:229. 求众数 II
    LeetCode:228. 汇总区间
    LeetCode:225. 用队列实现栈
    LeetCode:209. 长度最小的子数组
    LeetCode:208. 实现 Trie (前缀树)
    疯狂的订餐系统-软件需求分析挑战之旅 【转】
    产品需求文档(PRD)的写作 【转】
    软件需求文档标准格式
    通过实例编写开发规范文档 【转】
  • 原文地址:https://www.cnblogs.com/xiaoxt/p/5511005.html
Copyright © 2011-2022 走看看