简体   繁体   中英

cppunit only accept return value zero

I am new to CPPUnit testing and have written test for login authentication function with various test cases. 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. 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.

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. I have a function to read text file data to vector. So I called the function from CPPUnit testAuthenticate function. 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. 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 . Therefore your loop will always end after comparing to the first element of 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.

You need to remove the break in the else -block only. In fact your else block should only be executed if no user/password combination in cashierVector matches. 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. You need to move it before the return statement. 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) .

On a side node: This is a use case for a map instead of a 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) . Here you assign false to validUser and then test the return, which is false . Consequently this if block will never be executed. You should change it to if(validUser==false) or better if(!valid_user) . You are also missing a return statement in that block. If it is executed you don't want the following part to be executed, too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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