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

Fixtures or let me repeat myself

In general terms a test fixture or test context is the collection of one or more of the following items, required to perform the test:

Though these tasks are encountered in many if not all test cases, what makes a test fixture different is repetition. Where a normal test case implementation does all preparatory and cleanup work itself, a test fixture allows this to be implemented in a separate reusable unit.
雖然這個任務不是所有測試用例都會碰到,但在很多情況下會碰到,這會使得不同的測試夾具被重複。 通常的測試用例實現所有自己的準備和清理工作,測試夾具允許將這些實現為獨立的可復用的單元。

With introduction of extreme programming (XP), the testing style, that require test setup/cleanup repetition, is becoming more and more popular. Single XP adopted test modules may contain hundreds of single assertion test cases, many requiring very similar test setup/cleanup. This is the problem that the test fixture is designed to solve.
隨著敏捷編程 (XP) 的引入,需要測試安裝 / 清理重複的測試風格越來越普遍。 單個符合 XP 風格的測試模塊可能包含上百個單斷言的測試用例,許多都需要相似的測試安裝 / 清理。 這就是測試夾具設計要解決的問題。

In practice a test fixture usually is a combination of setup and teardown functions, associated with test case. The former serves the purposes of test setup; the later is dedicated to the cleanup tasks. Ideally it's preferable that a test module author is able to define variables used in fixtures on the stack and the same time is able to refer to them directly in test case.
實際上測試夾具通常是安裝和拆卸函數的組合,和測試用例結合。 前者提供測試安裝的目的;後者用於清理的任務。 理論上講,測試模塊作者可以在夾具的棧上定義變量,同時在測試用例中直接使用它們。

It's important to understand that C++ provides a way to implement a straightforward test fixture solution that almost satisfies our requirements without any extra support from the test framework. This may explain why test fixtures support was introduced in the UTF somewhat late in its life cycle. Here is how simple test module with such a fixture may look like:
很重要的一點是,C++ 提供了一種方法能夠直接實現測試夾具的解決方案,不需要測試框架額外的支持就幾乎能夠滿足我們的需要。 這就解釋了為什麼測試夾具支持在 UTF 這麼遲介紹。 下面的例子展示了簡單的測試模塊和夾具結合的情況:

struct MyFixture {
     MyFixture() { i = new int; *i = 0 }
     ~ MyFixture() { delete i; }

    int* i;

BOOST_AUTO_TEST_CASE( test_case1 )
    MyFixture f;

    // do something involving f.i

BOOST_AUTO_TEST_CASE( test_case2 )
    MyFixture f;

    // do something involving f.i

This is a generic solution that can be used to implement any kind of shared setup or cleanup procedure. Still there are several more or less minor practical issues with this pure C++ based fixtures solution:
這是通用的解決方案,可以用來實現各種類型的共享安裝或清理過程。 在這個基於純 C++ 的夾具解決方案中同樣或多或少有一些需要實踐注意的問題:

While there is little the UTF can do to address these issues for manually registered test units, it's possible to resolve them for test units that are automatically registered. To do this the UTF defines a generic fixture model - fixed interfaces that both setup and teardown fixture functions should comply to. Based on the generic fixture model, the UTF presents solution for the following tasks:
雖然 UTF 對於手動註冊的測試單元沒有什麼更好的解決方法, 但對於自動註冊的測試單元還是能夠解決的。 想要解決,UTF 定義了通用夾具模型 - 安裝和拆卸的夾具函數都需要遵守的固定接口。 基於通用夾具模型,UTF 為下列任務提供了解決方案: