GoogleTest做单元测试

目录

环境准备

bash 复制代码
git clone https://github.com/google/googletest.git

说cmkae版本过低了,解决方法

进到googletest中

bash 复制代码
cmake CMakeLists.txt
make
sudo make install
bash 复制代码
ls /usr/local/lib

存在以下文件说明安装成功

中间出了个问题就是,总是出现链接不成功,导致库导入不进去

可以对G++命令加上-L编译的命令,这样就指定了库的搜索路径。

bash 复制代码
g++ sample1.cc sample1_unittest.cc -o sample1 -L/usr/local/lib -lgtest -lgtest_main -lpthread

GoogleTest

单元测试是用来对一个模块、一个函数或者一个类来进行正确性检测的测试工作。

比如我们测试一个岛问题的解决方法

cpp 复制代码
#include <iostream>
#include <initializer_list>
#include <vector>
#include <gtest/gtest.h>

using namespace std;

class IslandProblem {
public:
    using Matrix = vector<vector<char>>;
    IslandProblem(const initializer_list<vector<char>> list) {
        _islands.assign(list);
    }

    int Do() {
        int num = 0;
        for (int row = 0; row < (int)_islands.size(); row++) {
            for (int col = 0; col < (int)_islands[row].size(); col++) {
                if (canUnion(row, col)) {
                    num++;
                    unionIsland(row, col);
                }
            }
        }
        return num;
    }

protected:
    bool canUnion(int row, int col) {
        if (row < 0 || row >= (int)_islands.size())
            return false;
        if (col < 0 || col >= (int)_islands[row].size())
            return false;
        if (_islands[row][col] != 1)
            return false;
        return true;
    }
    void unionIsland(int row, int col) {
        _islands[row][col] = 2;
        // up
        if (canUnion(row-1, col)) unionIsland(row-1, col);
        // left
        if (canUnion(row, col-1)) unionIsland(row, col-1);
        // down
        if (canUnion(row+1, col)) unionIsland(row+1, col);
        // right
        if (canUnion(row, col+1)) unionIsland(row, col+1);
    }

private:
    Matrix _islands;
};

TEST(IslandProblem, logic) {
    IslandProblem ip1{
        {1,1,1,1},
        {1,0,1,1},
        {0,0,0,0},
        {1,0,1,0}
    };
    EXPECT_EQ(ip1.Do(), 3);

    IslandProblem ip2{
        {1,0,1,1},
        {1,0,1,1},
        {0,0,0,0},
        {1,0,1,0}
    };
    EXPECT_EQ(ip2.Do(), 4);
}

TEST(IslandProblem, boundary) {
    IslandProblem ip1{
        {1,1,1,1},
        {1,0,0,1},
        {1,0,0,1},
        {1,1,1,1}
    };
    EXPECT_EQ(ip1.Do(), 1);
    IslandProblem ip2{
    };
    EXPECT_EQ(ip2.Do(), 0);
}

TEST(IslandProblem, exception) {
    IslandProblem ip1{
        {-1,1,1,1},
        {1,0,0,1},
        {1,0,0,1},
        {1,1,1,1}
    };
    EXPECT_EQ(ip1.Do(), 1);
}

解决方法要考虑:逻辑问题(功能正确),边界问题,异常情况。TEST的测试案例中已经把这些问题考虑进去了。

然后实际去测试,说明解决方法能够考虑以上三种情况

相关推荐
倚肆3 小时前
Spring Boot 日志系统全面详解
spring boot·junit·单元测试
文人sec4 小时前
pytest1-接口自动化测试场景
软件测试·python·单元测试·pytest
2401_861277556 小时前
分层架构系统测试的主要要点
功能测试·系统架构·单元测试·集成测试·模块测试
刘一说1 天前
深入理解 Spring Boot 单元测试:从基础到最佳实践
spring boot·后端·单元测试
草梅友仁1 天前
代码重构与测试覆盖率提升实践 | 2025 年第 46 周草梅周报
单元测试·开源·github
泛联新安2 天前
iUnit7.0重磅发布|AI驱动更智能的单元测试
人工智能·单元测试
安冬的码畜日常2 天前
【JUnit实战3_32】第二十章:用 JUnit 5 进行测试驱动开发(TDD)(上)——将非 TDD 项目改造为 TDD 项目
单元测试·测试驱动开发·tdd·junit5·test-driven
安冬的码畜日常2 天前
【JUnit实战3_33】第二十章:用 JUnit 5 进行测试驱动开发(TDD)(下)——TDD 项目的重构过程及新功能的开发实战
测试工具·junit·单元测试·测试驱动开发·tdd·junit5·test-driven
测试老哥3 天前
软件测试之单元测试知识总结
自动化测试·软件测试·python·测试工具·职场和发展·单元测试·测试用例
m0_565611134 天前
Java高级特性:单元测试、反射、注解、动态代理
java·单元测试·log4j