一. 选择开发工具
开发工具我们选择的是visual Studio,并使用c++进行开发,我使用的是visual Studio2019社区版。
二. 练习自动单元测试
我使用的测试工具是VSTS,测试步骤如下:
1 编写代码,
我写的是简单的求解最大公约数程序,代码如下
#include <iostream>
using namespace std;
int yue(int a,int b) {
int t;
if (a < b){
t = a;
a = b;
b = t;
}
while (b != 0){
t = a % b;
a = b;
b = t;
}
return a;
}
int main() {
int x, y;
cin >> x >> y;
int z = yue(x, y);
cout << z;
return 0;
}
头文件:
#pragma once
int Yue(int a, int b) {
int t;
if (a < b) {
t = a;
a = b;
b = t;
}
while (b != 0) {
t = a % b;
a = b;
b = t;
}
return a;
}
2 创建测试程序
- 右键解决方案 -> 添加 -> 新建项目 ->测试 -> 本机单元测试项目
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTest2
{
TEST_CLASS(UnitTest2)
{
public:
TEST_METHOD(TestMethod1)
{
int x = 1, y = 1;
int z = Yue(x, y);
Assert::AreEqual(1, z);
}
TEST_METHOD(TestMethod2)
{
int x = 2, y = 1;
int z = Yue(x, y);
Assert::AreEqual(1, z);
}
TEST_METHOD(TestMethod3)
{
int x = 1, y = 2;
int z = Yue(x, y);
Assert::AreEqual(1, z);
}
};
}
<table>
<tr>
<th>测试样例1</th>
<th>测试样例2二</th>
<th>测试样例3</th>
</tr>
<tr>
<td>两数相等</td>
<td>前数大于后数</td>
<td>后数大于前数</td>
</tr>
</table>
测试结果如下:
<center>

测试结果
</center>
可见测试结果均为正确。