使用CppUnit进行“小于”测试

时间:2010-06-08 17:13:41

标签: c++ cppunit

我是CppUnit的新手。有没有办法断言一个值必须小于零?我正在寻找与CPPUNIT_ASSERT_EQUAL()具有类似行为的内容。我在想可能有一个名为CPPUNIT_ASSERT_LESS_THAN()的测试函数。

2 个答案:

答案 0 :(得分:5)

CPPUNIT_ASSERT(variable < 0);怎么样?

答案 1 :(得分:3)

关注我们的cppunit测试模板是这个评论块:

/*
The following macros for adding test cases are available:

- CPPUNIT_TEST(memberFunction): Add a member function to the suite.

- CPPUNIT_TEST_EXCEPTION(memberFunction, exception): Add a member function to
  the suite, which fails if it does not throw the specified exception type.

- CPPUNIT_TEST_FAIL(memberFunction): Add a member function to the suite that
  is expected to fail (i.e., it fails if the memberFunction does not fail).


The following assert macros are available:

- CPPUNIT_ASSERT(condition): Assert that condition is true.

- CPPUNIT_ASSERT_MESSAGE(message, condition): Assert that condition is true,
  and fail with message if it is not.

- CPPUNIT_FAIL(message): Fail with the given message.

- CPPUNIT_ASSERT_EQUAL(expected, actual): Assert that expected equals actual.
  Note that expected and actual needs to be of the same type.

- CPPUNIT_ASSERT_EQUAL_MESSAGE(message, expected, actual): Assert that
  expected equals actual, and fail with message if not.

- CPPUNIT_ASSERT_DOUBLES_EQUAL(expected, actual, delta): Assert that the
  floating point values expected and actual do not differ by more than delta.

- CPPUNIT_ASSERT_THROW(expression, ExceptionType): Assert that the given
  expression causes an exception of type ExceptionType to be thrown.

- CPPUNIT_ASSERT_NO_THROW(expression): Assert that the given expression does
  not throw an exception.
*/

它涵盖了大多数情况。正如Oxley所提到的,CPPUNIT_ASSERT是这里的最佳选择,无论是否有消息。