[英]cppunit only accept return value zero
我是CPPUnit测试的新手,并且针对各种测试案例编写了针对登录身份验证功能的测试。 但它只接受返回值零。
如果期望值和实际值相同,则第一个测试用例应返回1,但仅当我更改为零时才有效。 任何建议表示赞赏。 谢谢。
void POSUnitTest::testAuthenticate()
{
CPPUNIT_ASSERT_EQUAL (console.Authenticate("testuser","password"),1); // valid username and password
CPPUNIT_ASSERT_EQUAL (console.Authenticate("testuser1","password"),0); //invalid username, valid password
CPPUNIT_ASSERT_EQUAL (console.Authenticate("testuser","password1"),0); //valid username, invalid password
CPPUNIT_ASSERT_EQUAL (console.Authenticate("testuser1","password1"),0); // invalid username and password
CPPUNIT_ASSERT_EQUAL (console.Authenticate(" ","password"),0); // Empty username, valid password
CPPUNIT_ASSERT_EQUAL (console.Authenticate("testuser",""),0); // Valid username, empty password
CPPUNIT_ASSERT_EQUAL (console.Authenticate("",""),0); // Empty username and password
cout << "Test 3) testAuthenticate successful.\n\n";
}
这是我在POSConsole类中的Authenticate函数。
int POSConsole::Authenticate(string _userID, string _password)
{
bool failedLogin=false;
int returnValue=0;
_password = Encrypt (_password); //Encrypt user enter password to compare with vector
for (int index = 0; index < cashierVector.size(); index++)
{
if ( (_userID == cashierVector[index].getUserID()) &&
(_password == cashierVector[index].getPassword()))
{
returnValue=1;
system("clear");
POSMenu();
break;
}
else
{
returnValue=0;
cout << "Invalid user login information. Please try again.\n";
failCount++;
break;
}
}
return returnValue;
if (failCount == 3)
{
cout << "You have used maximum attempts to login. Your account has been locked." << endl;
exit (0);
}
}
编辑:我认为我已经找到问题了。 运行CPPUnit测试时,矢量大小显示为零。 我具有将文本文件数据读取到矢量的功能。 所以我从CPPUnit testAuthenticate函数调用了该函数。 现在已通过所有测试用例,但其他单元测试功能变得不可见。 如果可以的话,编译器不会看到它们。 我总共有10个测试功能,但只有两个正在处理中。 我根本没有从其他测试用例中得到任何输出,甚至没有失败错误。
请问我该如何解决? 感谢您的帮助,因为我在这方面已经停留了几天。
void POSUnitTest::testAuthenticate()
{
console.readData();
CPPUNIT_ASSERT_EQUAL (console.Authenticate("testuser","password"),1); // valid username and password
CPPUNIT_ASSERT_EQUAL (console.Authenticate("testuser1","password"),0); //invalid username, valid password
CPPUNIT_ASSERT_EQUAL (console.Authenticate("testuser","password1"),0); //valid username, invalid password
CPPUNIT_ASSERT_EQUAL (console.Authenticate("testuser1","password1"),0); // invalid username and password
CPPUNIT_ASSERT_EQUAL (console.Authenticate(" ","password"),0); // Empty username, valid password
CPPUNIT_ASSERT_EQUAL (console.Authenticate("testuser",""),0); // Valid username, empty password
CPPUNIT_ASSERT_EQUAL (console.Authenticate("",""),0); // Empty username and password */
cout << "Test 3) testAuthenticate successful.\n\n";
}
这是修改后的功能:
int POSConsole::Authenticate(string _userID, string _password)
{
bool validUser=false;
bool failedLogin=false;
int returnValue=0;
_password = Encrypt (_password);
int vectorzie = cashierVector.size();
cout << "\nVector size" << vectorzie << endl;
for (int index = 0; index < cashierVector.size(); index++)
{
if ( (_userID == cashierVector[index].getUserID()) &&
(_password == cashierVector[index].getPassword()))
{
validUser = true;
returnValue=1;
cout << "\nOk Login Return Value: " << returnValue; //this show 1
system("clear");
POSMenu();
break;
}
}
if (validUser=false) //I have changed to this from else statement
{
failedLogin=true;
returnValue=2;
cout << "\nfail Login Return Value: " << returnValue;
cout << "\nInvalid user login information. Please try again.\n";
failCount++;
cout << "\nfail count: " << failCount << endl;
}
cout << "Final Login Return Value: " << returnValue;
if (failCount == 3)
{
cout << "You have used maximum attempts to login. Your account has been locked." << endl;
exit (0);
}
return returnValue;
}
对于if ... else
两种情况,您都有一个break
语句。 因此,在与cashierVector
的第一个元素进行比较之后,循环将始终结束。
如果删除两个break
,则即使已经找到用户,循环也将继续运行,并且returnValue
将再次重置为0
。
您只需要删除else
-block中的break
。 实际上,只有在cashierVector
中没有用户/密码组合匹配时,才应执行else
块。 因此,应将其放置在循环之后,而不是放置在循环中。
return returnValue
之后的块将永远不会到达该函数,因为该点已经返回了。 您需要将其移动到return语句之前。 但是,如果这样做,则单元测试将永远不会成功,因为在用错误的密码第三次调用Authenticate
之后(如在测试中一样),程序将通过exit(0)
。
在侧面节点上:这是map
的用例,而不是vector
的用例。 如果您有很多用户,则搜索将花费很长时间。 在地图中,这要快得多,并且不需要循环。
编辑后:
if (validUser=false)
。 在这里,将false
分配给validUser
,然后测试返回值false
。 因此,此if
块将永远不会执行。 您应该将其更改为if(validUser==false)
或更好的if(!valid_user)
。 您还缺少该块中的return语句。 如果执行了,您也不想执行以下部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.