Overview¶
Some test scenarios require to execute Ruby code through an external Ruby installation, for example if it makes use of a Gem that can't be installed using Squish's own Ruby installation.
Prerequisite¶
Install Ruby side by side with Squish, for example using an installer from https://rubyinstaller.org/ . In addition, install any Ruby Gem that is required by your script using this Ruby installation.
For example, assuming a Ruby installation at C:\Ruby
, to install the Ruby Gem rubyzip, execute the following command (with a cmd.exe instance runs as Administrator/elevated):
Simple script execution through external interpreter¶
Save the Ruby code that should be executed through the external Ruby installation in a separate script file. In this article, we are assuming that the script that should be executed exists at C:\Scripts\externalScript.rb
:
The script can be executed from within a Ruby Test Case by executing the external Ruby interpreter at C:\Ruby\bin\ruby
. In the following example, backticks ("`") are used to call the script.
Passing arguments to external scripts¶
Passing simple arguments¶
You can pass simple arguments the the external Ruby script. To do this, use the arguments command-line option of the Ruby interpreter. For example, this script passes two strings to the external script:
The external script can finally access the arguments:
Passing complex arguments¶
In case you want to pass complex data to the external script, serialization can be used. For example, the following script makes use of the YAML module to serialize an instance of the Container class:
To get an object out of the serialized object, the external script has to call the load
method:
Returning data from external script¶
Returning simple data¶
There are use cases where resulting data of the external script should be used in the calling Test Case. To do this, the external script can simply print the data.
In the Test Case that calls the external script, we can fetch the return value as shown below:
Returning complex data¶
There are use cases where resulting data of the external script should be used in the calling Test Case. To return complex data, YAML object serialization can be used. For example, we can return an instance of the following class:
To do this, we need to serialize the instance in the external script and print the resulting string represenation:
The external script can now be executed as shown earlier. Finally, the object can be restored with the help of YAML.