测试 pico cli 命令,Java

Testing pico cli commands, Java

我开发了一个 Java API 并且我构建了一个命令行界面来使用它 PicoCli

测试我的 pico 命令的正确方法是什么?

提前致谢

您可以通过验证退出代码和程序输出到标准输出流和标准错误流来进行黑盒测试。

您可以通过保留对应用程序的引用并在提供各种命令行输入后断言应用程序的状态来进行白盒测试。

例如:

MyApp app = new MyApp();
StringWriter sw = new StringWriter();
CommandLine cmd = new CommandLine(app);
cmd.setOut(new PrintWriter(sw));

// black box testing 
int exitCode = cmd.execute("-x", "-y=123");
assertEquals(0, exitCode);
assertEquals("Your output is abc...", sw.toString());

// white box testing 
assertEquals("expectedValue1", app.getState1());
assertEquals("expectedValue2", app.getState2());

Update: the picocli user manual now has a separate section about Testing.