简体   繁体   English

如何将 Google Test 测试用例标记为“预期失败”?

[英]How to mark a Google Test test-case as “expected to fail”?

I want to add a testcase for functionality not yet implemented and mark this test case as " it's ok that I fail ".我想为尚未实现的功能添加一个测试用例,并将这个测试用例标记为“我失败也没关系”。

Is there a way to do this?有没有办法做到这一点?

EDIT: I want the test to be executed and the framework should verify it is failing as long as the testcase is in the "expected fail" state.编辑:我希望测试被执行,只要测试用例处于“预期失败”状态,框架就应该验证它是否失败。

EDIT2: It seems that the feature I am interested in does not exist in google-test, but it does exist in the Boost Unit Test Framework , and in LIT . EDIT2:似乎我感兴趣的功能在 google-test 中不存在,但在Boost Unit Test FrameworkLIT 中确实存在。

EXPECT_NONFATAL_FAILURE is what you want to wrap around the code that you expect to fail. EXPECT_NONFATAL_FAILURE 是您想要将预期失败的代码包装起来的内容。 Note you will hav to include the gtest-spi.h header file:请注意,您必须包含 gtest-spi.h 头文件:

#include "gtest-spi.h"

 // ...

TEST_F( testclass, testname )
{
EXPECT_NONFATAL_FAILURE(
      // your code here, or just call:
      FAIL()
      ,"Some optional text that would be associated with"
      " the particular failure you were expecting, if you"
      " wanted to be sure to catch the correct failure mode" );
}

I'm not aware of a direct way to do this, but you can fake it with something like this:我不知道这样做的直接方法,但你可以用这样的东西来伪造它:

try {
  // do something that should fail
  EXPECT_TRUE(false);
} catch (...) {
  // return or print a message, etc.
}

Basically, the test will fail if it reaches the contradictory expectation.基本上,如果达到矛盾的期望,测试就会失败。

It would be unusual to have a unit test in an expected-to-fail state .在预期失败状态下进行单元测试是不寻常的。 Unit tests can test for positive conditions ("expect x to equal 2 ") or negative conditions ("expect save to throw an exception if name is null "), and can be flagged not to run at all (if the feature is pending and you don't want the noise in your test output).单元测试可以测试正条件(“期望x等于2 ”)或负条件(“如果namenull ,则期望save抛出异常”),并且可以标记为根本不运行(如果该功能处于挂起状态并且您不希望测试输出中出现噪音)。 But what you seem to be asking for is a way to negate a feature's test while you're working on it.但是您似乎要求的是一种在您处理功能时否定功能测试的方法。 This is against the tenants of Test Driven Development.这违背了测试驱动开发的租户。

In TDD, what you should do is write tests that accurately describe what a feature should do.在 TDD 中,您应该做的是编写准确描述功能该做什么的测试。 If that feature isn't written yet then, by definition, those tests will and should fail.如果该功能尚未编写,那么根据定义,这些测试将会并且应该失败。 Then you implement the feature until, one by one, all those tests pass.然后您实现该功能,直到所有这些测试都通过。 You want all the tests to start as failing and then move to passing.您希望所有测试都以失败开始,然后再通过。 That's how you know when your feature is complete.这就是您知道功能何时完成的方式。

Think of how it would look if you were able to mark failing tests as passing as you suggest: all tests would pass and everything would look complete when the feature didn't work.想象一下,如果您能够按照您的建议将失败的测试标记为通过,会是什么样子:所有测试都将通过,并且当该功能不起作用时,一切看起来都已完成。 Then, once you were done and the feature worked as expected, suddenly your tests would start to fail until you went in and unflagged them.然后,一旦您完成并且该功能按预期工作,您的测试就会突然开始失败,直到您进入并取消标记它们。 Beyond being a strange way to work, this workflow would be very prone to error and false-positives.除了是一种奇怪的工作方式之外,这个工作流程还很容易出错和误报。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM