问题
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 arrayenv
- optional environment properties (as JSON)redirectErrorStream
- boolean, true by default which means Sys.err appears in Sys.outuseShell
- default false, auto-prependcmd /c
orsh -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