How to execute bash script using karate and fail if script fails

时光毁灭记忆、已成空白 提交于 2021-01-16 02:38:22

问题


I'm trying to execute bash script using karate. I'm able to execute the script from karate-config.js and also from .feature file. I'm also able to pass the arguments to the script. The problem is, that if the script fails (exits with something else than 0) the test execution continues and finishes as succesfull.

I found out that when the script echo-es something then i can access it as a result of the script so I could possibly echo the exit value and do assertion on it (in some re-usable feature), but this seems like a workaround rather than a valid clean solution. Is there some clean way of accessing the exit code without echo-ing it? Am I missing on something?

script

#!/bin/bash

#possible solution
#echo 3

exit 3;

karate-config.js

var result = karate.exec('script.sh arg1')

feture file

def result = karate.exec('script.sh arg1')

回答1:


Great timing. We very recently did some work for CLI testing which I am sure you can use effectively. Here is a thread on Twitter: https://twitter.com/maxandersen/status/1276431309276151814

And we have just released version 0.9.6.RC4 and new we have a new karate.fork() option that returns an instance of Command on which you can call exitCode

Here's an example:

* def proc = karate.fork('script.sh arg1')
* proc.waitSync()
* match proc.exitCode == 0

You can get more ideas here: https://github.com/intuit/karate/issues/1191#issuecomment-650087023

Note that the argument to karate.fork() can take multiple forms

  • string - full command line as seen above
  • string array - e.g. ['script.sh', 'arg1']
  • json where the keys can be
    • line - string (OR)
    • args - string array
    • env - optional environment properties (as JSON)
    • redirectErrorStream - boolean, true by default which means Sys.err appears in Sys.out
    • useShell - default false, auto-prepend cmd /c or sh -c depending on OS

And since karate.fork() is async, you need to call waitSync() if needed as in the example above.

Do provide feedback and we can tweak further if needed.



来源:https://stackoverflow.com/questions/62910721/how-to-execute-bash-script-using-karate-and-fail-if-script-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!