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

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.

EDIT: here’s a very advanced example that shows how to listen to the process output / log, collect the log, and conditionally exit: fork-listener.feature

Another answer which can be a useful reference: Conditional match based on OS

Leave a Comment