Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
618 views
in Technique[技术] by (71.8m points)

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

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')
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...