📜  Concordion-执行命令(1)

📅  最后修改于: 2023-12-03 14:40:11.260000             🧑  作者: Mango

Concordion - 执行命令

简介

Concordion是一个用于结合行为驱动开发(BDD)和测试驱动开发(TDD)的Java测试框架。它可以通过标记化页面(HTML或Markdown)和Java类,将自然语言规范转换为可执行的测试。 而执行命令即是通过代码实现对测试的执行过程。

执行命令的实现

以下是在Concordion中执行命令的实现:

声明所需变量

在Java类中声明所需的变量,例如:

public class MyFixture {
    private final String workingDirectory;
    private final String command;

    public MyFixture(String workingDirectory, String command) {
        this.workingDirectory = workingDirectory;
        this.command = command;
    }
}
在HTML或Markdown页面中引用Java类和变量

在测试用例的HTML或Markdown页面中引用Java类和变量,例如:

<html xmlns:concordion="http://www.concordion.org/2007/concordion">
<head>
  <title>Execute Command Example</title>
</head>
<body>
  <h1>Execute Command Example</h1>
  <p>The working directory is <span concordion:echo="${myFixture.workingDirectory}"/></p>
  <p>The command is <span concordion:echo="${myFixture.command}"/></p>
  <p><span concordion:execute="${myFixture.executeCommand()}"/></p>
</body>
</html>
# Execute Command Example
The working directory is {{myFixture.workingDirectory}}
The command is {{myFixture.command}}

{{myFixture.executeCommand()}}
实现命令执行逻辑

在Java类中实现命令执行逻辑,例如:

public class MyFixture {
    // ...

    public String executeCommand() throws IOException, InterruptedException {
        ProcessBuilder pb = new ProcessBuilder(command.split(" "));
        if (workingDirectory != null) {
            pb.directory(new File(workingDirectory));
        }
        pb.redirectErrorStream(true);
        Process process = pb.start();

        StringBuilder output = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            output.append(line).append(System.lineSeparator());
        }

        int exitCode = process.waitFor();
        output.append("Exit Code: ").append(exitCode);

        return output.toString();
    }
}
总结

通过以上介绍,我们了解了在Concordion中执行命令的实现方法,可以根据实际测试需求进行不同的命令执行逻辑的实现,让我们更方便地进行自动化测试。