当前位置 : 主页 > 编程语言 > c语言 >

C语言实现单元测试的示例详解

来源:互联网 收集:自由互联 发布时间:2023-02-01
目录 前沿 使用前提 测试框架如下 测试方法编写文件 验证 前沿 单元测试(unit testing),是指对软件中的最小可测试单元进行检查和验证。对于单元测试中单元的含义,一般来说,要根
目录
  • 前沿
  • 使用前提
  • 测试框架如下
  • 测试方法编写文件
  • 验证

前沿

单元测试(unit testing),是指对软件中的最小可测试单元进行检查和验证。对于单元测试中单元的含义,一般来说,要根据实际情况去判定其具体含义,如C语言中单元指一个函数,Java里单元指一个类,图形化的软件中可以指一个窗口或一个菜单等。总的来说,单元就是人为规定的最小的被测功能模块。单元测试是在软件开发过程中要进行的最低级别的测试活动,软件的独立单元将在与程序的其他部分相隔离的情况下进行测试。

在网上找了找C语言都没有类似java 的junit单元测试 ,反复测试自己写的模块非常费劲,特别是交叉模块测试的时候根本就无法弄

因为一个程序只允许一个main方法,如果其他地方存在了,那么就会报错,这就导致了测完A模块想要测试B模块就需要把A模块测试的相关内容删除,这样会出现什么问题呢? 如果后期我们对A模块的内容进行了修改,那么是不是需要在重新写一套测试Demo, 这样非常浪费时间和精力 ,没办法只能自己开发一套类似的,来协助本地开发进行测试代码

使用前提

自己必须有集合数据结构和hash结构

测试框架如下

#ifndef STUDY_TESTCORE_H
#define STUDY_TESTCORE_H
#include "../structure/charHash.h"
typedef int boolean;//定义一个布尔类型
#define TRUE 1
#define FALSE 0
#define EXECUTE_TEST_RUN_METHOD(method) void (*testMethod)() = method;testMethod();
typedef  struct testCore{
    HashMap *methodAll;      // 方法集合
    HashMap *methodAllState; //方法状态

} TestCore;
extern  TestCore *testCore;
void   test_Run_MethodAll(void (*initMethod)());
void test_Run_Method(char *methodName,void (*initMethod)());
void addTestMethodName( char *methodName,void *method,boolean state) ;

#endif //STUDY_TESTCORE_H
#include "testcore.h"
#include "malloc.h"
#include "string.h"
TestCore *testCore = NULL;
//创建一个测试环境
TestCore *createTestCore() {
    TestCore *testCore = (TestCore *) malloc(sizeof(TestCore));
    testCore->methodAll = createHashMap(200);
    testCore->methodAllState = createHashMap(200);
    return testCore;
}
/**
 *
 * @param hash
 * @param methodName   方法名
 * @param method     需要运行的方法
 * @param state     方法状态TRUE启用 FALSE禁用
 */
void addTestMethodName( char *methodName,void *method,boolean state) {
    if(testCore == NULL) {
        testCore = createTestCore();
    }
    putHashMap(testCore->methodAll, methodName,method);
    putHashMap(testCore->methodAllState, methodName,state);
}


/**
 * 运行指定的测试方法
 * @param methodName
 * @param initMethod
 */
void test_Run_Method(char *methodName,void (*initMethod)()) {
    initMethod();//初始化方法
    void *method = getHashMap(testCore->methodAll, methodName);
    if(method != NULL) {
        EXECUTE_TEST_RUN_METHOD(method)
    }
}
/**
 * 运行所有的测试方法,如果方法状态为FALSE则跳过
 * @param initMethod
 */
void   test_Run_MethodAll(void (*initMethod)()) {
    initMethod();//初始化方法
    HashMapIterator *pIterator = createHashMapIterator(testCore->methodAllState);
    while(hasNextHashMapIterator(pIterator)) {
        CharKvLinkedNode *pNode = nextHashMapIterator(pIterator);
       if(pNode->value == (void *)TRUE) {
           void *method = getHashMap(testCore->methodAll, pNode->key);
           EXECUTE_TEST_RUN_METHOD(method)
       }
    }
}

测试方法编写文件

注意: 测试文件的方法名称不要一样,否则后面会把前面的给顶替了

#ifndef STUDY_TESTMETHOD_H
#define STUDY_TESTMETHOD_H
#include "testcore.h"
void initMethod();
#endif //STUDY_TESTMETHOD_H
#include <stdio.h>
#include "testmethod.h"

void  test_1(){
    printf("test_1\n");
}
void  test_2(){
    printf("test_2\n");
}
void initMethod(){
    addTestMethodName("test_1",test_1,TRUE);
    addTestMethodName("test_2",test_2,TRUE);
}

验证

#include <stdio.h>
#include "unittest/testcore.h"
#include "unittest/testmethod.h"
int main() {
    printf("==============test_Run_MethodAll=============\n");
    //批量测试
    test_Run_MethodAll(initMethod);
    printf("==============test_Run_Method=============\n");
    //点对点测试
    test_Run_Method("test_1",initMethod);
    return (0);
}

现在我就能随心所欲的测试了,想测试那个模块就测试那个模块,而且还可批量测试

到此这篇关于C语言实现单元测试的示例详解的文章就介绍到这了,更多相关C语言单元测试内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

上一篇:C++movesemantic移动语义介绍
下一篇:没有了
网友评论