Home > The Unit Test Framework > User's guide > Test organization >

Test organization or the house that Jack built

If you look at many legacy test modules, big chance is that it's implemented as one big test function that consists of a mixture of check and output statements. Is there anything wrong with it? Yes. There are various disadvantages in single test function approach:
如果你看了大量遺留的測試模塊,很可能會看到一個很大的測試函數包含驗證和輸出語句的混合。 這有什麼問題嗎?是的。單個測試函數的方法有如下一些不足:

The above points should make it clear that it's preferable to split test module into smaller units. These units are test cases. A test case has to be constructed based on some kind of function and registered in a test tree, so that the test runner knows how to invoke it. There are different possible designs for the test case construction problem: inheritance from the predefined base class, specifically named member function and so on. The UTF opted to avoid classed altogether and to use the least intrusive " generic callback" approach. The facility, the UTF provides, requires specific function signature and allows adopting any function or function object that matches the signature as a test case. The signatures the UTF supports are:
基於上面的觀點,最好將測試模塊拆分為小的單元。 這些單元就是測試用例。測試用例需要由某些類型的函數創建並註冊到測試樹中,使得測試運行器知道如何調用它。 關於測試用例構造問題可能有不同的設計:從預定義的基類繼承,特殊命名的成員函數,等等。 UTF 為了避免類的藕合,選擇使用最小侵入的 "泛型回調" 方式。 UTF 提供的工具需要特定的函數簽名,並允許接受符號簽名的函數或函數對像作為測試用例。 UTF 支持的簽名如下:

To solve test tree creation problem the UTF provides facilities for test suite creation.
UTF 提供用於支持測試套件創建,以此來解決測試樹創建的問題。

Generic test case construction design used by the UTF causes the test case implementation (test function body) and test case creation/registration points to be remote. As a result you may forget to register the test case and it's never going to be executed, even though it's present in test file. To alleviate this issue the UTF presents facilities for automated (in place) test case creation and registration in the test tree. These facilities sacrifice some generality and work for selected test function signatures only. But the result is that library users are relieved from the necessity to manually register test cases. These facilities are the most user-friendly and are recommended to be used whenever possible. In addition they support automated registration of test suites, thus allowing whole test tree to be created without any use of manual registration.
UTF 使用的泛型測試用例構造設計使得測試用例實現 (測試函數主體) 和測試用例創建 / 註冊點分離。 其結果是你可能會忘記註冊測試用例,這樣它就永遠都不會被執行,即使出現在測試文件中。 為了減輕這個問題,UTF 提供了工具來自動 (在適當地方) 創建測試用例並將其註冊到測試樹中。 這些工具犧牲了一些普遍性,只能工作於特定簽名的測試函數。但其結果是庫的用戶從必須手動註冊測試用例中解放出來。 這些工具對用戶非常友好,並在任何可能應用的時候都推薦使用。 另外,它們支持測試套件的自動註冊,允許整個測試樹都可以不用手動註冊就被創建。

The single test module may mix both automated and manual test case registration. In other words, within the same test module you can have both test cases implemented remotely and registered manually in the test module initialization function and test cases that are registered automatically at implementation point.
單個測試模塊可以同時包含自動和手動的測試用例註冊。 換句話說,在同一個測試模塊中,你可以在某個地方實現測試用例,並在測試模塊初始化函數中手動註冊它, 也可以在實現測試用例的地點上自動註冊它們。

In some cases it's desirable to allow some "expected" failures in test case without failing a test module. To support this request The UTF allows specifying the number of expected failures in a test case.
在某些情況下,在測試用例中允許某些 "期待的" 失敗,而不會導致測試模塊失敗,這是需要的。 為了支持這個需求,UTF 允許在測試用例中聲明一個期待失敗的數量。