从覆盖报告中删除自动生成的异常代码 [英] Remove auto generated exception code from coverage report

查看:206
本文介绍了从覆盖报告中删除自动生成的异常代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们从一个最简单的工作例子开始:

main.cpp:

  #include< iostream> 
#include< string>

int main(){
std :: cout<< hello+ std :: to_string(42);
返回0;
}

我使用以下标志编译此代码:

  [g ++ / clang ++] -std = c ++ 11 -g -Og --coverage -Wall -o main main.cpp 

叮当声4.0.1

gcc 4.8.5。



由于编译器生成的异常代码不会执行,所以我只获得50%的代码覆盖率,正如解释在另一个stackoverflow问题中。

问题是通过 -fno-exceptions 禁用异常对我来说不是一种选择。我正在编写单元测试的代码使用异常,因此禁用所有这些代码不是一种选择。

为了生成一个报告,我使用 gcovr ,在clang ++的情况下另外使用 llvm-cov gcov 来转换它。但我没有被绑定到这些工具,所以如果你有其他工具不显示这种行为,请建议他们!

基本上我需要一种编译/写入单元的方法测试此代码并获得启用了例外的100%分支/条件覆盖率。有没有办法?

解决方案

嗯,我相信你的意图不是实际测试这一小块代码,而是使用这个概念在一个项目中...



您输入的代码会引发异常 - 当您没有内存时,会抛出 bad_alloc 留下来存储将使用 std :: to_string 创建的字符串。为了100%安全, std :: to_string 应该被 try-catch 包围,在那里你可以处理你的异常。

要构建一个100%的代码覆盖率单元测试,您需要强制执行异常 - 在这种特定情况下几乎不可能保证,因为参数是一个常数。但是,在您的项目中,您可能需要分配大小可变的一些数据 - 在这种情况下,您可以在代码中分配分配内存的方法以单独测试它们。然后你传递给这些方法,在测试函数中,分配一大笔费用来评估你放在catch块上的内容(并检查你是否正确处理它)。

例如,这段代码应该抛出异常,你可以在构建测试时使用它来激发自己(

  // bad_alloc.cpp 
//编译:/ EHsc
#include
#包含< iostream>
使用namespace std;

int main(){
char * ptr;
try {
ptr = new char [(〜unsigned int((int)0)/ 2) - 1];
delete [] ptr;
}
catch(bad_alloc& ba){
cout<< ba.what()<< ENDL;






$ b然而,如果你不打算处理所有的 bad_alloc 异常(或绝对所有异常),因此无法获得100%的覆盖率 - 因为它不会被100%覆盖......大多数情况下不过,真正的100%覆盖率是不必要的。

Let's start with a minimal working example:
main.cpp:

#include <iostream>
#include <string>

int main() {
    std::cout << "hello " + std::to_string(42);
    return 0;
}

I compile this code using the following flags:

[g++/clang++] -std=c++11 -g -Og --coverage -Wall -o main main.cpp

clang 4.0.1
gcc 4.8.5.

I get only 50% code coverage, since the compiler generates exception code, which is not executed, as explained in another stackoverflow question.

The problem is that disabling exceptions via -fno-exceptionsis not an option for me. The code I am writing unit tests for uses exceptions, so disabling all of them is not an option.

In order to generate a report I'm using gcovr, in case of clang++ additionally llvm-cov gcovto convert it. But I am not bound to these tools, so if you have other tools that do not show this behaviour please suggest them!

Basically I need a way to compile/write unit tests for this code and get 100% branch / conditional coverage with exceptions enabled. Is there a way?

解决方案

Well, I believe your intention is not actually test this small piece of code, but use the concept in a project...

The code you entered throws an exception - bad_alloc is thrown when you have no memory left to store the string that will be created with std::to_string. To be 100% safe, std::to_string should be surrounded with try-catch, where you could handle your exception.

To build a 100% code coverage unit test, you will need to force the exception to happen - in this specific case it is almost impossible to guarantee, since the parameter is a constant number. But, in your project, you probably have some data to be allocated whose size is variable - in this case, you can isolate in your code the methods that allocate memory, to test them separately. Then you pass to these methods, in the test function, a huge amount to be allocated to evaluate what you have put on your catch block (and check if you are handling it properly).

For instance, this code should throw the exception, you could use it to inspire yourself when building your tests (source):

// bad_alloc.cpp
// compile with: /EHsc
#include<new>
#include<iostream>
using namespace std;

int main() {
   char* ptr;
   try {
      ptr = new char[(~unsigned int((int)0)/2) - 1];
      delete[] ptr;
   }
   catch( bad_alloc &ba) {
      cout << ba.what( ) << endl;
   }
}

However, if you are not planning to handle all bad_alloc exceptions (or absolutely all exceptions) in your code, there is no way to get 100% coverage - since it won't be 100% covered... Most of the cases, true 100% coverage is unnecessary, though.

这篇关于从覆盖报告中删除自动生成的异常代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆