vs2015 单元测试 linux,VS2015做单元测试
1.安裝測試插件
2.新建測試用例
這里就用課堂練習找水王? 作例子
寫一個類waterKing.h和waterKing.cpp
1 //idList.h
2 #pragma once
3 #include
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的測試教程很多,
來源:https://www.cnblogs.com/xiaoxt/p/5511005.html
總結
以上是生活随笔為你收集整理的vs2015 单元测试 linux,VS2015做单元测试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux中VI中看时间,Linux中v
- 下一篇: linux的 vi编辑器在哪,Linux