我在这里要疯了,所以请耐心等待......我们正在使用 Fitnesse(使用 DbFit 框架/基于 FIT)来自动化一些运行一些 shell 命令的测试。我们有一个连接到 linux 服务器的夹具,运行命令并返回结果(见下文)
class SSHConnection {
private static final String DEFAULT_KEY = "~/.ssh/id_rsa";
private String host;
private String password;
private int port;
private String privateKey;
private Session session;
private String user;
/** Creates a JSch object and open a connection with a remote server using the values set in the class variables.
*/
public void connect() {.........}
/**
* Executes a command in the remote server using the connection opened by connect().
* @param command command to execute in the remote server
* @return the output result of the command as String
*/
public String execute(String command) {
logger.info("Executing command: " + command);
String result;
try {
ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
channelExec.setCommand(command);
// Open exec channel
channelExec.connect();
InputStream stream = channelExec.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
StringBuilder output = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
output.append(line).append("\n");
}
result = output.toString();
// Close exec channel
channelExec.disconnect();
logger.debug("Command executed successfully: " + result);
} catch (JSchException | IOException e) {
logger.error("Error executing command: " + e.getMessage());
e.printStackTrace();
return "";
}
return result;
}}
因此,我期望在运行命令后返回(作为字符串)并与我在 Fitnesse 中的测试所需的任何内容进行比较后,shell 上显示的任何内容。
Fitnesse 捕捉到结果但总是比较失败,我不知道为什么(我只添加了 sed 命令来删除空格,但比较仍然失败!!
我觉得 Fitnesse 在嘲笑我向我展示了预期、实际和差异的相同价值。是和编码问题吗?是java类型的问题吗?检查如何工作?
编辑:我什至尝试两次运行 shell 命令并第一次保存结果,然后将其设置为预期结果。它仍然失败。
|set | VarAddress | run command | cat AddressNoSpaces.txt |
|check| run command | cat AddressNoSpaces.txt | @{VarAddress} |