Bats core 有没有办法摆脱蝙蝠测试?

Bats core 有没有办法摆脱蝙蝠测试?,bats-core,Bats Core,有没有办法摆脱整个测试文件?整个测试套件 类似于 @test 'dependent pgm unzip' { command -v unzip || BAIL 'missing dependency unzip, bailing out' } 编辑: 我可以做类似的事情 #!/usr/bin/env bats if [[ -z "$(type -t unzip)" ]]; then echo "Missing dep unzip" exit 1 fi @test ... 这对

有没有办法摆脱整个测试文件?整个测试套件

类似于

@test 'dependent pgm unzip' {
  command -v unzip || BAIL 'missing dependency unzip, bailing out'
}
编辑:

我可以做类似的事情

#!/usr/bin/env bats

if [[ -z "$(type -t unzip)" ]]; then
  echo "Missing dep unzip"
  exit 1
fi

@test ...
这对于在测试开始时运行的检查很好,只是它不会作为报告的一部分输出

但如果我想确定一个源代码脚本是否正确定义了一个函数,如果它没有,那么添加这种测试可以防止生成任何类型的报告。不显示成功的测试。

TL;博士
  • 要查看中止消息,请使用
    &2
    将全局范围内的消息重定向到
    stderr
  • 要在失败后中止所有文件,请在全局范围内使用
    退出1
  • 要仅中止单个文件,请创建一个
    setup
    函数,该函数使用
    skip
    仅中止该文件中的测试
  • 要使单个文件中的测试失败,请创建一个
    setup
    函数,该函数使用
    return 1
    使该文件中的测试失败

答案更详细 中止所有文件 你的第二个例子就快到了。诀窍是将输出重定向到
stderr

使用全局范围中的
exit
return 1
将停止整个测试套件

#!/usr/bin/env bats

if [[ -z "$(type -t unzip)" ]]; then
  echo "Missing dep unzip" >&2
  return 1
fi

@test ...
缺点是,在中止的文件中和之后的任何测试都不会运行,即使这些测试要通过

中止单个文件 更细粒度的解决方案是添加一个
setup
函数,如果不存在依赖项,该函数将
skip

由于
setup
函数是在文件中定义的每个测试之前调用的, 如果缺少依赖项,则将跳过该文件中的所有测试

#!/usr/bin/env bats

setup(){
    if [[ -z "$(type -t unzip)" ]]; then
        skip "Missing dep unzip"
    fi
}

@test ...
失败而不是跳过 也有可能使具有未满足依赖性的测试失败。使用
返回1
从a测试的
设置
功能将使该文件中的所有测试失败:

#!/usr/bin/env bats

setup(){
    if [[ -z "$(type -t unzip)" ]]; then
        echo "Missing dep unzip"
        return 1
    fi
}

@test ...
由于消息输出不在全局范围内,因此不必将其重定向到
sdterr
(尽管这样也可以)

脚注
  • 手册底部和手册中都提到了这一点(如果您运行
    man 7 bats
    ):

  • 有关
    设置的详细信息,请参见

  • 有关跳过的详细信息,请参见


  • 为了确保我理解你的要求,你想知道三件事吗?1.如何中止单个测试文件的其余部分2。如何中止所有测试文件的其余部分3。如何输出中止消息和其他测试输出(即成功测试)。对吗?对,对。谢谢。这帮了大忙。一个极好的回答。
     CODE OUTSIDE OF TEST CASES
    
         You can include code in your test file outside of @test functions.
         For example, this may be  useful  if  you  want  to check for
         dependencies and fail immediately if they´re not present. However,
         any output that you print in code outside of @test, setup or teardown
         functions must be redirected to stderr (>&2). Otherwise, the output
         may cause Bats to fail by polluting the TAP stream on stdout.