簡體   English   中英

cppunit只接受返回值零

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM