C語言實(shí)現(xiàn)單元測(cè)試的示例詳解
前沿
單元測(cè)試(unit testing),是指對(duì)軟件中的最小可測(cè)試單元進(jìn)行檢查和驗(yàn)證。對(duì)于單元測(cè)試中單元的含義,一般來說,要根據(jù)實(shí)際情況去判定其具體含義,如C語言中單元指一個(gè)函數(shù),Java里單元指一個(gè)類,圖形化的軟件中可以指一個(gè)窗口或一個(gè)菜單等。總的來說,單元就是人為規(guī)定的最小的被測(cè)功能模塊。單元測(cè)試是在軟件開發(fā)過程中要進(jìn)行的最低級(jí)別的測(cè)試活動(dòng),軟件的獨(dú)立單元將在與程序的其他部分相隔離的情況下進(jìn)行測(cè)試。
在網(wǎng)上找了找C語言都沒有類似java 的junit單元測(cè)試 ,反復(fù)測(cè)試自己寫的模塊非常費(fèi)勁,特別是交叉模塊測(cè)試的時(shí)候根本就無法弄
因?yàn)橐粋€(gè)程序只允許一個(gè)main方法,如果其他地方存在了,那么就會(huì)報(bào)錯(cuò),這就導(dǎo)致了測(cè)完A模塊想要測(cè)試B模塊就需要把A模塊測(cè)試的相關(guān)內(nèi)容刪除,這樣會(huì)出現(xiàn)什么問題呢? 如果后期我們對(duì)A模塊的內(nèi)容進(jìn)行了修改,那么是不是需要在重新寫一套測(cè)試Demo, 這樣非常浪費(fèi)時(shí)間和精力 ,沒辦法只能自己開發(fā)一套類似的,來協(xié)助本地開發(fā)進(jìn)行測(cè)試代碼
使用前提
自己必須有集合數(shù)據(jù)結(jié)構(gòu)和hash結(jié)構(gòu)
測(cè)試框架如下
#ifndef STUDY_TESTCORE_H
#define STUDY_TESTCORE_H
#include "../structure/charHash.h"
typedef int boolean;//定義一個(gè)布爾類型
#define TRUE 1
#define FALSE 0
#define EXECUTE_TEST_RUN_METHOD(method) void (*testMethod)() = method;testMethod();
typedef struct testCore{
HashMap *methodAll; // 方法集合
HashMap *methodAllState; //方法狀態(tài)
} 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;
//創(chuàng)建一個(gè)測(cè)試環(huán)境
TestCore *createTestCore() {
TestCore *testCore = (TestCore *) malloc(sizeof(TestCore));
testCore->methodAll = createHashMap(200);
testCore->methodAllState = createHashMap(200);
return testCore;
}
/**
*
* @param hash
* @param methodName 方法名
* @param method 需要運(yùn)行的方法
* @param state 方法狀態(tài)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);
}
/**
* 運(yùn)行指定的測(cè)試方法
* @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)
}
}
/**
* 運(yùn)行所有的測(cè)試方法,如果方法狀態(tài)為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)
}
}
}測(cè)試方法編寫文件
注意: 測(cè)試文件的方法名稱不要一樣,否則后面會(huì)把前面的給頂替了
#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);
}驗(yàn)證
#include <stdio.h>
#include "unittest/testcore.h"
#include "unittest/testmethod.h"
int main() {
printf("==============test_Run_MethodAll=============\n");
//批量測(cè)試
test_Run_MethodAll(initMethod);
printf("==============test_Run_Method=============\n");
//點(diǎn)對(duì)點(diǎn)測(cè)試
test_Run_Method("test_1",initMethod);
return (0);
}
現(xiàn)在我就能隨心所欲的測(cè)試了,想測(cè)試那個(gè)模塊就測(cè)試那個(gè)模塊,而且還可批量測(cè)試
到此這篇關(guān)于C語言實(shí)現(xiàn)單元測(cè)試的示例詳解的文章就介紹到這了,更多相關(guān)C語言單元測(cè)試內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用UDP協(xié)議實(shí)現(xiàn)單詞翻譯服務(wù)器
這篇文章主要為大家詳細(xì)介紹了如何使用UDP協(xié)議實(shí)現(xiàn)英文單詞翻譯服務(wù)器,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解下2023-08-08
ubuntu系統(tǒng)下C++調(diào)用matlab程序的方法詳解
學(xué)習(xí)c++與matlab混合編程一般是通過c++調(diào)用matlab函數(shù),因?yàn)閙atlab具有強(qiáng)大的數(shù)學(xué)函數(shù)庫,然而vc++具有界面設(shè)計(jì)靈活的優(yōu)點(diǎn),下面這篇文章主要給大家介紹了關(guān)于在ubuntu系統(tǒng)下C++調(diào)用matlab程序的方法,需要的朋友可以參考下。2017-08-08
win10系統(tǒng)下?VS2019點(diǎn)云庫PCL1.12.0的安裝與配置教程
點(diǎn)云庫全稱是Point?Cloud?Library(PCL),是一個(gè)獨(dú)立的、大規(guī)模的、開放的2D/3D圖像和點(diǎn)云處理項(xiàng)目,這篇文章主要介紹了win10系統(tǒng)下?VS2019點(diǎn)云庫PCL1.12.0的安裝與配置,需要的朋友可以參考下2022-07-07
VS2022設(shè)置編碼方式為utf-8的三種方式小結(jié)
本文主要介紹了VS2022設(shè)置編碼方式為utf-8的三種方式小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-09-09
C++ LeetCode1812判斷國(guó)際象棋棋盤格子顏色
這篇文章主要為大家介紹了C++ LeetCode1812判斷國(guó)際象棋棋盤格子顏色, 有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12

