[英]How do I make a driver function when the function to be tested requires user input?
我正在為我的大學介紹 cs class 做一個項目,我們必須編寫一個“賭場”之類的代碼。 One of the requirements is that the user can select a "test case" function, that, when selected, tests all of our functions. 問題是,我如何測試運行需要大量用戶輸入並使用大量隨機數的函數?
我試過研究如何讓計算機輸入而不是用戶輸入,但我還沒有找到任何結論性的東西。 另外,在提供答案時,請記住這是一個介紹性的 class,因此我的代碼既不干凈也不高效,但這是我學到的。
“賭場”的高/低示例游戲:
void highLowGame(int ¤tBalance)
{
int betAmount;
int baseNum;
int secondNum;
char hOrL;
char continueYOrN;
do
{
cout << "Your current balance is $" << currentBalance << "\nHow much would you like to bet?\n";
cin >> betAmount;
betAmount = correctIntInput(betAmount);
betAmount = validBet(currentBalance, betAmount);
baseNum = randomWithMax(9) + 1;
cout << "The first number is " << baseNum << "\nDo you bet (h)igh, or (l)ow?\n";
cin >> hOrL;
hOrL = correctCharInput(hOrL);
while (!(hOrL == 'l' || hOrL == 'L' || hOrL == 'h' || hOrL == 'H'))
{
cout << "Invalid input, please enter 'H' or 'L'\n";
cin >> hOrL;
}
secondNum = randomWithMax(9) + 1;
cout << "The second number is " << secondNum << "\n";
if ((((hOrL == 'l') || (hOrL == 'L')) && (secondNum < baseNum)) || (((hOrL == 'h') || (hOrL == 'H')) && (secondNum > baseNum)))
{
cout << "Congratulations, you won your bet!\n";
updateBalance(currentBalance, 1, betAmount, true);
cout << "Your new balance is " << currentBalance << "\n";
}
else
{
cout << "Looks like you lost your bet!\n";
updateBalance(currentBalance, 1, betAmount, false);
cout << "Your new balance is " << currentBalance << "\n";
}
if (currentBalance <= 0)
{
cout << "Uh oh, you are bankrupt!\n";
break;
}
cout << "Would you like to play again?\n";
cin >> continueYOrN;
continueYOrN = correctCharInput(continueYOrN);
while (continueYOrN != 'n' && continueYOrN != 'N' && continueYOrN != 'y' && continueYOrN != 'Y')
{
cout << "Invalid input, please enter a new input:\n";
cin >> continueYOrN;
continueYOrN = correctCharInput(continueYOrN);
}
cout << "\n\n\n\n";
}while(!(continueYOrN == 'n' || continueYOrN == 'N'));
教授給出的示例“測試”,它測試了一個名為 updateBalance() 的 function,它只執行通過/失敗條件中列出的功能:
void test()
{
int sampleBalance = 100;
int sampleBet = 50;
int samplePayout = 5;
bool sampleWin = true;
cout << "Testing updateBalance() win...";
updateBalance(sampleBalance, sampleBet, samplePayout, sampleWin);
if (sampleBalance == sampleBalance + samplePayout * sampleBet)
{
cout << "Pass!\n";
}
else
{
cout << "Fail!\n";
}
}
抱歉,highLow 游戲的大代碼塊,我不確定我應該拉出哪些部分,如果有的話。 我什至不知道 output 會發生什么(通過/失敗除外),因為我什至不知道該怎么做教授的要求。 同樣,我對此很陌生,所以我的代碼可能很臟或效率低下,而且我很好,我只需要有關如何完成我的任務的幫助。 提前致謝!
您會注意到教授示例 function 不需要用戶輸入,也沒有隨機元素。
如果我要為此編寫測試,我會將我的方法分解為更小的方法。 然后我會測試沒有用戶輸入和隨機元素的方法。
我注意到您有方法correctIntInput(betAmount)
。 這看起來是一個很好的測試候選者。 編寫一些測試以傳遞已知的好值和壞值,然后驗證它是否給你預期的結果不好。 與validBet(...)
和updateBalance(...)
相同。
這段代碼也可以放入它自己的 function 中並進行測試。
if ((((hOrL == 'l') || (hOrL == 'L')) && (secondNum < baseNum)) || (((hOrL == 'h') || (hOrL == 'H')) && (secondNum > baseNum)))
{
cout << "Congratulations, you won your bet!\n";
updateBalance(currentBalance, 1, betAmount, true);
cout << "Your new balance is " << currentBalance << "\n";
}
else
{
cout << "Looks like you lost your bet!\n";
updateBalance(currentBalance, 1, betAmount, false);
cout << "Your new balance is " << currentBalance << "\n";
}
隨着你做這種類型的事情更多,你會更好地了解如何分解你的代碼,什么類型的代碼容易測試,什么類型的代碼難。 最終,您將到達一個難以測試的東西只是代碼庫的一小部分的地方,並且它的 rest 已被測試覆蓋。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.