Skip to content

Commit 4028d75

Browse files
authored
Merge pull request #38 from intelowlproject/gsoc2025_refactor_analyzer_test_docs
Gsoc2025 refactor analyzer test docs
2 parents 71af3f3 + 5dd13e5 commit 4028d75

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

docs/IntelOwl/contribute.md

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,40 @@ You may want to look at a few existing examples to start to build a new one, suc
211211
After having written the new python module, you have to remember to:
212212

213213
1. Put the module in the `file_analyzers` or `observable_analyzers` directory based on what it can analyze
214-
2. Remember to use `_monkeypatch()` in its class to create automated tests for the new analyzer. This is a trick to have tests in the same class of its analyzer.
214+
2. **Write Unit Tests for the Analyzer**
215+
- Monkeypatch-based tests are no longer used.
216+
- All analyzers are now tested using pure unit tests that focus only on the business logic. This ensures that our tests are fast, deterministic, and independent of external services.
217+
- To make writing tests easier, base test classes are available for both observable analyzers and file analyzers. These base classes handle all common setup and mocking, so you only need to define analyzer-specific behavior.
218+
`BaseAnalyzerTest` located at:
219+
```
220+
tests/api_app/analyzers_manager/unit_tests/[observable_analyzers|file_analyzers]/base_test_class.py
221+
```
222+
- Place your test file under the appropriate directory (`observable_analyzers` or `file_analyzers`).
223+
- Example structure:
224+
```
225+
tests/
226+
└── api_app/
227+
└── analyzers_manager/
228+
└── unit_tests/
229+
├── observable_analyzers/
230+
│ ├── test_mynewanalyzer.py
231+
│ └── base_test_class.py
232+
└── file_analyzers/
233+
├── ...
234+
```
235+
236+
- Your test case should:
237+
- Each analyzer test class should inherit from the base test class provided in the same directory.
238+
- Define which analyzer is under test by assigning it to analyzer_class - Set `analyzer_class = YourAnalyzerClass`
239+
- Override `get_mocked_response()` to simulate the data your analyzer would normally produce. All external dependencies (e.g., API calls, file I/O, subprocesses) must be mocked — no real external calls should happen.
240+
This method should return a list of all applied patches, ensuring that every external call used by the analyzer is properly mocked.
241+
- Optionally override `get_extra_config()` to provide additional runtime config that are not already defined inside the base test class.
242+
243+
- **For reference**, you can find numerous analyzer test examples already implemented under `tests/api_app/analyzers_manager/unit_tests/observable_analyzers/` and `file_analyzers/`.
244+
245+
246+
> Note: If your analyzer is Docker-based, you can refer to tests/api_app/analyzers_manager/unit_tests/file_analyzers/test_suricate.py for an example of how such analyzers are tested.
247+
215248
3. Create the configuration inside django admin in `Analyzers_manager/AnalyzerConfigs` (\* = mandatory, ~ = mandatory on conditions)
216249
1. \*Name: specific name of the configuration
217250
2. \*Python module: <module_name>.<class_name>
@@ -628,11 +661,11 @@ Follow these guides to understand how to start to contribute to them while devel
628661

629662
## How to test the application
630663

631-
IntelOwl makes use of the django testing framework and the `unittest` library for unit testing of the API endpoints and End-to-End testing of the analyzers and connectors.
664+
IntelOwl makes use of the django testing framework and the `unittest` library for unit testing of the API endpoints and End-to-End testing of the analyzers and connectors.
632665

633666
### Configuration
634667

635-
- In the encrypted folder `tests/test_files.zip` (password: "intelowl") there are some files that you can use for testing purposes.
668+
- In the encrypted folder `tests/test_files.zip` (password: "intelowl") there are some files that you can use for testing purposes. The async_tests/ dir has mainly transactional test cases as they are run separately from other unit tests.
636669

637670
- With the following environment variables you can customize your tests:
638671

@@ -678,6 +711,17 @@ To test a plugin in real environment, i.e. without mocked data, we suggest that
678711
Meaning that you have your plugin configured, you have selected a correct observable/file to analyze,
679712
and the final report shown in the GUI of IntelOwl is exactly what you wanted.
680713

714+
##### Running Tests for a Specific Analyzer
715+
716+
To test a particular analyzer, locate its corresponding unittest file inside: tests/api_app/analyzers_manager/unit_tests/[observable_analyzers / file_analyzers]
717+
718+
719+
Once you’ve identified the test file, you can run it individually with:
720+
721+
```bash
722+
docker exec -ti intelowl_uwsgi python manage.py test tests.api_app.analyzers_manager.unit_tests.observable_analyzers.<test_file>
723+
```
724+
681725
##### Run tests available in a particular file
682726

683727
Examples:

0 commit comments

Comments
 (0)