C++ 单元测试库:UnitC++

jopen 10年前

UnitC++是一个轻量级、只包含头文件的C++库用于简化单元测试。 UnitC++的目的是使它很容易以一种可移植的方式来测试C+ +代码。

Now you are ready to write the test code. UnitC++ provided some helpful macros for writing tests. Here is a list of them.

TEST_EQUAL(A, B)  TEST_NOT_EQUAL(A, B)  TEST_LESS_THAN(A, B)  TEST_MORE_THAN(A, B)  TEST_APPROX_EQUAL(A, B, TOLERANCE)  TEST_TRUE(A)  TEST_FALSE(A)  TEST_THROWS(FUNCTION, EXCEPTION, ...)

So here is a filled out version of the above example of a test.

#include <UnitCpp/Test.h>    TEST(MyString, length_test)  {    MyString str("This is a string");    TEST_EQUAL(str.length(), 16);    TEST_NOT_EQUAL(str.length(), 17);    TEST_LESS_THAN(str.length(), 20);    TEST_MORE_THAN(str.length(), 10);    TEST_APPROX_EQUAL(str.length(), 15, 1.1); // test the length is within 1.1 of 15  }    TEST(MyString, validity_test)  {    MyString invalid_string;    TEST_FALSE(invalid_string.valid());      MyString valid_string("");    TEST_TRUE(valid_string.valid());    #ifdef UNITCPP_TEST_THROWS_AVAILABLE    TEST_THROWS([&](){invalid_string.length();}, MyString::InvalidStringException);  #endif // UNITCPP_TEST_THROWS_AVAILABLE    }

项目主页:http://www.open-open.com/lib/view/home/1406770175327