Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit testing best practices #1381

Closed
wants to merge 9 commits into from
Closed
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/integrations/unit-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,15 @@ def test_function():

If the function raises a ValueError with proper error message, the test will pass.

## Content Testing Guidelines

dorschw marked this conversation as resolved.
Show resolved Hide resolved
* When testing a command, mock all external requests (e.g. mock `Client` methods), run the command on given inputs, and compare the output to a hardcoded value.
dorschw marked this conversation as resolved.
Show resolved Hide resolved
* Prefer comparing whole outputs (e.g. comparing the output to a `CommandResult` object), to make sure the code behavior is preserved even when others add features and fix issues.
* Use the `@freezegun.freeze_time` decorator on the testing method when the output may vary in different time, date or timezone (e.g. daylight-saving time) in run time.
* Methods that handle errors or exceptions should have multiple tests - one for the _happy path_ (normal functionality, without errors), and one testing method for each error handled (a.k.a _sad path/s_).
* Make sure to sanitize or remove any sensitive data (passwords, tokens, email addresses, personal information, etc.) from unit tests and test data.
* Using `pytest.parametrize` is preferred over loops in a unit test. Use `pytest.param` to give each case an `id` (Learn more [here](https://docs.pytest.org/en/7.1.x/example/parametrize.html))
dorschw marked this conversation as resolved.
Show resolved Hide resolved

dorschw marked this conversation as resolved.
Show resolved Hide resolved
## Troubleshooting Tips

* The `demisto-sdk lint` by default prints out minimal output. If for some reason it is failing and not clear, run the
Expand Down