• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • MinUnit – 最小的c語言測試模板

    發表于:2007-07-01來源:作者:點擊數: 標簽:
    MinUnit – 最小的c語言 測試框架 介紹 單元測試 模板在 面向對象 的編程語言中相當流行。像JUnit,SUnit和CppUnit等模板提供相當多的功能??墒?,如此多的功能會使做單元測試的 開發 者(例如使用C語言開發 嵌入式 系統的開發者)進入拘泥的環境。單元測試的

    MinUnit – 最小的c語言測試框架







    介紹

    單元測試模板在面向對象的編程語言中相當流行。像JUnit,SUnit和CppUnit等模板提供相當多的功能??墒?,如此多的功能會使做單元測試的開發者(例如使用C語言開發嵌入式系統的開發者)進入拘泥的環境。單元測試的重點是測試而不是測試模板。MiniUnit是一個極端簡單的C語言單元測試模板。他沒有內存分配,因此可以使用在包括ROMable代碼在內的幾乎任意環境中良好工作。

    源代碼

     /* 文件: minunit.h */ #define mu_assert(message, test) do { if (!(test)) return message; } while (0) #define mu_run_test(test) do { char *message = test(); tests_run++; \                                if (message) return message; } while (0) extern int tests_run;

    它只有三行代碼。

    安裝一個測試實例

    一個MinUnit測試實例是一個通過測試則返回0(NULL)的函數。如果測試失敗,這個函數應該返回一個測試失敗的描述字符串。mu_assert 是一個當表達式失敗時返回字符串的宏。而宏mu_runtest重復調用測試實例直到測試實例失敗。這就是MinUnit的全部。

    例子

    下面的例子運行兩個測試,一個成功,一個失敗。

     /* 文件 minunit_example.c */  #include <stdio.h> #include "minunit.h"  int tests_run = 0;  int foo = 7; int bar = 4;  static char * test_foo() {     mu_assert("error, foo != 7", foo == 7);     return 0; }  static char * test_bar() {     mu_assert("error, bar != 5", bar == 5);     return 0; }  static char * all_tests() {     mu_run_test(test_foo);     mu_run_test(test_bar);     return 0; }  int main(int argc, char **argv) {     char *result = all_tests();     if (result != 0) {         printf("%s\n", result);     }     else {         printf("ALL TESTS PASSED\n");     }     printf("Tests run: %d\n", tests_run);      return result != 0; }

    結論

    人們一般認為寫一個單元測試模板會很復雜。但實際上你僅僅需要寫幾行代碼。當然如果你接觸如JUnit一樣的全功能的單元測試模板,那你應該用所有的手段使用它。如果你沒有,你仍然可以使用像MinUnit一樣簡單的模板,否則你的時間將會飛逝。這不是放棄單元測試的理由。

    許可

    你可以在任意目的下使用本文的代碼而不需要任何的理由。

     


    原文轉自:http://www.kjueaiud.com

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>