我的最终目标是覆盖以下代码(使整个代码变绿)。我正在使用康塔塔工具。
#define CHECK1 ((a == 1) || (a == 4))
void check_fun(int a, int b)
{
if((!CHECK1)&&(b>0)&&(b<10))
{
}
}
我已经写了5个测试用例如下
void test_check_fun(int doIt){
if (doIt) {
/* Test case data declarations */
int a;
int b;
START_TEST("test_check_fun_001",
"<Entry condition: True check for if((!((a == 1) || (a == 4)))&&(b>0)&&(b<10)) ((!(F||F))&&(T)&&(T)) equalent to (T&&T&&T)>");
/* Expected Call Sequence */
EXPECTED_CALLS("");
/* Set global data */
initialise_global_data();
a = 3;
b = 5;
/* Set expected values for global data checks */
initialise_expected_global_data();
/* Call SUT */
check_fun(a, b);
/* Test case checks */
/* Checks on global data */
check_global_data();
END_CALLS();
END_TEST();
START_TEST("test_check_fun_002",
"<Entry condition: False check for if((!((a == 1) || (a == 4)))&&(b>0)&&(b<10)) ((!(T||F))&&(T)&&(T)) equalent to (F&&T&&T)>");
/* Expected Call Sequence */
EXPECTED_CALLS("");
/* Set global data */
initialise_global_data();
a = 1;
b = 5;
/* Set expected values for global data checks */
initialise_expected_global_data();
/* Call SUT */
check_fun(a, b);
/* Test case checks */
/* Checks on global data */
check_global_data();
END_CALLS();
END_TEST();
START_TEST("test_check_fun_003",
"<Entry condition: False check for if((!((a == 1) || (a == 4)))&&(b>0)&&(b<10)) ((!(F||T))&&(T)&&(T)) equalent to (F&&T&&T)>");
/* Expected Call Sequence */
EXPECTED_CALLS("");
/* Set global data */
initialise_global_data();
a = 4;
b = 5;
/* Set expected values for global data checks */
initialise_expected_global_data();
/* Call SUT */
check_fun(a, b);
/* Test case checks */
/* Checks on global data */
check_global_data();
END_CALLS();
END_TEST();
START_TEST("test_check_fun_004",
"<Entry condition: False check for if((!((a == 1) || (a == 4)))&&(b>0)&&(b<10)) ((!(F||F))&&(F)&&(T)) equalent to (T&&F&&T)>");
/* Expected Call Sequence */
EXPECTED_CALLS("");
/* Set global data */
initialise_global_data();
a = 3;
b = -1;
/* Set expected values for global data checks */
initialise_expected_global_data();
/* Call SUT */
check_fun(a, b);
/* Test case checks */
/* Checks on global data */
check_global_data();
END_CALLS();
END_TEST();
START_TEST("test_check_fun_005",
"<Entry condition: False check for if((!((a == 1) || (a == 4)))&&(b>0)&&(b<10)) ((!(F||F))&&(T)&&(F)) equalent to (T&&T&&F)>");
/* Expected Call Sequence */
EXPECTED_CALLS("");
/* Set global data */
initialise_global_data();
a = 3;
b = 11;
/* Set expected values for global data checks */
initialise_expected_global_data();
/* Call SUT */
check_fun(a, b);
/* Test case checks */
/* Checks on global data */
check_global_data();
END_CALLS();
END_TEST();
}}
结果显示为
===========================================================================
= Cantata Test Harness v6.2 =
= (c) 2012 QA Systems GmbH =
=-------------------------------------------------------------------------=
= Test Description: check =
= Log File: test_check.ctr =
= Test Started: Wed Jun 24 12:10:56 2015 =
===========================================================================
===========================================================================
= Test Finished: Wed Jun 24 12:10:56 2015 =
=-------------------------------------------------------------------------=
= Test Script Checks Checks Checks Call Seq TEST =
= Case Errors Failed Passed Warned Failures RESULT =
=-------------------------------------------------------------------------=
= test_check_fun_001 0 0 0 3 0 PASS =
= test_check_fun_002 0 0 0 3 0 PASS =
= test_check_fun_003 0 0 0 3 0 PASS =
= test_check_fun_004 0 0 0 3 0 PASS =
= test_check_fun_005 0 0 0 3 0 PASS =
= Other 0 0 4 0 0 PASS =
=-------------------------------------------------------------------------=
= TOTALS 0 0 4 15 0 PASS =
===========================================================================
我的问题是有些代码没有显示为绿色。所以请告诉我我错过了哪些测试用例来覆盖以下条件
if((!((a == 1) || (a == 4)))&&(b>0)&&(b<10))
请帮我一下。提前谢谢。
这是为什么代码覆盖率有点灰的一个例子。它作为一个指标很有用,但它不是透明的。
如果我将代码剪切并粘贴到C#中,并运行相同的测试,我可以获得100%的代码覆盖率,那么有什么区别呢?
根据您的编译器设置(您应该在关闭所有编译器优化的情况下测试覆盖率),C编译器实际上可能正在创建额外的代码来优化某些流,因此即使在您的逻辑中(b<10)
只是一个检查,它也可能导致不同流在其余逻辑中使用两个单独的代码指令。如何报告将取决于您的代码覆盖率工具如何评估覆盖率。
如果真的想弄清楚是什么导致覆盖率下降,那么你可以尝试删除检查的不同元素,看看它有什么影响。你也可以尝试重新排列你的公式。在所有条件相同的情况下,最好先在逻辑中出现简单的故障,因为这样可以减少代码中的流量。因此,在下面的版本中,尽管结果应该是相同的,但由于&&
在||
之前,代码的构建可能会有所不同。
if((b>0)&&(b<10)&&(!((a == 1) || (a == 4))))
您还可以删除括号,看看这是否能提高覆盖率。以下返回相同的结果,同时使测试内容更加明确。
if (b<10 && b>0 && a!=1 && a!=4)