简体   繁体   English

cppunit只接受返回值零

[英]cppunit only accept return value zero

I am new to CPPUnit testing and have written test for login authentication function with various test cases. 我是CPPUnit测试的新手,并且针对各种测试案例编写了针对登录身份验证功能的测试。 But it only accepts return value zero. 但它只接受返回值零。

The first test case should return 1 if expected and actual are the same but it only works when I change to zero. 如果期望值和实际值相同,则第一个测试用例应返回1,但仅当我更改为零时才有效。 Any advice is appreciated. 任何建议表示赞赏。 Thanks. 谢谢。

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";   
}

This is my Authenticate function in class POSConsole. 这是我在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);                       
         }

    }

Edited: I think I have found the problem. 编辑:我认为我已经找到问题了。 My vector size shows zero when I run CPPUnit testing. 运行CPPUnit测试时,矢量大小显示为零。 I have a function to read text file data to vector. 我具有将文本文件数据读取到矢量的功能。 So I called the function from CPPUnit testAuthenticate function. 所以我从CPPUnit testAuthenticate函数调用了该函数。 All test cases are passed now but other unit test functions became invisible. 现在已通过所有测试用例,但其他单元测试功能变得不可见。 They are not seen by the compiler if that makes senses. 如果可以的话,编译器不会看到它们。 I have total 10 test functions but only two is being processed. 我总共有10个测试功能,但只有两个正在处理中。 I do not get any output from other test cases at all, not even fail error. 我根本没有从其他测试用例中得到任何输出,甚至没有失败错误。

How do I solve this, please? 请问我该如何解决? Appreciate your help as I'm stuck in this for days now. 感谢您的帮助,因为我在这方面已经停留了几天。

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";   
}

Here's the amended function: 这是修改后的功能:

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;                 
    }

You have a break statement for both cases of the if ... else . 对于if ... else两种情况,您都有一个break语句。 Therefore your loop will always end after comparing to the first element of cashierVector . 因此,在与cashierVector的第一个元素进行比较之后,循环将始终结束。

If you remove both break , then your loop will keep running even if the user was already found and returnValue will be reset to 0 again. 如果删除两个break ,则即使已经找到用户,循环也将继续运行,并且returnValue将再次重置为0

You need to remove the break in the else -block only. 您只需要删除else -block中的break In fact your else block should only be executed if no user/password combination in cashierVector matches. 实际上,只有在cashierVector中没有用户/密码组合匹配时,才应执行else块。 So it should be placed after the loop, not in it. 因此,应将其放置在循环之后,而不是放置在循环中。

The block after return returnValue will never be reached by the function, because it already has returned at that point. return returnValue之后的块将永远不会到达该函数,因为该点已经返回了。 You need to move it before the return statement. 您需要将其移动到return语句之前。 However if you do so, then your unit test will never succeed, because after the third call to Authenticate with the wrong password (as you do in the test) the program will exit via exit(0) . 但是,如果这样做,则单元测试将永远不会成功,因为在用错误的密码第三次调用Authenticate之后(如在测试中一样),程序将通过exit(0)

On a side node: This is a use case for a map instead of a vector . 在侧面节点上:这是map的用例,而不是vector的用例。 If you have many users then the search will take very long. 如果您有很多用户,则搜索将花费很长时间。 In a map this is much faster and you don't need a loop. 在地图中,这要快得多,并且不需要循环。

After your edit: 编辑后:

if (validUser=false) . if (validUser=false) Here you assign false to validUser and then test the return, which is false . 在这里,将false分配给validUser ,然后测试返回值false Consequently this if block will never be executed. 因此,此if块将永远不会执行。 You should change it to if(validUser==false) or better if(!valid_user) . 您应该将其更改为if(validUser==false)或更好的if(!valid_user) You are also missing a return statement in that block. 您还缺少该块中的return语句。 If it is executed you don't want the following part to be executed, too. 如果执行了,您也不想执行以下部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM