Skip to content
Joshua Gleitze edited this page Mar 22, 2015 · 5 revisions

To provide good readability of your tests, please stick to the following style guidelines:

Use our formatter config

The repository contains xml configuration files for the Eclipse Formatter tool. Please import it and apply it before commiting!

One method for one thing

Create exactly one method to test one thing.

Examples:

  • Testing a search command's parsing syntax:
    • create one testCorrectSearch() method, not testCorrectSearch1(), testCorrectSearc2(), etc.
    • create another method for invalid searches, like testInvalidSearch(). Do not put invalid searches into testCorrectSearch().

Use suites

Tests with more than some hundred lines are getting very hard to read. Use JUnit Test Suites to split them up into multiple files.

  • Only put the test suite running all tests in the top level package of your test
  • Create a subpackage subtests for the different test classes.

Examples:

  • Sheet 6 Task C has:
    • BookDatabaseTest in the package sheet6.c_bookDatabase. It runs all the subtests.
    • BookDatabaseSubtest in the package sheet6.c_bookDatabase.subtests contains some common fields and methods for subtests.
    • BasicCommandsTest in the package sheet6.c_bookDatabase.subtests tests whether the program supports the basic commands
    • InputFileParsingTest in the package sheet6.c_bookDatabase.subtests tests wheter the correctly reads in input files
    • etc.

Testing an interactive console

If you want to test a interactive console, make your test extend InteractiveConsoleTest and use its methods.

command and expected result before the test method

  • State the command you will be running and the result you expect before running the test. Use the appropriate fields of InteractiveConsoleTest (e.g. command, expectedResults, etc.).
  • Use the array notation for multiple commands or multiple expected results. If you want to use matchers, use getMatchers(Matcher...)
  • Use the array notation for input files. Use getFile(String[]) of the Input class to pass the file to the program.

Examples:

  •  command = "search title=Musterbuch";
     expectedResult = "creator=galileocomputing,title=java_ist_auch_eine_insel,year=unknown,false";
     oneLineTest(command, expectedResult, args);
     
  •  commands = new String[] {
         "command1",
         "command2",
         "etc."
     };
     expectedResults = new String[] {
         "result1",
         "result2",
         "etc."
     };
     multiLineTest(commands, expectedResults, args);
    
-
        file = new String[] {
                "creator=galileocomputing,title=java_ist_auch_eine_insel",
                "title=grundkursprogrammieren_in_java,year=2007",
                "creator=ralf_reussner,year=2006"
        };
        commands = new String[] {
                "search creator=ralf_reussner",
                "search year=2006",
                "search AND(creator=ralf_reussner,year=2006)",
                "search OR(creator=ralf_reussner,year=2006)",
                "search isbn=12345",
                "quit"
        };
        // @formatter:off
        expectedResultMatchers = getMatchers(
                is("creator=galileocomputing,title=java_ist_auch_eine_insel,year=unknown,false"),
                is("creator=unknown,title=grundkursprogrammieren_in_java,year=2007,false"),
                is("creator=ralf_reussner,title=unknown,year=2006,true"),
            is("creator=galileocomputing,title=java_ist_auch_eine_insel,year=unknown,false"),
            is("creator=unknown,title=grundkursprogrammieren_in_java,year=2007,true"),
            is("creator=ralf_reussner,title=unknown,year=2006,true"),
            
            is("creator=galileocomputing,title=java_ist_auch_eine_insel,year=unknown,false"),
            is("creator=unknown,title=grundkursprogrammieren_in_java,year=2007,false"),
            is("creator=ralf_reussner,title=unknown,year=2006,true"),
            
            is("creator=galileocomputing,title=java_ist_auch_eine_insel,year=unknown,false"),
            is("creator=unknown,title=grundkursprogrammieren_in_java,year=2007,true"),
            is("creator=ralf_reussner,title=unknown,year=2006,true"),
            
            startsWith("Error,")
     );
    // @formatter:on

    multiLineTest(commands, expectedResultMatchers, "0.5", Input.getFile(file));