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

    }

Try this: Your function is exiting the loop after the first iteration whether the username and password are found or not.

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;                                
                }
        }
        if( returnValue == 0)
        {
             cout << "Invalid user login information. Please try again.\n";           
             failCount++;                   
        }

        return returnValue;   

// The following is unreachable code!
        if (failCount == 3)                
         {         
              cout << "You have used maximum attempts to login. Your account has been locked." << endl;
              exit (0);                       
         }

    }

Hi... thanks for the reply. I have made changes as advised but the test is still not successful.

One thing I noticed.. My vector size shows zero (tested using cout) 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 in testAuthenticate are passed when I did that but other unit test functions became invisible. They are not seen by the compiler if that makes senses. I have total 9 test functions but only two is being processed which is testAuthenticate and testEncryption. I do not get any output from other test cases at all, not even fail error.
Any advice, please?

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

I can't help you if you don't post the code that exhibits the problem(s).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.