对基本的井字棋游戏进行自动化测试
正如标题所说,我正在为 tic tac toe 游戏制作自动化测试用例,但是我在尝试测试的方法中使用扫描仪 nextInt 方法,我只是不明白如何使我的测试用例自动填充输入,而不是比程序询问用户。下面是两个方法:
主类方法:
public static void playGame(String playerOne, String playerTwo, char[][] gameBoard, int playerNum, Scanner input) {
int moveSelected = 0;
while (moveSelected >= 10 || moveSelected <= 0) {
moveSelected = 0;
try {
if (playerNum == 1) {
System.out.println(playerOne + " please input your spot selection(1-9)");
} else {
System.out.println(playerTwo + " please input your spot selection(1-9)");
}
moveSelected = input.nextInt();
}
catch(InputMismatchException e){
System.err.println("Wrong input! Integer Values Only.");
input.next();
}
}
playerMove(moveSelected, playerNum, gameBoard);
displayBoard(gameBoard);
}
测试方法:
public void testplayGame(){
Scanner input = new Scanner(System.in);
char[][] gameBoard = {{' ', '|', ' ', '|', ' '},
{'-', '+', '-', '+', '-'},
{' ', '|', ' ', '|', ' '},
{'-', '+', '-', '+', '-'},
{' ', '|', ' ', '|', ' '}};
for(int i =1; i<10;i++) {
assertEquals(fstTest.playGame("p1","p2",gameBoard,1,input));
}
}
As the title says, I am making automated test cases for a tic tac toe game, however I use the scanner nextInt method within the method I'm trying to test, i just dont understand how to make my test case automatically fill the input rather than the program asking the user. Here are the two methods:
Main class method:
public static void playGame(String playerOne, String playerTwo, char[][] gameBoard, int playerNum, Scanner input) {
int moveSelected = 0;
while (moveSelected >= 10 || moveSelected <= 0) {
moveSelected = 0;
try {
if (playerNum == 1) {
System.out.println(playerOne + " please input your spot selection(1-9)");
} else {
System.out.println(playerTwo + " please input your spot selection(1-9)");
}
moveSelected = input.nextInt();
}
catch(InputMismatchException e){
System.err.println("Wrong input! Integer Values Only.");
input.next();
}
}
playerMove(moveSelected, playerNum, gameBoard);
displayBoard(gameBoard);
}
Test Method:
public void testplayGame(){
Scanner input = new Scanner(System.in);
char[][] gameBoard = {{' ', '|', ' ', '|', ' '},
{'-', '+', '-', '+', '-'},
{' ', '|', ' ', '|', ' '},
{'-', '+', '-', '+', '-'},
{' ', '|', ' ', '|', ' '}};
for(int i =1; i<10;i++) {
assertEquals(fstTest.playGame("p1","p2",gameBoard,1,input));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论