*************
c++
topic: gtest 单元测试
*************
Taking a gtest is a necessary step before you upload your codes. The gtest can easily tells you wether your code realize the functions.
The basic form of gtest is like as follow.
#include <gtest/gtest.h>
// 测试套件(Test Suite)声明
TEST(TestSuiteName, TestName)
{
// 测试代码
EXPECT_EQ(expected_value, actual_value); // 断言
}
I wrote a function to calculate the real power of the system in power_calculator.h. This is a head file.
// power_calculator.h
double CalculatePower(double voltage, double current)
{
return voltage * current;
}
feed data to the program.
// power_calculator_test.cpp
#include <gtest/gtest.h>
#include "power_calculator.h"
// 测试用例:正常情况下的功率计算
TEST(PowerCalculatorTest, NormalCase)
{
//给程序喂数据
double voltage = 10.0;
double current = 5.0;
// 期待的值
double expected_power = 50.0;
// 调用函数并计算结果
double actual_power = CalculatePower(voltage, current);
// 比较计算结果和期待的值
EXPECT_EQ(expected_power, actual_power);
}