(一)、建立需测试的项目
1、新建项目
2、正常书写.h和.cpp文件
#pragma once
class jiafa
{
public:
int add(int x, int y);
};
#include "标头.h"
#include "iostream"
using namespace std;
int jiafa::add(int x, int y)
{
return x + y;
}
int main()
{
return 0;
}
(二)、新单元测试
1、右击解决方案->添加->新建项目
Visual C++->测试->本机单元测试项目
(三)、添加被测项目的引用
UnitTest1(个人单元测试项目名称,下同)->右击引用->添加引用->勾选被测试项目
(四)、编写测试代码
修改unittest1.cpp
#include "stdafx.h"
#include "CppUnitTest.h"
#include"..\fzf\标头.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTest1
{
TEST_CLASS(UnitTest1)
{
public:
TEST_METHOD(TestMethod1)
{
// TODO: 在此输入测试代码
jiafa a;
int x = 1, y = 2, result = 3;
int real = a.add(x, y);
Assert::AreEqual(real, result);
}
};
}
(五)、运行测试
测试->运行->所有测试