一、测试环境
本次作业我采用的是Visual Studio 2017开发工具,之前便已安装使用过,此次针对使用C++语言编写的项目进行单元测试
![]()
二、新建项目
本程序是一个简单的减法运算,代码如下:
#include "hanfeng.h"
#include <iostream>
using namespace std;
int jianfa::sub(int x, int y) {
return x - y;
}
int main()
{
return 0;
}
原有项目下书写.h头文件,代码如下:
#pragma once
class jianfa {
public:
int sub(int x, int y);
};
三、单元测试
添加本机单元测试新项目进行测试,截图如下:
![]()
之后添加引用
![]()
对测试代码进行修改:
#include "stdafx.h"
#include "CppUnitTest.h"
#include"..\Project1\hanfeng.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTest1
{
TEST_CLASS(UnitTest1)
{
public:
TEST_METHOD(TestMethod1)
{
jianfa a;
int x = 2, y = 1, result = 1;
int real = a.sub(x, y);
Assert::AreEqual(real, result);
}
};
}
通过测试
![]()