Читать книгу Practical Go - Amit Saha - Страница 58

Testing the Main Package

Оглавление

First, let's write the unit test for the main package. The handleCommand() is the key function that also calls the other functions in the package. It is declared as follows:

err := handleCommand(w io.Writer, args []string)

In the test, we will call the function with a slice of strings containing the arguments that the program may call and verify the expected behavior. Let's look at the test configurations:

testConfigs := []struct { args []string output string err error }{ // Tests the behavior when no arguments are specified to // the application { args: []string{}, err: errInvalidSubCommand, output: "Invalid sub-command specified\n" + usageMessage, }, // Tests the behavior when "-h" is specified as an argument // to the application { args: []string{"-h"}, err: nil, output: usageMessage, }, // Tests the behavior when an unrecognized sub-command is // to the application { args: []string{"foo"}, err: errInvalidSubCommand, output: "Invalid sub-command specified\n" + usageMessage, }, }

The complete test is shown in Listing 2.6.

Practical Go

Подняться наверх