C語言實現(xiàn)單元測試的示例詳解
前沿
單元測試(unit testing),是指對軟件中的最小可測試單元進行檢查和驗證。對于單元測試中單元的含義,一般來說,要根據(jù)實際情況去判定其具體含義,如C語言中單元指一個函數(shù),Java里單元指一個類,圖形化的軟件中可以指一個窗口或一個菜單等??偟膩碚f,單元就是人為規(guī)定的最小的被測功能模塊。單元測試是在軟件開發(fā)過程中要進行的最低級別的測試活動,軟件的獨立單元將在與程序的其他部分相隔離的情況下進行測試。
在網(wǎng)上找了找C語言都沒有類似java 的junit單元測試 ,反復測試自己寫的模塊非常費勁,特別是交叉模塊測試的時候根本就無法弄
因為一個程序只允許一個main方法,如果其他地方存在了,那么就會報錯,這就導致了測完A模塊想要測試B模塊就需要把A模塊測試的相關內(nèi)容刪除,這樣會出現(xiàn)什么問題呢? 如果后期我們對A模塊的內(nèi)容進行了修改,那么是不是需要在重新寫一套測試Demo, 這樣非常浪費時間和精力 ,沒辦法只能自己開發(fā)一套類似的,來協(xié)助本地開發(fā)進行測試代碼
使用前提
自己必須有集合數(shù)據(jù)結(jié)構(gòu)和hash結(jié)構(gòu)
測試框架如下
#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; //方法狀態(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)建一個測試環(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 需要運行的方法 * @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); } /** * 運行指定的測試方法 * @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) } } /** * 運行所有的測試方法,如果方法狀態(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) } } }
測試方法編寫文件
注意: 測試文件的方法名稱不要一樣,否則后面會把前面的給頂替了
#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); }
現(xiàn)在我就能隨心所欲的測試了,想測試那個模塊就測試那個模塊,而且還可批量測試
到此這篇關于C語言實現(xiàn)單元測試的示例詳解的文章就介紹到這了,更多相關C語言單元測試內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
qt獲取當前時間(QDateTime、QTime、QDate)
本文主要介紹了qt獲取當前時間(QDateTime、QTime、QDate),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-04-04Qt中QMapIterator檢測是否為空的實現(xiàn)
本文主要介紹了Qt中QMapIterator檢測是否為空的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-10-10java 中ArrayList與LinkedList性能比較
這篇文章主要介紹了java 中ArrayList與LinkedList性能比較的相關資料,需要的朋友可以參考下2017-03-03