我是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测试时,我的矢量大小显示为零。我有一个函数来读取文本文件数据到vector。所以我从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;
}
答案 0 :(得分:1)
break
的两个案例都有if ... else
个陈述。因此,在与cashierVector
的第一个元素进行比较后,您的循环将始终结束。
如果同时删除了break
,那么即使找到了用户,您的循环也会继续投放,returnValue
将再次重置为0
。
您只需删除break
- 块中的else
即可。实际上,只有在else
中没有用户/密码组合匹配时才应执行cashierVector
块。所以它应该放在循环之后,而不是它。
函数永远不会到达return returnValue
之后的块,因为它已经在那时返回了。您需要在return语句之前移动它。但是,如果您这样做,那么您的单元测试将永远不会成功,因为在第三次使用错误密码调用Authenticate
之后(正如您在测试中所做的那样),程序将通过exit(0)
退出。
在辅助节点上:这是map
而非vector
的用例。如果您有很多用户,那么搜索将花费很长时间。在地图中,这要快得多,而且不需要循环。
编辑后:
if (validUser=false)
。在这里,您将false
分配给validUser
,然后测试返回值false
。因此,永远不会执行此if
块。您应该将其更改为if(validUser==false)
或更高if(!valid_user)
。您还缺少该块中的return语句。如果执行它,您也不希望执行以下部分。