对基本的井字棋游戏进行自动化测试

发布于 2025-01-11 03:43:45 字数 1478 浏览 0 评论 0原文

正如标题所说,我正在为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文