在使用 ASSERT_ 或 EXPECT_ 宏的 gtest 中使用辅助函数时,该辅助函数必须为空。但是,我也想检查调用测试代码中的那些错误。
ASSERT_NO_FATAL_FAILURE 宏有助于在触发 ASSERT_ 时停止调用代码,但我也想通过正确处理 EXPECT_ 故障(阅读:NonFatalFailures)来扩展它。这是我到目前为止得到的:
#include <gtest/gtest.h>
// A void test-function using ASSERT_ or EXPECT_ calls should be encapsulated by this macro.
// Example: CHECK_FOR_FAILURES(MyCheckForEquality(lhs, rhs))
#define CHECK_FOR_FAILURES(statement) \
ASSERT_NO_FATAL_FAILURE((statement)); \
EXPECT_FALSE(HasNonfatalFailure())
void TestHelperFunction(bool givenAssert, int givenExpect)
{
ASSERT_TRUE(givenAssert); // note: this is line 11 in my code
EXPECT_EQ(givenExpect, 0); // note: this is line 12 in my code
}
TEST(FailureInFunctionTestNoChecks, noChecks)
{
// note: this is line 17 in my code
TestHelperFunction(true, 0);
TestHelperFunction(true, 1);
for (int i = 0; i < 3; ++i)
{
TestHelperFunction(true, i);
}
TestHelperFunction(false, 1);
TestHelperFunction(true, 2);
}
TEST(FailureInFunctionTestWithChecks, withChecks)
{
// note: this is line 30 in my code
CHECK_FOR_FAILURES(TestHelperFunction(true, 0)) << "\n All good - will NOT be seen! \n";
CHECK_FOR_FAILURES(TestHelperFunction(true, 1)) << "\n optional msg: First Expect failed \n";
for (int i = 0; i < 3; ++i)
{
CHECK_FOR_FAILURES(TestHelperFunction(true, i)) << "\n optional msg: Expect failed for i=" << i << "\n";
}
CHECK_FOR_FAILURES(TestHelperFunction(false, 1)) << "this message will NOT be seen due to the assert";
CHECK_FOR_FAILURES(TestHelperFunction(true, 2)) << "\n will not be seen because assert stops the test \n";
}
// This test creates the following output:
// Note: Google Test filter = *FailureInFunctionTest*
// [==========] Running 2 tests from 2 test cases.
// [----------] Global test environment set-up.
// [----------] 1 test from FailureInFunctionTestNoChecks
// [ RUN ] FailureInFunctionTestNoChecks.noChecks
// ./checked_test_failure.cpp:12: Failure
// Expected equality of these values:
// givenExpect
// Which is: 1
// 0
// ./checked_test_failure.cpp:12: Failure
// Expected equality of these values:
// givenExpect
// Which is: 1
// 0
// ./checked_test_failure.cpp:12: Failure
// Expected equality of these values:
// givenExpect
// Which is: 2
// 0
// ./checked_test_failure.cpp:11: Failure
// Value of: givenAssert
// Actual: false
// Expected: true
// ./checked_test_failure.cpp:12: Failure
// Expected equality of these values:
// givenExpect
// Which is: 2
// 0
// [ FAILED ] FailureInFunctionTestNoChecks.noChecks (0 ms)
// [----------] 1 test from FailureInFunctionTestNoChecks (0 ms total)
//
// [----------] 1 test from FailureInFunctionTestWithChecks
// [ RUN ] FailureInFunctionTestWithChecks.withChecks
// ./checked_test_failure.cpp:12: Failure
// Expected equality of these values:
// givenExpect
// Which is: 1
// 0
// ./checked_test_failure.cpp:32: Failure
// Value of: HasNonfatalFailure()
// Actual: true
// Expected: false
//
// optional msg: First Expect failed
//
// ./checked_test_failure.cpp:35: Failure
// Value of: HasNonfatalFailure()
// Actual: true
// Expected: false
//
// optional msg: Expect failed for i=0
//
// ./checked_test_failure.cpp:12: Failure
// Expected equality of these values:
// givenExpect
// Which is: 1
// 0
// ./checked_test_failure.cpp:35: Failure
// Value of: HasNonfatalFailure()
// Actual: true
// Expected: false
//
// optional msg: Expect failed for i=1
//
// ./checked_test_failure.cpp:12: Failure
// Expected equality of these values:
// givenExpect
// Which is: 2
// 0
// ./checked_test_failure.cpp:35: Failure
// Value of: HasNonfatalFailure()
// Actual: true
// Expected: false
//
// optional msg: Expect failed for i=2
//
// ./checked_test_failure.cpp:11: Failure
// Value of: givenAssert
// Actual: false
// Expected: true
// ./checked_test_failure.cpp:37: Failure
// Expected: (TestHelperFunction(false, 1)) doesn't generate new fatal failures in the current thread.
// Actual: it does.
// [ FAILED ] FailureInFunctionTestWithChecks.withChecks (1 ms)
// [----------] 1 test from FailureInFunctionTestWithChecks (1 ms total)
//
// [----------] Global test environment tear-down
// [==========] 2 tests from 2 test cases ran. (1 ms total)
// [ PASSED ] 0 tests.
// [ FAILED ] 2 tests, listed below:
// [ FAILED ] FailureInFunctionTestNoChecks.noChecks
// [ FAILED ] FailureInFunctionTestWithChecks.withChecks
//
// 2 FAILED TESTS
//
正如您从输出中看到的那样:使用新的“CHECK_FOR_FAILURES”宏改进了测试输出:它告诉您哪一行导致了失败,并阻止在命中断言后执行测试。
但是,正如您在 i=0 的输出中看到的那样,使用“HasNonfatalFailure()”还不够好。原因是已经有一个非 fatal error 并且对于 i=0 没有新的非 fatal error 但是 HasNonfatalFailure() 由于旧错误返回 true.. :-(
知道如何摆脱错误的 i=0 输出吗?
最佳答案
使用 suggestion of gtest docs 的明显方法是以下内容,但我宁愿在没有自定义消息或 SCOPED_TRACE() 宏的情况下执行此操作
#include <gtest/gtest.h>
// A void test-function using ASSERT_ or EXPECT_ calls with a custom message should be encapsulated
// by this macro. Example: CHECK_FOR_FAILURES_MSG(MyCheckForEquality(counter, 42), "for counter=42")
#define CHECK_FOR_FAILURES_MSG(statement, message) \
{ \
SCOPED_TRACE(message); \
ASSERT_NO_FATAL_FAILURE((statement)); \
}
// A void test-function using ASSERT_ or EXPECT_ calls should be encapsulated by this macro.
// Example: CHECK_FOR_FAILURES(MyCheckForEquality(lhs, rhs))
#define CHECK_FOR_FAILURES(statement) CHECK_FOR_FAILURES_MSG(statement, " <-- line of failure\n")
void TestHelperFunction(bool givenAssert, int givenExpect)
{
ASSERT_TRUE(givenAssert); // note: this is line 17 in my code
EXPECT_EQ(givenExpect, 0); // note: this is line 18 in my code
}
TEST(FailureInFunctionTestNoChecks, noChecks)
{
// note: this is line 23 in my code
TestHelperFunction(true, 0);
TestHelperFunction(true, 1);
for (int i = 0; i < 3; ++i)
{
TestHelperFunction(true, i);
}
TestHelperFunction(false, 1);
TestHelperFunction(true, 2);
}
TEST(FailureInFunctionTestWithChecks, withChecks)
{
// note: this is line 36 in my code
CHECK_FOR_FAILURES(TestHelperFunction(true, 0));
CHECK_FOR_FAILURES(TestHelperFunction(true, 1));
for (int i = 0; i < 3; ++i)
{
CHECK_FOR_FAILURES_MSG(TestHelperFunction(true, i), "for i=" + std::to_string(i) + "\n");
}
CHECK_FOR_FAILURES(TestHelperFunction(false, 1));
CHECK_FOR_FAILURES(TestHelperFunction(true, 2));
}
// Note: Google Test filter = *FailureInFunction*
//[==========] Running 2 tests from 2 test cases.
//[----------] Global test environment set-up.
//[----------] 1 test from FailureInFunctionTestNoChecks
//[ RUN ] FailureInFunctionTestNoChecks.noChecks
//./checked_test_failure.cpp:18: Failure
//Expected equality of these values:
// givenExpect
// Which is: 1
// 0
//./checked_test_failure.cpp:18: Failure
//Expected equality of these values:
// givenExpect
// Which is: 1
// 0
//./checked_test_failure.cpp:18: Failure
//Expected equality of these values:
// givenExpect
// Which is: 2
// 0
//./checked_test_failure.cpp:17: Failure
//Value of: givenAssert
// Actual: false
//Expected: true
//./checked_test_failure.cpp:18: Failure
//Expected equality of these values:
// givenExpect
// Which is: 2
// 0
//[ FAILED ] FailureInFunctionTestNoChecks.noChecks (0 ms)
//[----------] 1 test from FailureInFunctionTestNoChecks (0 ms total)
//
//[----------] 1 test from FailureInFunctionTestWithChecks
//[ RUN ] FailureInFunctionTestWithChecks.withChecks
//./checked_test_failure.cpp:18: Failure
//Expected equality of these values:
// givenExpect
// Which is: 1
// 0
//Google Test trace:
//./checked_test_failure.cpp:38: <-- line of failure
//
//./checked_test_failure.cpp:18: Failure
//Expected equality of these values:
// givenExpect
// Which is: 1
// 0
//Google Test trace:
//./checked_test_failure.cpp:41: for i=1
//
//./checked_test_failure.cpp:18: Failure
//Expected equality of these values:
// givenExpect
// Which is: 2
// 0
//Google Test trace:
//./checked_test_failure.cpp:41: for i=2
//
//./checked_test_failure.cpp:17: Failure
//Value of: givenAssert
// Actual: false
//Expected: true
//Google Test trace:
//./checked_test_failure.cpp:43: <-- line of failure
//
//./checked_test_failure.cpp:43: Failure
//Expected: (TestHelperFunction(false, 1)) doesn't generate new fatal failures in the current thread.
// Actual: it does.
//Google Test trace:
//./checked_test_failure.cpp:43: <-- line of failure
//
//[ FAILED ] FailureInFunctionTestWithChecks.withChecks (0 ms)
//[----------] 1 test from FailureInFunctionTestWithChecks (0 ms total)
//
//[----------] Global test environment tear-down
//[==========] 2 tests from 2 test cases ran. (0 ms total)
//[ PASSED ] 0 tests.
//[ FAILED ] 2 tests, listed below:
//[ FAILED ] FailureInFunctionTestNoChecks.noChecks
//[ FAILED ] FailureInFunctionTestWithChecks.withChecks
//
// 2 FAILED TESTS
//
关于c++ - 使用 void 函数检查 gtest 中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53883790/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。