For the complete documentation index, see llms.txt. This page is also available as Markdown.
Cortex XSOAR 6.xCortex XSOAR 8 (SaaS)Cortex XSOAR 8.x (On-premCortex XSIAM

generate-unit-tests

Generate unit test scaffolding from Cortex integration Python code and command examples.

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-tests command only works if demisto-sdk is installed with pip install demisto-sdk [generate-unit-tests].

Arguments
Argument
Description

-i, --input_path

Path of the integration Python file. (Mandatory)

-c, --commands

The specific commands to generate unit tests for (for example 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 INFO. Possible values are DEBUG, INFO, WARNING, ERROR.

-flt, --file_log_threshold

Minimum logging threshold for the file logger. Default value is DEBUG. Possible values are DEBUG, INFO, WARNING, ERROR.

-lp, --log_file_path

Path to the log file. Default path is <CONTENT_PATH>/demisto_sdk_debug.log.

-e, --examples

One of the following:

  • A path for a file containing Integration command examples. Each command example should be in a separate line.

  • A comma-separated list of examples, wrapped by quotes. If the file or the list contains a command with more than one example, all examples are used as different test cases.

-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 are used as mock outputs.

Note

If 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.

Notes

The outputs from the command run are written in an output file in the given directory.

Required Code Conventions
  • 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 the get() method.

  • Every command method must have the client parameter which must be typed (client: Client, where Client extends BaseClient).

  • Each HTTP request made during the flow of the command must be done using the _http_request() method. The method 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.

Test Data Files

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.

Examples

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

Command output file

malwarebazaar_comment_add_command.json

Last updated

Was this helpful?