lundi 28 décembre 2015

Grouping tests that are located in the same class

I am working on my interview prep and the way organize my work is that each question gets its own class. This is convenient because I can group the problem statement, implementation and testing into one place making it easy to relook at problems in the future. However, I have been trying to find a way to group test cases together within that single class. If I were splitting the testing and the implementation into separate classes I could just set up an additional test suit class like this.

In my case, I have two methods for checking if a tic tac toe board is complete. One is for only 3 by 3 boards and the other is generic for N by N boards. I have a method for testing them both and a lot of "basic" tests:

    public void doTestBasic(char expected, char[][] board){
        assertThat("Only 3 by 3", expected, is(checkForWinner3By3(board)));
        assertThat("Generic", expected, is(checkForWinnerGeneric(board)));
    }

    @Test
    public void testSimpleRow(){
        char[][] board = {
                {'X','X', 'X'},
                {' ',' ', ' '},
                {' ',' ', ' '}};
        char expected = 'X';
        doTestBasic(expected, board);
    }

    @Test
    public void testSimpleCol(){
        char[][] board = {
                {'X',' ', ' '},
                {'X',' ', ' '},
                {'X',' ', ' '}};
        char expected = 'X';
        doTestBasic(expected, board);
    }

    @Test
    public void testBasicTie(){
        char[][] board = {
                {'X','O', 'O'},
                {'O','X', 'X'},
                {'O','X', 'O'}};
        char expected = 'N';
        doTestBasic(expected, board);
    }

And then I more tests for only larger boards like:

    @Test
    public void testHigherEmpty(){
        char[][] board = {
                {' ',' ', ' ', ' '},
                {' ',' ', ' ', ' '},
                {' ',' ', ' ', ' '},
                {' ',' ', ' ', ' '}};

        char expected = 'N';
        assertEquals(expected, checkForWinnerGeneric(board));

    }


    @Test
    public void testHigherRow(){
        char[][] board = {
                {'X','X', 'X', 'X'},
                {' ',' ', ' ', ' '},
                {' ',' ', ' ', ' '},
                {' ',' ', ' ', ' '}};

        char expected = 'X';
        assertEquals(expected, checkForWinnerGeneric(board));

    }

I want the two different kinds of tests to appear in two different groups when run instead of one long list, all while staying inside the one class file for the problem. Note that I am using Java 8 and JUnit4.

Aucun commentaire:

Enregistrer un commentaire