如何测试命令行界面(CLI)?

Joh*_*ood 4 java testing command-line-interface

我的Java应用程序由两部分组成:

  1. 核心库(类,接口等)
  2. 命令行界面(CLI),它使用核心库

对于1.我使用JUnit进行单元测试,但是你会为2做什么?

如何为命令行界面创建自动化测试?

edd*_*ddy 8

我有完全相同的问题,落在这里并没有找到一个好的答案,所以我想我会发布解决方案,我最终将作为未来降落在这里的任何人的起点.

我在CLI之后编写了测试(对我来说很遗憾,我知道),所以首先我确保CLI是以可测试的方式编写的.它看起来像这样(我省略了异常处理并简化了许多以使其更具可读性):

public class CLI {

    public static void main(String... args) {
        new CLI(args).startInterface();
    }

    CLI(String... args) {
        System.out.println("Welcome to the CLI!");
        // parse args, load resources, etc
    }

    void startInterface() {
        BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            String[] input = sanitiseInput(consoleReader.readLine());

            if (input[0].equalsIgnoreCase("help") {
                help();
            } else if (input[0].equalsIgnoreCase("exit") {
                break;
            } else if (input[0].equalsIgnoreCase("save") {
                save(input);
            } else {
                System.out.println("Unkown command.");
            }
        }
    }

    String[] sanitiseInput(String rawInput) {
        // process the input and return each part of it in order in an array, something like:
        return rawInput.trim().split("[ \t]+");
    }

    void help() {
        // print help information
        System.out.println("Helpful help.");
    }

    void save(String[] args) {
        // save something based on the argument(s)
    }
}
Run Code Online (Sandbox Code Playgroud)

接受测试.CLI不是公共图书馆的一部分,因此应该保护它免受图书馆用户的侵害.正如提到这里,你可以使用默认的访问修饰符,使其包专用.这使得您的测试可以完全访问该类(只要它们位于同一个包中),同时仍然可以保护它,因此需要处理.

为CLI接受的每个命令编写方法允许JUnit测试几乎完美地模拟用户输入.由于在调用之前对象不会从stdin读取startInterface(),因此您可以简单地实例化它并测试各个方法.

首先,测试原始输入是否正确清理是很好的,您可以通过编写JUnit测试来轻松完成sanitiseInput().我写了这样的测试:

@Test
public void commandAndArgumentsSeparatedBySpaces() throws Exception {
    String[] processedInput = uut.sanitiseInput("command argument1 argument2");

    assertEquals("Wrong array length.", 3, processedInput.length);
    assertEquals("command", processedInput[0]);
    assertEquals("argument1", processedInput[1]);
    assertEquals("argument2", processedInput[2]);
}
Run Code Online (Sandbox Code Playgroud)

它也很容易覆盖一些边缘情况:

@Test
public void leadingTrailingAndIntermediaryWhiteSpace() throws Exception {
    String[] processedInput = uut.sanitiseInput("  \t  this   \twas \t  \t  a triumph  \t\t    ");

    assertEquals("Wrong array length.", 4, processedInput.length);
    assertEquals("this", processedInput[0]);
    assertEquals("was", processedInput[1]);
    assertEquals("a", processedInput[2]);
    assertEquals("triumph", processedInput[3]);
}
Run Code Online (Sandbox Code Playgroud)

接下来,我们可以通过监视stdout来测试invididual命令方法.我这样做了(我在这里找到):

private CLI uut;
private ByteArrayOutputStream testOutput;
private PrintStream console = System.out;
private static final String EOL = System.getProperty("line.separator");

@Before
public void setUp() throws Exception {
    uut = new CLI();
    testOutput = new ByteArrayOutputStream();
}

@Test
public void helpIsPrintedToStdout() throws Exception {
    try {
        System.setOut(new PrintStream(testOutput));
        uut.help();
    } finally {
        System.setOut(console);
    }
    assertEquals("Helpful help." + EOL, testOutput.toString());
}
Run Code Online (Sandbox Code Playgroud)

换句话说,out在练习之前用JVM替换你可以查询的东西,然后在测试的拆解中重新设置旧控制台.

当然,CLI应用程序通常不仅仅是打印到控制台.假设您的程序将信息保存到文件中,您可以像这样测试它(从JUnit 4.7开始):

@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();

@Test
public void informationIsSavedToFile() throws Exception {
    File testFile = tempFolder.newFile();
    String expectedContent = "This should be written to the file.";

    uut.save(testFile.getAbsolutePath(), expectedContent);

    try (Scanner scanner = new Scanner(testFile)) { 
        String actualContent = scanner.useDelimiter("\\Z").next();
        assertEquals(actualContent, expectedContent);
    }
}
Run Code Online (Sandbox Code Playgroud)

JUnit将负责创建一个有效的文件并在测试运行结束时将其删除,让您可以自由地测试CLI方法是否正确处理它.