GoogleTest:如何跳过testing?

使用Google Test 1.6(Windows 7,Visual Studio C ++)。 我如何closures特定的testing? (又如何防止testing运行)。 除了评论整个testing之外,还有什么可以做的吗?

Google Test 1.7的文档 build议 :

“如果你有一个不能立即修复的testing失败,你可以将DISABLED_前缀添加到它的名字中,这样就不能执行。”

根据文档,您也可以运行一个testing子集 :

运行testing的子集

默认情况下,Googletesting程序会运行用户定义的所有testing。 有时,您只想运行一部分testing(例如,用于debugging或快速validation更改)。 如果将GTEST_FILTER环境variables或–gtest_filter标志设置为filterstring,Google Test将仅运行全名(以TestCaseName.TestName的forms)与filter匹配的testing。

filter的格式是通配符模式(称为正模式)的“:”分隔列表,可选地后跟“ – ”和另一个“:”分隔的模式列表(称为负模式)。 一个testing匹配filter当且仅当它匹配任何正模式,但不匹配任何负模式。

模式可能包含'*'(匹配任何string)或'?' (匹配任何单个字符)。 为了方便起见,filter'* -NegativePatterns'也可以写成'-NegativePatterns'。

例如:

./foo_test Has no flag, and thus runs all its tests. ./foo_test --gtest_filter=* Also runs everything, due to the single match-everything * value. ./foo_test --gtest_filter=FooTest.* Runs everything in test case FooTest. ./foo_test --gtest_filter=*Null*:*Constructor* Runs any test whose full name contains either "Null" or "Constructor". ./foo_test --gtest_filter=-*DeathTest.* Runs all non-death tests. ./foo_test --gtest_filter=FooTest.*-FooTest.Bar Runs everything in test case FooTest except FooTest.Bar. 

不是最漂亮的解决scheme,但它的工作原理。

这里的expression式包括名称中包含stringfoo1或foo2的testing,并排除其名称中包含stringbar1或bar2的testing:

 --gtest_filter=*foo1*:*foo2*-*bar1*:*bar2* 

如果需要跳过多个testing,

 --gtest_filter=-TestName.*:TestName.*TestCase 

对于另一种方法,你可以将你的testing包装在一个函数中,并在运行时使用正常的条件检查来只在需要时执行它们。

 #include <gtest/gtest.h> const bool skip_some_test = true; bool some_test_was_run = false; void someTest() { EXPECT_TRUE(!skip_some_test); some_test_was_run = true; } TEST(BasicTest, Sanity) { EXPECT_EQ(1, 1); if(!skip_some_test) { someTest(); EXPECT_TRUE(some_test_was_run); } } 

这对我来说很有用,因为我只是在系统支持双协议栈IPv6时才运行一些testing。

从技术上讲,双栈的东西不应该是一个unit testing,因为它依赖于系统。 但是,我无法进行任何集成testing,直到我已经testing了它们的工作情况,并确保它不会在代码错误时报告故障。

至于它的testing,我有存根对象,通过构造假套接字来模拟系统对dualstack(或缺less)的支持。

唯一的缺点是testing输出和testing次数会改变,这可能会导致监视成功testing次数的问题。

您也可以使用ASSERT_ *而不是EQUAL_ *。 如果失败,将断言testing的其余部分。 防止大量冗余的东西被转储到控制台。

我更喜欢用代码来做:

 // Run a specific test only //testing::GTEST_FLAG(filter) = "MyLibrary.TestReading"; // I'm testing a new feature, run something quickly // Exclude a specific test testing::GTEST_FLAG(filter) = "-MyLibrary.TestWriting"; // The writing test is broken, so skip it 

我可以注释掉两行来运行所有的testing,取消第一行的注释来testing我正在研究/正在处理的单个function,或者如果testing被破坏,则取消注释第二行,但是我想testing其他所有内容。
您还可以使用通配符并编写一个列表“MyLibrary.TestNetwork *”或“-MyLibrary.TestFileSystem *”来testing/排除一组function。