> For the complete documentation index, see [llms.txt](https://cortex-docs.paloaltonetworks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cortex-docs.paloaltonetworks.com/demisto-sdk-development-guide/demisto-sdk-guide/demisto-sdk-commands/generate-unit-tests.md).

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

{% hint style="info" %}

### Note

The `generate-unit-tests` command only works if `demisto-sdk` is installed with `pip install demisto-sdk [generate-unit-tests]`.
{% endhint %}

<details>

<summary>Arguments</summary>

| 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`                | <p>One of the following:</p><ul><li>A path for a file containing Integration command examples. Each command example should be in a separate line.</li><li>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.</li></ul>                                                                                                                                                 |
| `-d, --use_demisto`             | <p>If passed, the Cortex XSOAR instance configured in the <code>DEMISTO\_BASE\_URL</code> and <code>DEMISTO\_API\_KEY</code> environment variables runs the integration commands and generates outputs which are used as mock outputs.</p><div data-gb-custom-block data-tag="hint" data-style="info" class="hint hint-info"><p><strong>Note</strong></p><p>If this flag is not passed, you need to create the mocks manually, at the outputs directory, with the name of the command.</p></div> |
| `--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.                                                                                                                                                                                                                                                                                                                                                                                                    |

</details>

<details>

<summary>Notes</summary>

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

</details>

<details>

<summary>Required Code Conventions</summary>

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

</details>

<details>

<summary>Test Data Files</summary>

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.

</details>

<details>

<summary>Examples</summary>

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

```programlisting
!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`

```programlisting
{"readable_output": "Comment added to 1234 malware sample successfully",
  "outputs": {"comment": "test", "sha256_hash": "1234"}}
```

</details>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://cortex-docs.paloaltonetworks.com/demisto-sdk-development-guide/demisto-sdk-guide/demisto-sdk-commands/generate-unit-tests.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
