天天看點

Unity(純C語言單元測試架構!不是那個Unity3d)配置指南Unity配置指南以下是中英對照版Unity Configuration Guide

譯者注:譯者部落格(http://blog.csdn.net/lin_strong),轉載請保留這條。此為Unity手冊的翻譯,僅供學習交流使用,請勿用于商業用途。

翻譯的資料是公開的,在docs/UnityConfigurationGuide.md,我想應該不會有什麼版權問題,如涉及版權問題,請聯系我删除文章。

文章目錄

  • Unity配置指南
    • C 标準、編譯器和微控制器
      • 使用這些選項
    • 選項
      • 整數類型
          • `UNITY_EXCLUDE_STDINT_H`
          • `UNITY_EXCLUDE_LIMITS_H`
          • `UNITY_INT_WIDTH`
          • `UNITY_LONG_WIDTH`
          • `UNITY_POINTER_WIDTH`
          • `UNITY_SUPPORT_64`
      • 浮點數類型
          • `UNITY_INCLUDE_FLOAT`
          • `UNITY_EXCLUDE_FLOAT`
          • `UNITY_INCLUDE_DOUBLE`
          • `UNITY_EXCLUDE_DOUBLE`
          • `UNITY_EXCLUDE_FLOAT_PRINT`
          • `UNITY_FLOAT_TYPE`
          • `UNITY_DOUBLE_TYPE`
          • `UNITY_FLOAT_PRECISION`
          • `UNITY_DOUBLE_PRECISION`
      • 雜項
          • `UNITY_EXCLUDE_STDDEF_H`
      • 工具集定制
          • `UNITY_OUTPUT_CHAR(a)`
          • `UNITY_OUTPUT_FLUSH()`
          • `UNITY_OUTPUT_START()`
          • `UNITY_OUTPUT_COMPLETE()`
          • `UNITY_WEAK_ATTRIBUTE`
          • `UNITY_WEAK_PRAGMA`
          • `UNITY_NO_WEAK`
          • `UNITY_PTR_ATTRIBUTE`
          • `UNITY_PRINT_EOL`
          • `UNITY_EXCLUDE_DETAILS`
          • `UNITY_EXCLUDE_SETJMP`
          • `UNITY_OUTPUT_COLOR`
    • 深入虎穴
          • `main()`
          • `RUN_TEST`
    • 愉快的移植
  • 以下是中英對照版
  • Unity Configuration Guide
    • C Standards, Compilers and Microcontrollers
      • Using These Options
    • The Options
      • Integer Types
          • `UNITY_EXCLUDE_STDINT_H`
          • `UNITY_EXCLUDE_LIMITS_H`
          • `UNITY_INT_WIDTH`
          • `UNITY_LONG_WIDTH`
          • `UNITY_POINTER_WIDTH`
          • `UNITY_SUPPORT_64`
      • Floating Point Types
          • `UNITY_INCLUDE_FLOAT`
          • `UNITY_EXCLUDE_FLOAT`
          • `UNITY_INCLUDE_DOUBLE`
          • `UNITY_EXCLUDE_DOUBLE`
          • `UNITY_EXCLUDE_FLOAT_PRINT`
          • `UNITY_FLOAT_TYPE`
          • `UNITY_DOUBLE_TYPE`
          • `UNITY_FLOAT_PRECISION`
          • `UNITY_DOUBLE_PRECISION`
      • Miscellaneous
          • `UNITY_EXCLUDE_STDDEF_H`
      • Toolset Customization
          • `UNITY_OUTPUT_CHAR(a)`
          • `UNITY_OUTPUT_FLUSH()`
          • `UNITY_OUTPUT_START()`
          • `UNITY_OUTPUT_COMPLETE()`
          • `UNITY_WEAK_ATTRIBUTE`
          • `UNITY_WEAK_PRAGMA`
          • `UNITY_NO_WEAK`
          • `UNITY_PTR_ATTRIBUTE`
          • `UNITY_PRINT_EOL`
          • `UNITY_EXCLUDE_DETAILS`
          • `UNITY_EXCLUDE_SETJMP`
          • `UNITY_OUTPUT_COLOR`
    • Getting Into The Guts
          • `main()`
          • `RUN_TEST`
    • Happy Porting

Unity配置指南

C 标準、編譯器和微控制器

嵌入式軟體世界擁有自己的挑戰。編譯器支援不同修訂版的C标準。它們忽視一些要求,這有

時在會使得語言更有用。有時是為了簡化它們的支援。有時是由于它們目标微控制器的特殊性。

模拟器們則使事情更加複雜了。

Unity被設計為可運作在幾乎任何由C編譯器支援的東西上。要是不需要任何配置就能實作這個

目标的話就太棒了。盡管在有些目标上已經很接近這個夢想了,但遺憾的是并不都可以。很可

能你至少需要修改一些在這篇文檔中描述的配置選項。

所有的Unity配置選項都是

#defines

。其中大部分都是簡單的定義。一些是帶有參數的宏。

它們在unity_internals.h頭檔案中。除非你真的需要,其實不必打開那個檔案。那個檔案

就是跨平台庫十分難搭建的一個證明。往好了想,它也證明了許多複雜性可以集中在一個地

方以使得其他地方更加一緻和簡單。

使用這些選項

你是在使用一個目标特定的編譯器和一個模拟器或者一個本地編譯器并沒有什麼差别。不管在

哪種情況下,你都有許多選擇以配置這些選項:

  1. 因為這些選項是通過C定義來指定的,你可以通過指令行傳遞大部分這些選項給你的編譯器。

    即使你在使用一個強制你使用它們IDE來進行所有配置的嵌入式目标,那也總會有一個地方可以

    配置你項目的編譯器的定義。

  2. 你可以建立一個定制的

    unity_config.h

    配置檔案(放在你工具鍊的搜尋路徑中)。在這個檔案

    中,列出特定于你的目标的定義和宏。你唯一要做的就是定義

    UNITY_INCLUDE_CONFIG_H

    ,然後,

    Unity會在

    unity_config.h

    中進一步尋找它可能需要的定義。

選項

整數類型

如果你做了很久的C開發者,你很可能已經知道C中整型值會随着目标平台而變化。C标準的

規矩是,

int

比對目标微處理器的寄存器大小,以及

int

大小與其他整型大小的關系。一

個目标平台上

int

可能是16位的,到了另一個上可能就成64位的了。編譯器中有更多遵從

C99以及之後的标準的特定類型,但當然不是每個編譯器都這樣。是以,Unity有許多特性能

幫助調整自己以滿足你需要的整型大小。它最開始會嘗試自動完成這事。

UNITY_EXCLUDE_STDINT_H

Unity猜測你的類型做的頭件事是檢查

stdint.h

。這個檔案會包含定義如

UINT_MAX

Unity可以用其來了解你的系統。可能你不想要它做這件事(昂,為什麼?)或者(更可能)

是因為你的系統不支援

stdint.h

。如果是這樣的話,你應該定義這個宏。這樣,

Unity就知道要跳過這個檔案,而你也不會得到編譯錯誤了。

示例:

#define UNITY_EXCLUDE_STDINT_H
           

UNITY_EXCLUDE_LIMITS_H

做的第二件事是檢查

limits.h

。一些不支援

stdint.h

的編譯器會補償你個

limits.h

如果你同樣不想讓Unity檢查這個檔案,定義這個宏以使Unity跳過這個包含。

示例:

#define UNITY_EXCLUDE_LIMITS_H
           

如果你把上面兩個功能都禁用了,那你就得自己進行配置了。别擔心。即使這樣也沒那麼糟…

如果你不喜歡預設配置的話,你隻需要定義幾個東西就好了。

UNITY_INT_WIDTH

定義這個為你系統上

int

的比特數。預設為32位。

示例:

#define UNITY_INT_WIDTH 16
           

UNITY_LONG_WIDTH

定義這個為你系統上

long

的比特數。預設為32位。這被用于表明你的系統支不支援64位。

是需要指定一個

long

還是

long long

來獲得64位值。在16位系統上,這個選項應該忽略。

示例:

#define UNITY_LONG_WIDTH 16
           

UNITY_POINTER_WIDTH

定義這個為你系統上一個指針的比特數。預設為32位。如果你編譯器警告說對指針進行了

強制類型轉換,那你要看看這個。

示例:

#define UNITY_POINTER_WIDTH 64
           

UNITY_SUPPORT_64

Unity如果自動探測到了系統的64位支援,或者如果你的

int

long

或指針寬度大于32位

的話,它會自動包含64位支援。如果其他選項都沒幫你完成這事的話,你可以定義這個宏以

啟用64位支援。在小目标上啟用64位支援可能會對大小和速度有重大影響,是以如果你不需要

的話,不要定義它。

示例:

#define UNITY_SUPPORT_64
           

浮點數類型

在嵌入式世界中,某個目标一點都不支援浮點數運算或者隻支援單精度一點都不稀奇。我們

能夠猜測整型的大小是因為總是有整型的,起碼有一個位元組的整型。另一方面,浮點數有時

根本就沒有。在這些平台上嘗試include

float.h

會導緻一個錯誤。這導緻隻能人工配置。

UNITY_INCLUDE_FLOAT

UNITY_EXCLUDE_FLOAT

UNITY_INCLUDE_DOUBLE

UNITY_EXCLUDE_DOUBLE

預設的,Unity猜測你想要單精度浮點數支援,但是不想要雙精度的。你可以使用這兒的

include和exclude選項來改變這個行為。你可以按你需要include這兩個、其中一個、或

都不要。對于啟用了的精度,以下浮點數選項也可以用。

示例:

// 這是多奇怪的處理器?
    #define UNITY_EXCLUDE_FLOAT
    #define UNITY_INCLUDE_DOUBLE
           

UNITY_EXCLUDE_FLOAT_PRINT

Unity的目标是盡可能小的開銷,它盡可能避免調用标準庫(一些嵌入式平台沒有标準庫!)。

是以,它用于列印整型值的例程是極簡寫死的。是以,測試失敗時是否展示浮點數值是可選

的。預設地,Unity會在斷言失敗時列印浮點數的真實值(比如”Expected 4.56 Was 4.68”)。

不想要這個額外支援的話,你可以使用這個宏定義來把斷言失敗消息替換為像

”Values Not Within Delta”。如果你想要更啰嗦的浮點數斷言失敗消息,使用這個選項來給出

更加正确的故障消息。

示例:

#define UNITY_EXCLUDE_FLOAT_PRINT
           

UNITY_FLOAT_TYPE

如果啟用了浮點數支援,Unity假設你想要你的

FLOAT

斷言比較的是标準C float。如果你的

編譯器支援特殊的浮點數類型,你可以通過使用這個定義重寫這個行為。

示例:

#define UNITY_FLOAT_TYPE float16_t
           

UNITY_DOUBLE_TYPE

如果啟用了浮點數支援,Unity假設你想要你的

DOUBLE

斷言比較的是标準C double。

如果你想要改變它,你可以使用這個選項指定其他東西。比如,定義

UNITY_DOUBLE_TYPE

long double

可以在你的64位處理器上啟用巨浮點數類型以替代标準的

double

示例:

#define UNITY_DOUBLE_TYPE long double
           

UNITY_FLOAT_PRECISION

UNITY_DOUBLE_PRECISION

如果你在Unity斷言指南中查閱

UNITY_ASSERT_EQUAL_FLOAT

UNITY_ASSERT_EQUAL_DOUBLE

你會發現它們實際上并不是真的斷言兩個值是相等的,而是"足夠接近"到相等。“足夠接近”

是由這些精度配置選項控制的。如果你在使用32位float和/或64位double(大部分處理器上是這樣),

你應該不需要修改這些選項。它們都被設為給你大約一個有效bit的誤差。float是0.00001,

double是10-12。想要了解更多的細節,見Unity斷言指南的附件。

示例:

#define UNITY_FLOAT_PRECISION 0.001f
           

雜項

UNITY_EXCLUDE_STDDEF_H

Unity使用

NULL

宏,它定義了一個null指針常量的值,預設定義在

stddef.h

中。如果你想要

在自己的宏中提供它,你應該通過添加這個宏定義到配置中以排除

stddef.h

頭檔案。

示例:

#define UNITY_EXCLUDE_STDDEF_H
           

工具集定制

除了以上列出的選項,還有許多其他選項,它們在你需要為你特定工具鍊定制Unity行為時

能派上用場。可能你不需要動這些東西…但在特定的平台,特别是運作在模拟器中的那些,

可能需要做一些事情以正确運作。這些宏有助于這些情況。

UNITY_OUTPUT_CHAR(a)

UNITY_OUTPUT_FLUSH()

UNITY_OUTPUT_START()

UNITY_OUTPUT_COMPLETE()

預設的,Unity在運作時會往

stdout

列印它的結果。在你使用一個本地編譯器來進行測試時,

這幾乎總可以完美工作。在一些模拟器上也沒有問題,隻要這些模拟器将

stdout

路由回了

指令行。然而,有時模拟器不支援輸出結果,或者你也許需要把結果路由到其他地方。這種

情況下,你應該定義

UNITY_OUTPUT_CHAR

宏。這個宏一次接受一個字元(作為

int

,因為這

是标志C

putchar

函數最常用的輸入)。你可用任意函數替代它。

示例:

假設你被迫在一個沒有

stdout

的嵌入式處理器上運作你的測試套件。你決定路由的你測試結果

輸出到一個定制的串行

RS232_putc()

函數,你會像這樣寫:

#include "RS232_header.h"
    ...
    #define UNITY_OUTPUT_CHAR(a) RS232_putc(a)
    #define UNITY_OUTPUT_START() RS232_config(115200,1,8,0)
    #define UNITY_OUTPUT_FLUSH() RS232_flush()
    #define UNITY_OUTPUT_COMPLETE() RS232_close()
           

注意:

UNITY_OUTPUT_FLUSH()

可以通過指定

UNITY_USE_FLUSH_STDOUT

很友善地被設為标志輸出flush

函數。不需要其他定義。

UNITY_WEAK_ATTRIBUTE

UNITY_WEAK_PRAGMA

UNITY_NO_WEAK

對于一些目标,Unity可以讓要求的setUp()和tearDown()函數變成可選的。這對于測試寫作者

十分便利,因為setUp和tearDown常常什麼都不做。如果你在使用gcc或clang,這個選項會自動

幫你定義。其他編譯器也可以支援這個行為,如果他們支援一個稱為weak函數的C特性。weak函

數是指,除非在某處定義了一個non-weak版本的同個函數,它就會被編譯進可執行程式的函數。

如果發現了一個non-weak版本,weak版本就會被忽略,好像它不存在。如果你的編譯器支援這

個特性,你可以通過定義UNITY_WEAK_ATTRIBUTE或UNITY_WEAK_PRAGMA為辨別一個函數為weak

所需的函數屬性以讓Unity知道。如果你的編譯器缺少對weak函數的支援,那你就得每次都定義

setUp和tearDown函數(盡管它們可以,且經常是空的)。你可以通過定義UNITY_NO_WEAK以強制

Unity不要使用weak函數。這個特性最常見的選項是:

示例:

#define UNITY_WEAK_ATTRIBUTE weak
    #define UNITY_WEAK_ATTRIBUTE __attribute__((weak))
    #define UNITY_WEAK_PRAGMA
    #define UNITY_NO_WEAK
           

UNITY_PTR_ATTRIBUTE

一些編譯器需要給指針一個定制的屬性,像

near

far

。在這些情況下,你可以通過定義

這個選項為你想要的屬性以給Unity一個安全的預設值。

示例:

#define UNITY_PTR_ATTRIBUTE __attribute__((far))
    #define UNITY_PTR_ATTRIBUTE near
           

UNITY_PRINT_EOL

預設的,Unity在每一行輸出的最後輸出\n。這對于如Ceedling這樣的腳本來說很好分析,

但這不一定适用于你的系統。你可以随意的重寫它,改成你想要的東西。

示例:

#define UNITY_PRINT_EOL { UNITY_OUTPUT_CHAR('\r'); UNITY_OUTPUT_CHAR('\n') }
           

UNITY_EXCLUDE_DETAILS

如果你必須節省一絲一毫的記憶體的話,那就是這個選項了。Unity存儲了一個内部便簽集,其

被用于傳遞額外的細節資訊。它被用于像CMock這樣的系統以報告是哪個函數或參數出了錯。

如果你不在使用CMock,并且你沒将其用于其他東西,那你可以排除掉它。

示例:

#define UNITY_EXCLUDE_DETAILS
           

UNITY_EXCLUDE_SETJMP

如果你的嵌入式系統不支援标準庫的setjmp,你可以通過這個定義來排除Unity對setjmp的依賴。

然而,排除依賴是有代價的。你将無法為你的測試使用定制的helper函數,你将不能使用像CMock

這樣的工具。但話說回來,如果你的編譯器連setjmp都不支援,那很可能你甚至都沒有記憶體放這些

東西…是以這個這個選項是為這些場合存在的。

示例:

#define UNITY_EXCLUDE_SETJMP
           

UNITY_OUTPUT_COLOR

如果你想要使用ANSI轉義碼添加顔色,你可以使用這個定義。

示例:

#define UNITY_OUTPUT_COLOR
           

深入虎穴

偶爾可能上面的選項仍無法滿足你的需求。如果你在使用一個本地工具鍊(如Mac上的clang)

編譯和執行你的測試,它們很可能完全夠了。這些選項應該也足夠你應對大部分從本地指令

行運作目标模拟器的情況了。但特别是如果你必須在你的目标硬體上運作你的測試套件,你

的Unity配置将需要特别的幫助。這個特别的幫助通常處于兩個地方之一:

main()

函數或者

RUN_TEST

宏。我們看看是怎麼回事。

main()

每個測試子產品都是自己編譯和運作的,與你工程中的其他測試檔案互相獨立。是以每個測試

檔案都有一個

main

函數。這個

main

函數将需要包含初始化你的系統到可工作狀态的所有

必要代碼。在你必須設定記憶體映射或為你的測試結果初始化輸出信道時這就特别明顯了。

一個簡單的main函數看起來像這樣子:

int main(void) {
        UNITY_BEGIN();
        RUN_TEST(test_TheFirst);
        RUN_TEST(test_TheSecond);
        RUN_TEST(test_TheThird);
        return UNITY_END();
    }
           

你可以看到我們的main函數并沒有接受任何參數。對于我們最基本的情況,我們不需要參數,

因為我們每次就把所有的測試都運作了。一開始,我們調用了

UNITY_BEGIN

。我們(以任意

想要的順序)運作每個測試。最後,我們調用

UNITY_END

,傳回其傳回值(傳回值就是失敗的

總數)。

很容易看的出來,你可以在所有測試用例運作之前或者所有測試用例完成之後添加代碼。

這使得你能做系統等級的setup或teardown,在一些特殊情況下可能需要。

RUN_TEST

每個測試用例函數都會使用

RUN_TEST

宏來調用。它的工作是展開執行單個測試用例函數

所需的各種setup和teardown。包括捕獲失敗,調用測試子產品的

setUp()

tearDown()

函數并調用

UnityConcludeTest()

。如果使用了CMock或者測試覆寫率,這裡還會使用額

外的樁。一個簡單的最小RUN_TEST宏看起來像這樣:

#define RUN_TEST(testfunc) \
        UNITY_NEW_TEST(#testfunc) \
        if (TEST_PROTECT()) { \
            setUp(); \
            testfunc(); \
        } \
        if (TEST_PROTECT() && (!TEST_IS_IGNORED)) \
            tearDown(); \
        UnityConcludeTest();
           

是以這真是TM個宏呀,呵?它讓你大概看了下對于每個測試用例,Unity需要做些什麼事情。

對于每個測試用例,我們聲明它是一個新的測試。然後我們運作

setUp

和我們的測試函數。

這些是運作在一個

TEST_PROTECT

語句塊中的,TEST_PROTECT函數用于處理測試中發生的

失敗。然後,假如我們的測試仍然在運作且沒有被忽略,我們運作

tearDown

。不管咋樣,

我們的最後一個是在繼續下一個測試前結束這一個。

假如你需要添加一個對

fsync

的調用以強迫所有的輸出資料在每個測試後flush到一個檔案中。

你應該簡單地插入它到你的

UnityConcludeTest

調用後。也許你想要在每個測試結果集前後

寫一個xml标簽。再次,你可以通過添加行到這個宏中來實作。更新這個宏适用于那種你需要

在整個測試套件中,在每單個測試用例前後添加行為的情況。

愉快的移植

這篇向導中的定義和宏應該能幫助你移植Unity到所有我們能想象的到的C目标上。如果你

遇到了一兩個障礙,盡管在論壇上尋求幫助。我們熱愛好的挑戰!

Find The Latest of This And More at ThrowTheSwitch.org

以下是中英對照版

Unity Configuration Guide

Unity配置指南

C Standards, Compilers and Microcontrollers

C 标準、編譯器和微控制器

The embedded software world contains its challenges. Compilers support different

revisions of the C Standard. They ignore requirements in places, sometimes to

make the language more usable in some special regard. Sometimes it’s to simplify

their support. Sometimes it’s due to specific quirks of the microcontroller they

are targeting. Simulators add another dimension to this menagerie.

嵌入式軟體世界擁有自己的挑戰。編譯器支援不同修訂版的C标準。它們忽視一些要求,這有

時在會使得語言更有用。有時是為了簡化它們的支援。有時是由于它們目标微控制器的特殊性。

模拟器們則使事情更加複雜了。

Unity is designed to run on almost anything that is targeted by a C compiler. It

would be awesome if this could be done with zero configuration. While there are

some targets that come close to this dream, it is sadly not universal. It is

likely that you are going to need at least a couple of the configuration options

described in this document.

Unity被設計為可運作在幾乎任何由C編譯器支援的東西上。要是不需要任何配置就能實作這個

目标的話就太棒了。盡管在有些目标上已經很接近這個夢想了,但遺憾的是并不都可以。很可

能你至少需要修改一些在這篇文檔中描述的配置選項。

All of Unity’s configuration options are

#defines

. Most of these are simple

definitions. A couple are macros with arguments. They live inside the

unity_internals.h header file. We don’t necessarily recommend opening that file

unless you really need to. That file is proof that a cross-platform library is

challenging to build. From a more positive perspective, it is also proof that a

great deal of complexity can be centralized primarily to one place to

provide a more consistent and simple experience elsewhere.

所有的Unity配置選項都是

#defines

。其中大部分都是簡單的定義。一些是帶有參數的宏。

它們在unity_internals.h頭檔案中。除非你真的需要,其實不必打開那個檔案。那個檔案

就是跨平台庫十分難搭建的一個證明。往好了想,它也證明了許多複雜性可以集中在一個地

方以使得其他地方更加一緻和簡單。

Using These Options

使用這些選項

It doesn’t matter if you’re using a target-specific compiler and a simulator or

a native compiler. In either case, you’ve got a couple choices for configuring

these options:

你是在使用一個目标特定的編譯器和一個模拟器或者一個本地編譯器并沒有什麼差别。不管在

哪種情況下,你都有許多選擇以配置這些選項:

  1. Because these options are specified via C defines, you can pass most of these

    options to your compiler through command line compiler flags. Even if you’re

    using an embedded target that forces you to use their overbearing IDE for all

    configuration, there will be a place somewhere in your project to configure

    defines for your compiler.

  2. You can create a custom

    unity_config.h

    configuration file (present in your

    toolchain’s search paths). In this file, you will list definitions and macros

    specific to your target. All you must do is define

    UNITY_INCLUDE_CONFIG_H

    and

    Unity will rely on

    unity_config.h

    for any further definitions it may need.
  3. 因為這些選項是通過C定義來指定的,你可以通過指令行傳遞大部分這些選項給你的編譯器。

    即使你在使用一個強制你使用它們IDE來進行所有配置的嵌入式目标,那也總會有一個地方可以

    配置你項目的編譯器的定義。

  4. 你可以建立一個定制的

    unity_config.h

    配置檔案(放在你工具鍊的搜尋路徑中)。在這個檔案

    中,列出特定于你的目标的定義和宏。你唯一要做的就是定義

    UNITY_INCLUDE_CONFIG_H

    ,然後,

    Unity會在

    unity_config.h

    中進一步尋找它可能需要的定義。

The Options

選項

Integer Types

整數類型

If you’ve been a C developer for long, you probably already know that C’s

concept of an integer varies from target to target. The C Standard has rules

about the

int

matching the register size of the target microprocessor. It has

rules about the

int

and how its size relates to other integer types. An

int

on one target might be 16 bits while on another target it might be 64. There are

more specific types in compilers compliant with C99 or later, but that’s

certainly not every compiler you are likely to encounter. Therefore, Unity has a

number of features for helping to adjust itself to match your required integer

sizes. It starts off by trying to do it automatically.

如果你做了很久的C開發者,你很可能已經知道C中整型值會随着目标平台而變化。C标準的

規矩是,

int

比對目标微處理器的寄存器大小,以及

int

大小與其他整型大小的關系。一

個目标平台上

int

可能是16位的,到了另一個上可能就成64位的了。編譯器中有更多遵從

C99以及之後的标準的特定類型,但當然不是每個編譯器都這樣。是以,Unity有許多特性能

幫助調整自己以滿足你需要的整型大小。它最開始會嘗試自動完成這事。

UNITY_EXCLUDE_STDINT_H

The first thing that Unity does to guess your types is check

stdint.h

.

This file includes defines like

UINT_MAX

that Unity can use to

learn a lot about your system. It’s possible you don’t want it to do this

(um. why not?) or (more likely) it’s possible that your system doesn’t

support

stdint.h

. If that’s the case, you’re going to want to define this.

That way, Unity will know to skip the inclusion of this file and you won’t

be left with a compiler error.

Unity猜測你的類型做的頭件事是檢查

stdint.h

。這個檔案會包含定義如

UINT_MAX

Unity可以用其來了解你的系統。可能你不想要它做這件事(昂,為什麼?)或者(更可能)

是因為你的系統不支援

stdint.h

。如果是這樣的話,你應該定義這個宏。這樣,

Unity就知道要跳過這個檔案,而你也不會得到編譯錯誤了。

Example:

#define UNITY_EXCLUDE_STDINT_H
           

UNITY_EXCLUDE_LIMITS_H

The second attempt to guess your types is to check

limits.h

. Some compilers

that don’t support

stdint.h

could include

limits.h

instead. If you don’t

want Unity to check this file either, define this to make it skip the inclusion.

做的第二件事是檢查

limits.h

。一些不支援

stdint.h

的編譯器會補償你個

limits.h

如果你同樣不想讓Unity檢查這個檔案,定義這個宏以使Unity跳過這個包含。

Example:

#define UNITY_EXCLUDE_LIMITS_H
           

If you’ve disabled both of the automatic options above, you’re going to have to

do the configuration yourself. Don’t worry. Even this isn’t too bad… there are

just a handful of defines that you are going to specify if you don’t like the

defaults.

如果你把上面兩個功能都禁用了,那你就得自己進行配置了。别擔心。即使這樣也沒那麼糟…

如果你不喜歡預設配置的話,你隻需要定義幾個東西就好了。

UNITY_INT_WIDTH

Define this to be the number of bits an

int

takes up on your system. The

default, if not autodetected, is 32 bits.

定義這個為你系統上

int

的比特數。預設為32位。

Example:

#define UNITY_INT_WIDTH 16
           

UNITY_LONG_WIDTH

Define this to be the number of bits a

long

takes up on your system. The

default, if not autodetected, is 32 bits. This is used to figure out what kind

of 64-bit support your system can handle. Does it need to specify a

long

or a

long long

to get a 64-bit value. On 16-bit systems, this option is going to be

ignored.

定義這個為你系統上

long

的比特數。預設為32位。這被用于表明你的系統支不支援64位。

是需要指定一個

long

還是

long long

來獲得64位值。在16位系統上,這個選項應該忽略。

Example:

#define UNITY_LONG_WIDTH 16
           

UNITY_POINTER_WIDTH

Define this to be the number of bits a pointer takes up on your system. The

default, if not autodetected, is 32-bits. If you’re getting ugly compiler

warnings about casting from pointers, this is the one to look at.

定義這個為你系統上一個指針的比特數。預設為32位。如果你編譯器警告說對指針進行了

強制類型轉換,那你要看看這個。

Example:

#define UNITY_POINTER_WIDTH 64
           

UNITY_SUPPORT_64

Unity will automatically include 64-bit support if it auto-detects it, or if

your

int

,

long

, or pointer widths are greater than 32-bits. Define this to

enable 64-bit support if none of the other options already did it for you. There

can be a significant size and speed impact to enabling 64-bit support on small

targets, so don’t define it if you don’t need it.

Unity如果自動探測到了系統的64位支援,或者如果你的

int

long

或指針寬度大于32位

的話,它會自動包含64位支援。如果其他選項都沒幫你完成這事的話,你可以定義這個宏以

啟用64位支援。在小目标上啟用64位支援可能會對大小和速度有重大影響,是以如果你不需要

的話,不要定義它。

Example:

#define UNITY_SUPPORT_64
           

Floating Point Types

浮點數類型

In the embedded world, it’s not uncommon for targets to have no support for

floating point operations at all or to have support that is limited to only

single precision. We are able to guess integer sizes on the fly because integers

are always available in at least one size. Floating point, on the other hand, is

sometimes not available at all. Trying to include

float.h

on these platforms

would result in an error. This leaves manual configuration as the only option.

在嵌入式世界中,某個目标一點都不支援浮點數運算或者隻支援單精度一點都不稀奇。我們

能夠猜測整型的大小是因為總是有整型的,起碼有一個位元組的整型。另一方面,浮點數有時

根本就沒有。在這些平台上嘗試include

float.h

會導緻一個錯誤。這導緻隻能人工配置。

UNITY_INCLUDE_FLOAT

UNITY_EXCLUDE_FLOAT

UNITY_INCLUDE_DOUBLE

UNITY_EXCLUDE_DOUBLE

By default, Unity guesses that you will want single precision floating point

support, but not double precision. It’s easy to change either of these using the

include and exclude options here. You may include neither, either, or both, as

suits your needs. For features that are enabled, the following floating point

options also become available.

預設的,Unity猜測你想要單精度浮點數支援,但是不想要雙精度的。你可以使用這兒的

include和exclude選項來改變這個行為。你可以按你需要include這兩個、其中一個、或

都不要。對于啟用了的精度,以下浮點數選項也可以用。

Example:

//what manner of strange processor is this?
    #define UNITY_EXCLUDE_FLOAT
    #define UNITY_INCLUDE_DOUBLE
           

UNITY_EXCLUDE_FLOAT_PRINT

Unity aims for as small of a footprint as possible and avoids most standard

library calls (some embedded platforms don’t have a standard library!). Because

of this, its routines for printing integer values are minimalist and hand-coded.

Therefore, the display of floating point values during a failure are optional.

By default, Unity will print the actual results of floating point assertion

failure (e.g. ”Expected 4.56 Was 4.68”). To not include this extra support, you

can use this define to instead respond to a failed assertion with a message like

”Values Not Within Delta”. If you would like verbose failure messages for floating

point assertions, use these options to give more explicit failure messages.

Unity的目标是盡可能小的開銷,它盡可能避免調用标準庫(一些嵌入式平台沒有标準庫!)。

是以,它用于列印整型值的例程是極簡寫死的。是以,測試失敗時是否展示浮點數值是可選

的。預設地,Unity會在斷言失敗時列印浮點數的真實值(比如”Expected 4.56 Was 4.68”)。

不想要這個額外支援的話,你可以使用這個宏定義來把斷言失敗消息替換為像

”Values Not Within Delta”。如果你想要更啰嗦的浮點數斷言失敗消息,使用這個選項來給出

更加正确的故障消息。

Example:

#define UNITY_EXCLUDE_FLOAT_PRINT
           

UNITY_FLOAT_TYPE

If enabled, Unity assumes you want your

FLOAT

asserts to compare standard C

floats. If your compiler supports a specialty floating point type, you can

always override this behavior by using this definition.

如果啟用了浮點數支援,Unity假設你想要你的

FLOAT

斷言比較的是标準C float。如果你的

編譯器支援特殊的浮點數類型,你可以通過使用這個定義重寫這個行為。

Example:

#define UNITY_FLOAT_TYPE float16_t
           

UNITY_DOUBLE_TYPE

If enabled, Unity assumes you want your

DOUBLE

asserts to compare standard C

doubles. If you would like to change this, you can specify something else by

using this option. For example, defining

UNITY_DOUBLE_TYPE

to

long double

could enable gargantuan floating point types on your 64-bit processor instead of

the standard

double

.

如果啟用了浮點數支援,Unity假設你想要你的

DOUBLE

斷言比較的是标準C double。

如果你想要改變它,你可以使用這個選項指定其他東西。比如,定義

UNITY_DOUBLE_TYPE

long double

可以在你的64位處理器上啟用巨浮點數類型以替代标準的

double

Example:

#define UNITY_DOUBLE_TYPE long double
           

UNITY_FLOAT_PRECISION

UNITY_DOUBLE_PRECISION

If you look up

UNITY_ASSERT_EQUAL_FLOAT

and

UNITY_ASSERT_EQUAL_DOUBLE

as

documented in the big daddy Unity Assertion Guide, you will learn that they are

not really asserting that two values are equal but rather that two values are

“close enough” to equal. “Close enough” is controlled by these precision

configuration options. If you are working with 32-bit floats and/or 64-bit

doubles (the normal on most processors), you should have no need to change these

options. They are both set to give you approximately 1 significant bit in either

direction. The float precision is 0.00001 while the double is 10-12.

For further details on how this works, see the appendix of the Unity Assertion

Guide.

如果你在Unity斷言指南中查閱

UNITY_ASSERT_EQUAL_FLOAT

UNITY_ASSERT_EQUAL_DOUBLE

你會發現它們實際上并不是真的斷言兩個值是相等的,而是"足夠接近"到相等。“足夠接近”

是由這些精度配置選項控制的。如果你在使用32位float和/或64位double(大部分處理器上是這樣),

你應該不需要修改這些選項。它們都被設為給你大約一個有效bit的誤差。float是0.00001,

double是10-12。想要了解更多的細節,見Unity斷言指南的附件。

Example:

#define UNITY_FLOAT_PRECISION 0.001f
           

Miscellaneous

雜項

UNITY_EXCLUDE_STDDEF_H

Unity uses the

NULL

macro, which defines the value of a null pointer constant,

defined in

stddef.h

by default. If you want to provide

your own macro for this, you should exclude the

stddef.h

header file by adding this

define to your configuration.

Unity使用

NULL

宏,它定義了一個null指針常量的值,預設定義在

stddef.h

中。如果你想要

在自己的宏中提供它,你應該通過添加這個宏定義到配置中以排除

stddef.h

頭檔案。

Example:

#define UNITY_EXCLUDE_STDDEF_H
           

Toolset Customization

工具集定制

In addition to the options listed above, there are a number of other options

which will come in handy to customize Unity’s behavior for your specific

toolchain. It is possible that you may not need to touch any of these… but

certain platforms, particularly those running in simulators, may need to jump

through extra hoops to run properly. These macros will help in those

situations.

除了以上列出的選項,還有許多其他選項,它們在你需要為你特定工具鍊定制Unity行為時

能派上用場。可能你不需要動這些東西…但在特定的平台,特别是運作在模拟器中的那些,

可能需要做一些事情以正确運作。這些宏有助于這些情況。

UNITY_OUTPUT_CHAR(a)

UNITY_OUTPUT_FLUSH()

UNITY_OUTPUT_START()

UNITY_OUTPUT_COMPLETE()

By default, Unity prints its results to

stdout

as it runs. This works

perfectly fine in most situations where you are using a native compiler for

testing. It works on some simulators as well so long as they have

stdout

routed back to the command line. There are times, however, where the simulator

will lack support for dumping results or you will want to route results

elsewhere for other reasons. In these cases, you should define the

UNITY_OUTPUT_CHAR

macro. This macro accepts a single character at a time (as

an

int

, since this is the parameter type of the standard C

putchar

function

most commonly used). You may replace this with whatever function call you like.

預設的,Unity在運作時會往

stdout

列印它的結果。在你使用一個本地編譯器來進行測試時,

這幾乎總可以完美工作。在一些模拟器上也沒有問題,隻要這些模拟器将

stdout

路由回了

指令行。然而,有時模拟器不支援輸出結果,或者你也許需要把結果路由到其他地方。這種

情況下,你應該定義

UNITY_OUTPUT_CHAR

宏。這個宏一次接受一個字元(作為

int

,因為這

是标志C

putchar

函數最常用的輸入)。你可用任意函數替代它。

Example:

Say you are forced to run your test suite on an embedded processor with no

stdout

option. You decide to route your test result output to a custom serial

RS232_putc()

function you wrote like thus:

假設你被迫在一個沒有

stdout

的嵌入式處理器上運作你的測試套件。你決定路由的你測試結果

輸出到一個定制的串行

RS232_putc()

函數,你會像這樣寫:

#include "RS232_header.h"
    ...
    #define UNITY_OUTPUT_CHAR(a) RS232_putc(a)
    #define UNITY_OUTPUT_START() RS232_config(115200,1,8,0)
    #define UNITY_OUTPUT_FLUSH() RS232_flush()
    #define UNITY_OUTPUT_COMPLETE() RS232_close()
           

Note:

UNITY_OUTPUT_FLUSH()

can be set to the standard out flush function simply by

specifying

UNITY_USE_FLUSH_STDOUT

. No other defines are required.

注意:

UNITY_OUTPUT_FLUSH()

可以通過指定

UNITY_USE_FLUSH_STDOUT

很友善地被設為标志輸出flush

函數。不需要其他定義。

UNITY_WEAK_ATTRIBUTE

UNITY_WEAK_PRAGMA

UNITY_NO_WEAK

For some targets, Unity can make the otherwise required setUp() and tearDown()

functions optional. This is a nice convenience for test writers since setUp and

tearDown don’t often actually do anything. If you’re using gcc or clang, this

option is automatically defined for you. Other compilers can also support this

behavior, if they support a C feature called weak functions. A weak function is

a function that is compiled into your executable unless a non-weak version of

the same function is defined elsewhere. If a non-weak version is found, the weak

version is ignored as if it never existed. If your compiler supports this feature,

you can let Unity know by defining UNITY_WEAK_ATTRIBUTE or UNITY_WEAK_PRAGMA as

the function attributes that would need to be applied to identify a function as

weak. If your compiler lacks support for weak functions, you will always need to

define setUp and tearDown functions (though they can be and often will be just

empty). You can also force Unity to NOT use weak functions by defining

UNITY_NO_WEAK. The most common options for this feature are:

對于一些目标,Unity可以讓要求的setUp()和tearDown()函數變成可選的。這對于測試寫作者

十分便利,因為setUp和tearDown常常什麼都不做。如果你在使用gcc或clang,這個選項會自動

幫你定義。其他編譯器也可以支援這個行為,如果他們支援一個稱為weak函數的C特性。weak函

數是指,除非在某處定義了一個non-weak版本的同個函數,它就會被編譯進可執行程式的函數。

如果發現了一個non-weak版本,weak版本就會被忽略,好像它不存在。如果你的編譯器支援這

個特性,你可以通過定義UNITY_WEAK_ATTRIBUTE或UNITY_WEAK_PRAGMA為辨別一個函數為weak

所需的函數屬性以讓Unity知道。如果你的編譯器缺少對weak函數的支援,那你就得每次都定義

setUp和tearDown函數(盡管它們可以,且經常是空的)。你可以通過定義UNITY_NO_WEAK以強制

Unity不要使用weak函數。這個特性最常見的選項是:

Example:

#define UNITY_WEAK_ATTRIBUTE weak
    #define UNITY_WEAK_ATTRIBUTE __attribute__((weak))
    #define UNITY_WEAK_PRAGMA
    #define UNITY_NO_WEAK
           

UNITY_PTR_ATTRIBUTE

Some compilers require a custom attribute to be assigned to pointers, like

near

or

far

. In these cases, you can give Unity a safe default for these by

defining this option with the attribute you would like.

一些編譯器需要給指針一個定制的屬性,像

near

far

。在這些情況下,你可以通過定義

這個選項為你想要的屬性以給Unity一個安全的預設值。

Example:

#define UNITY_PTR_ATTRIBUTE __attribute__((far))
    #define UNITY_PTR_ATTRIBUTE near
           

UNITY_PRINT_EOL

By default, Unity outputs \n at the end of each line of output. This is easy

to parse by the scripts, by Ceedling, etc, but it might not be ideal for YOUR

system. Feel free to override this and to make it whatever you wish.

預設的,Unity在每一行輸出的最後輸出\n。這對于如Ceedling這樣的腳本來說很好分析,

但這不一定适用于你的系統。你可以随意的重寫它,改成你想要的東西。

Example:

#define UNITY_PRINT_EOL { UNITY_OUTPUT_CHAR('\r'); UNITY_OUTPUT_CHAR('\n') }
           

UNITY_EXCLUDE_DETAILS

This is an option for if you absolutely must squeeze every byte of memory out of

your system. Unity stores a set of internal scratchpads which are used to pass

extra detail information around. It’s used by systems like CMock in order to

report which function or argument flagged an error. If you’re not using CMock and

you’re not using these details for other things, then you can exclude them.

如果你必須節省一絲一毫的記憶體的話,那就是這個選項了。Unity存儲了一個内部便簽集,其

被用于傳遞額外的細節資訊。它被用于像CMock這樣的系統以報告是哪個函數或參數出了錯。

如果你不在使用CMock,并且你沒将其用于其他東西,那你可以排除掉它。

Example:

#define UNITY_EXCLUDE_DETAILS
           

UNITY_EXCLUDE_SETJMP

If your embedded system doesn’t support the standard library setjmp, you can

exclude Unity’s reliance on this by using this define. This dropped dependence

comes at a price, though. You will be unable to use custom helper functions for

your tests, and you will be unable to use tools like CMock. Very likely, if your

compiler doesn’t support setjmp, you wouldn’t have had the memory space for those

things anyway, though… so this option exists for those situations.

如果你的嵌入式系統不支援标準庫的setjmp,你可以通過這個定義來排除Unity對setjmp的依賴。

然而,排除依賴是有代價的。你将無法為你的測試使用定制的helper函數,你将不能使用像CMock

這樣的工具。但話說回來,如果你的編譯器連setjmp都不支援,那很可能你甚至都沒有記憶體放這些

東西…是以這個這個選項是為這些場合存在的。

Example:

#define UNITY_EXCLUDE_SETJMP
           

UNITY_OUTPUT_COLOR

If you want to add color using ANSI escape codes you can use this define.

如果你想要使用ANSI轉義碼添加顔色,你可以使用這個定義。

Example:

#define UNITY_OUTPUT_COLOR
           

Getting Into The Guts

深入虎穴

There will be cases where the options above aren’t quite going to get everything

perfect. They are likely sufficient for any situation where you are compiling

and executing your tests with a native toolchain (e.g. clang on Mac). These

options may even get you through the majority of cases encountered in working

with a target simulator run from your local command line. But especially if you

must run your test suite on your target hardware, your Unity configuration will

require special help. This special help will usually reside in one of two

places: the

main()

function or the

RUN_TEST

macro. Let’s look at how these

work.

偶爾可能上面的選項仍無法滿足你的需求。如果你在使用一個本地工具鍊(如Mac上的clang)

編譯和執行你的測試,它們很可能完全夠了。這些選項應該也足夠你應對大部分從本地指令

行運作目标模拟器的情況了。但特别是如果你必須在你的目标硬體上運作你的測試套件,你

的Unity配置将需要特别的幫助。這個特别的幫助通常處于兩個地方之一:

main()

函數或者

RUN_TEST

宏。我們看看是怎麼回事。

main()

Each test module is compiled and run on its own, separate from the other test

files in your project. Each test file, therefore, has a

main

function. This

main

function will need to contain whatever code is necessary to initialize

your system to a workable state. This is particularly true for situations where

you must set up a memory map or initialize a communication channel for the

output of your test results.

每個測試子產品都是自己編譯和運作的,與你工程中的其他測試檔案互相獨立。是以每個測試

檔案都有一個

main

函數。這個

main

函數将需要包含初始化你的系統到可工作狀态的所有

必要代碼。在你必須設定記憶體映射或為你的測試結果初始化輸出信道時這就特别明顯了。

A simple main function looks something like this:

一個簡單的main函數看起來像這樣子:

int main(void) {
        UNITY_BEGIN();
        RUN_TEST(test_TheFirst);
        RUN_TEST(test_TheSecond);
        RUN_TEST(test_TheThird);
        return UNITY_END();
    }
           

You can see that our main function doesn’t bother taking any arguments. For our

most barebones case, we’ll never have arguments because we just run all the

tests each time. Instead, we start by calling

UNITY_BEGIN

. We run each test

(in whatever order we wish). Finally, we call

UNITY_END

, returning its return

value (which is the total number of failures).

你可以看到我們的main函數并沒有接受任何參數。對于我們最基本的情況,我們不需要參數,

因為我們每次就把所有的測試都運作了。一開始,我們調用了

UNITY_BEGIN

。我們(以任意

想要的順序)運作每個測試。最後,我們調用

UNITY_END

,傳回其傳回值(傳回值就是失敗的

總數)。

It should be easy to see that you can add code before any test cases are run or

after all the test cases have completed. This allows you to do any needed

system-wide setup or teardown that might be required for your special

circumstances.

很容易看的出來,你可以在所有測試用例運作之前或者所有測試用例完成之後添加代碼。

這使得你能做系統等級的setup或teardown,在一些特殊情況下可能需要。

RUN_TEST

The

RUN_TEST

macro is called with each test case function. Its job is to

perform whatever setup and teardown is necessary for executing a single test

case function. This includes catching failures, calling the test module’s

setUp()

and

tearDown()

functions, and calling

UnityConcludeTest()

. If

using CMock or test coverage, there will be additional stubs in use here. A

simple minimalist RUN_TEST macro looks something like this:

每個測試用例函數都會使用

RUN_TEST

宏來調用。它的工作是展開執行單個測試用例函數

所需的各種setup和teardown。包括捕獲失敗,調用測試子產品的

setUp()

tearDown()

函數并調用

UnityConcludeTest()

。如果使用了CMock或者測試覆寫率,這裡還會使用額

外的樁。一個簡單的最小RUN_TEST宏看起來像這樣:

#define RUN_TEST(testfunc) \
        UNITY_NEW_TEST(#testfunc) \
        if (TEST_PROTECT()) { \
            setUp(); \
            testfunc(); \
        } \
        if (TEST_PROTECT() && (!TEST_IS_IGNORED)) \
            tearDown(); \
        UnityConcludeTest();
           

So that’s quite a macro, huh? It gives you a glimpse of what kind of stuff Unity

has to deal with for every single test case. For each test case, we declare that

it is a new test. Then we run

setUp

and our test function. These are run

within a

TEST_PROTECT

block, the function of which is to handle failures that

occur during the test. Then, assuming our test is still running and hasn’t been

ignored, we run

tearDown

. No matter what, our last step is to conclude this

test before moving on to the next.

是以這真是TM個宏呀,呵?它讓你大概看了下對于每個測試用例,Unity需要做些什麼事情。

對于每個測試用例,我們聲明它是一個新的測試。然後我們運作

setUp

和我們的測試函數。

這些是運作在一個

TEST_PROTECT

語句塊中的,TEST_PROTECT函數用于處理測試中發生的

失敗。然後,假如我們的測試仍然在運作且沒有被忽略,我們運作

tearDown

。不管咋樣,

我們的最後一個是在繼續下一個測試前結束這一個。

Let’s say you need to add a call to

fsync

to force all of your output data to

flush to a file after each test. You could easily insert this after your

UnityConcludeTest

call. Maybe you want to write an xml tag before and after

each result set. Again, you could do this by adding lines to this macro. Updates

to this macro are for the occasions when you need an action before or after

every single test case throughout your entire suite of tests.

假如你需要添加一個對

fsync

的調用以強迫所有的輸出資料在每個測試後flush到一個檔案中。

你應該簡單地插入它到你的

UnityConcludeTest

調用後。也許你想要在每個測試結果集前後

寫一個xml标簽。再次,你可以通過添加行到這個宏中來實作。更新這個宏适用于那種你需要

在整個測試套件中,在每單個測試用例前後添加行為的情況。

Happy Porting

愉快的移植

The defines and macros in this guide should help you port Unity to just about

any C target we can imagine. If you run into a snag or two, don’t be afraid of

asking for help on the forums. We love a good challenge!

這篇向導中的定義和宏應該能幫助你移植Unity到所有我們能想象的到的C目标上。如果你

遇到了一兩個障礙,盡管在論壇上尋求幫助。我們熱愛好的挑戰!

Find The Latest of This And More at ThrowTheSwitch.org