# Simple Test Case

如何確認你的程式碼是否有照你想像的運作？

其實有很多種法。一步步的透過debugger檢查或使用一些另外寫的鷹架程式碼來輸出結果，是兩個簡單而常見的做法，但它們都有缺點。一步步的透過debugger是一個好主意，但它不會主動的告訴你程式碼的狀況。每次修改都得檢查一次。鷹架程式碼也不錯，但是它讓程式碼醜掉了，它會在你不需要訊息的時機產生訊息。

用CppUnit進行測試，會自動回報程式碼狀況。單元測試很容易建立，而且只要你有寫，就會來帶程式碼愈改品質愈高的信心。

一個簡單的測試程式，可以這樣做:

衍生自`TestCase`類別。Override `runTest()`。呼叫`CPPUNIT_ASSERT(bool)`有要確認的值當參數，傳入`true`代表測試通過。

例如，下面這個「是否相等」的測試 比較`Complex`(複數)類別是否相等的測試:

```
class ComplexNumberTest : public CppUnit::TestCase
{ 
public: 
    ComplexNumberTest(std::string name)
    : CppUnit::TestCase( name ) {}

    void runTest()
    {
        CPPUNIT_ASSERT( Complex (10, 1) == Complex (10, 1) );
        CPPUNIT_ASSERT( !(Complex (1, 1) == Complex (2, 2)) );
    }
};
```

這是一個非常簡單的測試。通常你會對同一個物件進行許多小小的test case。要做到這一點，用一個fixture把它們裝起來。

## 意譯

* littering your code with stream output 一些另外寫的鷹架程式碼
* streaming out text 鷹架程式碼 &#x20;
* it is not automatic 它不會主動的告訴你程式碼的狀況&#x20;

## 譯註

* treaming 應該是streaming的筆誤&#x20;
* expression  表達式 運算式
* Complex number class 複數類別，我覺得翻成「`Complex`(複數)類別」比較好
* Override 覆寫


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dwatow.gitbook.io/cppunit-cookbook-in-traditional-chinese/simple_test_case.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
