The generate-unit-tests command uses an integration's Python code to automatically generate unit tests.
This command generates unit tests automatically from an integration's Python code. It also supports generating unit tests for specific commands. Note that this command is not intended to fully replace manual work on unit tests but is intended to make it easier to write them.
Note
The generate-unit-test command only works if demisto-sdk is installed with pip install demisto-sdk [generate-unit-tests]
.
Argument | Description |
---|---|
-i, --input_path | Path of the integration python file. (Mandatory) |
-c, --commands | The specific commands to generate unit tests for (e.g. xdr-get-incidents). |
-o, --output_dir | Directory to store the command output (generated test file) in. Default directory is the input integration directory. |
-clt, --console_log_threshold | Minimum logging threshold for the console logger. Default value is |
-flt --file_log_threshold | Minimum logging threshold for the file logger. Default value is |
-lp, --log_file_path | Path to the log file. Default path is |
-e, --examples | One of the following:
|
-d, --use_demisto | If passed, the Cortex XSOAR instance configured in the DEMISTO_BASE_URL and DEMISTO_API_KEY environment variables runs the integration commands and generates outputs which is used as mock outputs. NoteIf this flag is not passed, you need to create the mocks manually, at the outputs directory, with the name of the command. |
--insecure | Skip certificate validation when authorizing Cortex XSOAR or Cortex XSIAM. |
-a, --append | Append the generated unit tests to an existing file. Only works if there is an existing file. |
The outputs from the command run are written in an output file in the given directory.
Every command method name must have the suffix
_command
.Every command method must have the
args
dictionary parameter.Each argument access in
args
must be made using theget()
method.Every command method must have the
client
parameter which must be typed (client: Client
, whereClient
extendsBaseClient
).Each HTTP request made during the flow of the command must be done using the
_http_request()
method. Themethod
must be passed.Every command must return a
CommandResults
object.Client class must include
__init__
function.Client class
__init__
function arguments have to include types. If not, none will be passed.
For the unit tests to run successfully, a test_data
folder must exist and include:
outputs folder - Contains a JSON file for each HTTP request with a mock response. The file name should be the name of the client function making the HTTP call.
outputs command files - If the
--use_demisto
flag was not given, outputs files must be provided for each command. File names should have the same name as the command. See examples files below to view the desired structure for the file.
Command Executions
demisto-sdk generate-unit-tests -i Packs/MyPack/Integrations/MyInt/MyInt.py -d --insecure
demisto-sdk generate-unit-tests -i Packs/MyPack/Integrations/MyInt/MyInt.py -o Packs/MyPack/Integrations/MyInt
demisto-sdk generate-unit-tests -i Packs/MyPack/Integrations/MyInt/MyInt.py -c MyInt_example_command
For the command to work as planned, a command_examples
file must be provided.
command_examples
File
!malwarebazaar-samples-list sample_type=tag sample_value=test limit=2 !malwarebazaar-samples-list sample_type=tag1 sample_value=test2 limit=444 !malwarebazaar-download-sample sha256_hash=1234 !file file=1234 !malwarebazaar-comment-add comment="test" sha256_hash=1234
Command Output File
malwarebazaar_comment_add_command.json
{"readable_output": "Comment added to 1234 malware sample successfully", "outputs": {"comment": "test", "sha256_hash": "1234"}}