如何在C语言中获得80%的代码覆盖率
为此,我需要在我的两个函数inputchar()和inputstring()中编写代码。当我这样做时,我需要使针对testme()函数运行的代码覆盖率至少达到80%。在运行GCOV时,我的覆盖率仅为17%,从17%到23%有所反弹。有人有什么建议/帮助吗?我是C语言的新手,创建了这样的随机测试程序 唯一需要修改的代码是inputchar文件和inputstring文件如何在C语言中获得80%的代码覆盖率,c,testing,C,Testing,为此,我需要在我的两个函数inputchar()和inputstring()中编写代码。当我这样做时,我需要使针对testme()函数运行的代码覆盖率至少达到80%。在运行GCOV时,我的覆盖率仅为17%,从17%到23%有所反弹。有人有什么建议/帮助吗?我是C语言的新手,创建了这样的随机测试程序 唯一需要修改的代码是inputchar文件和inputstring文件 char inputChar() { char random = (rand() % 94) + 32; ret
char inputChar()
{
char random = (rand() % 94) + 32;
return random;
}
char *inputString()
{
static char s[6];
s[0] = 'r';
s[1] = 'e';
s[2] = 's';
s[3] = 'e';
s[4] = 't';
s[5] = '\0';
int i;
for(i = 0; i<6;i++) {
s[i]++;
i++;
}
return s;
}
void testme()
{
int tcCount = 0;
char *s;
char c;
int state = 0;
while (1)
{
tcCount++;
c = inputChar();
s = inputString();
printf("Iteration %d: c = %c, s = %s, state = %d\n", tcCount, c, s, state);
if (c == '[' && state == 0) state = 1;
if (c == '(' && state == 1) state = 2;
if (c == '{' && state == 2) state = 3;
if (c == ' '&& state == 3) state = 4;
if (c == 'a' && state == 4) state = 5;
if (c == 'x' && state == 5) state = 6;
if (c == '}' && state == 6) state = 7;
if (c == ')' && state == 7) state = 8;
if (c == ']' && state == 8) state = 9;
if (s[0] == 'r' && s[1] == 'e'
&& s[2] == 's' && s[3] == 'e'
&& s[4] == 't' && s[5] == '\0'
&& state == 9)
{
printf("error ");
exit(200);
}
}
}
char inputChar()
{
随机字符=(rand()%94)+32;
返回随机;
}
char*inputString()
{
静态字符s[6];
s[0]=‘r’;
s[1]=‘e’;
s[2]=‘s’;
s[3]=‘e’;
s[4]=‘t’;
s[5]='\0';
int i;
对于(i=0;i不要随机测试;当代码覆盖率是目标时不测试。
阅读要测试的代码,并定义专门用于引导您通过要测试函数的每个路径的输入。您知道传递每个路径是这项工作的目标,不是吗?
为了简化此过程,请在测试时进行调试并观察哪个路径通过,哪个路径不通过,然后扩展测试以同时通过丢失的路径。为此,您需要自己编写测试程序。看起来测试只能调用testme
函数,它永远不会干净地返回,当它停止循环时,它将退出整个计划
更具体地说:
问问自己“我如何执行语句state=9
”,这就是你的目标。
不希望出现随机字符是实现这一目标的关键。
也不要在目标实现之前执行exit(200)
语句
你需要实现两件事:
执行语句state=9
,否则您的比率将很低。
待测试的代码被精心设计,这当然很难做到
不要在第一个目标之前执行语句exit(200);
,否则不会执行多个测试
对于1,您必须使用函数char inputChar()
,因为它是唯一影响if
s的函数。
对于2,您必须使用函数inputString
,否则它将执行不希望的提前退出
我建议停止阅读这里的内容,首先尝试自己编写这个程序
请在继续阅读之前进行调试
读下去之前先想一想
在继续阅读之前,请先编写程序
真的,不要继续读下去
我是认真的
char inputChar()
{
int static charstate=0; /* function internal state counter, starts with 0 */
/* the following lines are intentionally written
to be similar to the code to be tested */
char testchar='['; /* get tested code from state 0 to 1 */
if (charstate == 1) testchar='('; /* get tested code from state 1 to 2 */
if (charstate == 2) testchar='{'; /* get tested code from state 2 to 3 */
if (charstate == 3) testchar=' '; /* get tested code from state 3 to 4 */
if (charstate == 4) testchar='a'; /* get tested code from state 4 to 5 */
if (charstate == 5) testchar='x'; /* get tested code from state 5 to 6 */
if (charstate == 6) testchar='}'; /* get tested code from state 6 to 7 */
if (charstate == 7) testchar=']'; /* get tested code from state 7 to 8 */
if (charstate == 8) testchar=')'; /* get tested code from state 8 to 9 */
/* synchronise function internal state counter */
charstate ++; /* only once, do not do this inside the ifs,
that would cause a fall through... */
/* return the character which will achieve the next state in each loop iteration */
return testchar;
}
char *inputString()
{
int static stringstate=0; /* function internal state counter, starts with 0 */
static char s[6];
s[0] = '#'; /* intentionally avoid triggering the last test case */
s[1] = 'e';
s[2] = 's';
s[3] = 'e';
s[4] = 't';
s[5] = '\0';
/* I left your code as much as possible similar to what you had,
there are other ways. */
if (stringstate!=9) /* wait for it ... */
{ /* not yet */}
else
{
/* in state 9, all the previous if statements have been tested */
s[0]='r'; /* trigger the last, terminal, test branch */
}
/* synchronise function internal state counter */
stringstate++;
return s;
}
事实上,我相信实现80%是可以通过不返回魔法字符串来实现的,那么随机字符最终可能会让你达到目标。但是为什么要在80%停止呢?
看到老师输出迭代,如果看到数以百万计的循环,可能不会给你最好的分数。
对于额外的加分(或者至少是一份点心),我建议使用他们也输出字符串的事实。让测试向老师输出一条诙谐的消息:
“嗨”
“先生”或“妈妈”
“不错”
“目标。”
“可以”
“我”
“有”
“蛋糕?”
“重置”
如果他们带蛋糕来,我会尊重他们。;-
我的回答是,如果他们找不到这个答案,也不会有太多。这就是为什么我建议不要继续读下去。如果你自己这样做,从长远来看,你的期末考试成绩会更好。
真的,真的抵制使用我的解决方案。不要随机测试。阅读要测试的代码定义输入,该输入特别针对要测试的函数的每个路径。您知道传递每个路径是目标,不是吗?在测试时进行调试,观察哪个路径通过,哪个路径不通过,然后退出结束测试以通过丢失的路径。更具体地说:问问自己“我如何执行语句state=9
”,这是你的目标。我的第一条评论是实现这一目标的关键。另外,在实现目标之前不要执行exit(200)
语句。