testing - 通过bats测试特定目录及其内容的创建

标签 testing bats-core

我正在调用我创建的交互式 cli 工具(使用 go 但这不在问题的范围内)。

我正在使用 BATS 的组合对其执行集成测试和期望

这是具体的测试套件:

@test "Running interactive test - providing clone directory parameter" {
    cd test_expect
    eval "./basic_interactive.exp"
}

此步骤的成功是创建了具有预定义内容的特定目录。

由于我是 BATS 的新手,我找不到一种方法来断言该命令

ls -1 /path/to/directory/that/is/supposed/to/be/created

将等于

file1
file2
file3

等等

有什么建议吗?

我已经尝试过了

@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
}

但它不打印任何内容。

最佳答案

如果我正确理解你的问题,你基本上想运行一个命令并验证输出,对吗?

引用自the BATS manual

Bats includes a run helper that invokes its arguments as a command, saves the exit status and output into special global variables

BATS 测试方法中可用于验证输出的两个变量是:

  • $output,包含命令的标准输出标准错误流的组合内容

  • $lines 数组,用于轻松访问各个输出行

将此应用到您的示例中将为我们提供:

@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}" ]
}

如果您发现自己更频繁地使用 BATS(或更复杂的测试),您可能会考虑使用专用的断言库(如 bats-assert )来让您的生活更轻松。

(尤其是 assert_output 命令值得研究,因为它支持文字、部分和正则表达式匹配)。

要了解为什么看不到任何输出,您需要阅读 the section in the manual titled "Printing to the terminal" 。简而言之,它归结为仅在重定向到文件描述符 3 时才显示输出:

@test "test with output to terminal" {
    echo "# This will show up when you run the test" >&3
}

关于testing - 通过bats测试特定目录及其内容的创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63943056/

相关文章:

ruby-on-rails - rr gem assert_received 相当于 mocha gem

ruby-on-rails - 使用 RSpec 测试模块内部的类

java - AWS-Device Farm : remotely trigger AWS device farm script and send parameter to the same

bats-core - 有没有办法从 bat 测试中保释出来?

bash - 如何重用 bat 单元测试

java - 如何使用 MockMvc 向模拟 Controller 发送 http 请求?

javascript - 如果正确应用了内联样式,如何使用 Jest/Enzyme 进行测试

bash - Shell 脚本单元测试 : How to mockup a complex utility program

bash - 如何使用 `bats-mock` 断言 Bash 测试中对模拟脚本的调用

bash - 如何编写可单元测试的 bash shell 代码?