我开始使用“Catch”单元测试框架,到目前为止它真的很棒。我曾经使用过内置的VS单元测试框架,但感觉很痛苦。
我注意到一个问题,即宏REQUIRE_THROWS_AS
的行为并不像人们所期望的那样。
根据文档:
当我尝试编写时
REQUIRE_THROWS_AS( expression, exception type ) and CHECK_THROWS_AS( expression, exception type )
Expects that an exception of the specified type is thrown during evaluation of the expression.
TEST_CASE("some test") {
SECTION("vector throws") {
std::vector<int> vec;
REQUIRE_THROWS_AS(vec.at(10), std::logic_error);
}
}
我本来以为测试会失败,但它却显示测试通过了。是框架出了问题还是我错了?
vec.at(10)
-> 抛出异常std::out_of_range
-> 您认为应该抛出异常std::logic_error
->std::out_of_range
是std::logic_error
的子类型,所以一切都没问题。 注意:此翻译仅供参考,如果需要精确的技术翻译,请咨询专业的翻译人员或技术人员。 - milleniumbug