[英]Test creation of specific directory and its contents via bats
I am invoking an interactive cli tool I have created (using go
but that's not in the question's scope).我正在调用我创建的交互式 cli 工具(使用
go
但这不在问题的范围内)。
I am performing integration tests on it using a combination of BATS and expect
.我正在使用BATS和
expect
的组合对其进行集成测试。
Here is the specific test suite:下面是具体的测试套件:
@test "Running interactive test - providing clone directory parameter" {
cd test_expect
eval "./basic_interactive.exp"
}
The success of this step is the creation of a specific directory with pre-defined contents.这一步的成功是创建一个具有预定义内容的特定目录。
Since I am new to BATS
I cannot find a way to somehow assert that the command因为我是
BATS
新手,所以我找不到一种方法来以某种方式断言该命令
ls -1 /path/to/directory/that/is/supposed/to/be/created
will equal将等于
file1
file2
file3
etc.等等。
Any suggestions?有什么建议?
I have tried this我试过这个
@test "Running interactive test - providing clone directory parameter" {
cd test_expect
eval "./basic_interactive.exp"
eval "ls -1 path/to/directory/that/is/supposed/to/be/created"
echo $output
}
but it does not print anything.但它不打印任何东西。
If I understand your question correctly, you basically want to run a command and validate the output, correct?如果我正确理解您的问题,您基本上想运行命令并验证输出,对吗?
Quoting from the BATS manual引用BATS 手册
Bats includes a
run
helper that invokes its arguments as a command, saves the exit status and output into special global variablesBats 包含一个
run
helper,它将其参数作为命令调用,将退出状态和输出保存到特殊的全局变量中
The two variables that are available inside a BATS test method to validate output are: BATS 测试方法中可用于验证输出的两个变量是:
$output
, which contains the combined contents of the command's standard output and standard error streams $output
,它包含命令的标准输出和标准错误流的组合内容
the $lines
array, for easily accessing individual lines of output $lines
数组,用于轻松访问各个输出行
Applying this to your example would give us:将此应用于您的示例将为我们提供:
@test "Running interactive test - providing clone directory parameter" {
cd test_expect
./basic_interactive.exp
run ls -1 path/to/directory/that/is/supposed/to/be/created
expected=$(cat <<-TXT
file1
file2
file3
TXT
)
[ "${output}" = "${expected}" ]
}
If you find yourself using BATS more often (or for more complex tests) you might consider using a dedicated asserts library (like bats-assert ) to make your life a bit easier.如果您发现自己更频繁地使用 BATS(或用于更复杂的测试),您可以考虑使用专用的断言库(如bats-assert )让您的生活更轻松。
(Especially the assert_output
command is worth looking into, as it supports literal, partial, and regex matching). (特别是
assert_output
命令值得研究,因为它支持文字、部分和正则表达式匹配)。
To understand why you are not seeing any output, you will need to readthe section in the manual titled "Printing to the terminal" .要了解为什么看不到任何输出,您需要阅读手册中标题为“打印到终端”的部分。 In short it boils down to output only being shown when redirected to file descriptor 3:
简而言之,它归结为仅在重定向到文件描述符 3 时才显示输出:
@test "test with output to terminal" {
echo "# This will show up when you run the test" >&3
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.