This document provides an overview of the unit-test suite added in the recent merge, explaining its structure, purpose, and how to extend it.
- Validate parser behavior (PDF & Office):
- Ensure corrupt inputs raise
ParserErrorwith proper logging. - Confirm valid inputs produce the expected output schema and types.
- Ensure corrupt inputs raise
- Catch regressions in future changes to parsing, enrichment, scoring, or reporting logic.
- Maintain a stable API contract: Parsers must keep returning the same field names and data types.
- Enforce code quality via automated linting (Ruff) and type-checking (Mypy).
- Achieve ≥ 80 % code coverage, so any drop below that threshold fails the CI build.
tests/
├── conftest.py # shared fixtures and autouse stubs
└── unit/
├── test_pdf_parser_error.py # corrupt PDF → ParserError + ERROR log
├── test_pdf_parser_ok.py # valid PDF → correct fields & types
├── test_doc_parser.py # Office parser happy & failure paths
├── test_analyze_dispatch.py # dispatch logic smoke tests
├── test_heuristics.py # scoring logic scenarios
├── test_url_reputation.py # VirusTotal helper, no-key cases
├── test_abuseipdb_check.py # AbuseIPDB helper, no-key & empty input
└── test_report_generator.py # Markdown & JSON report outputs
sample_pdf: Path toexamples/test.pdf(benign PDF sample).sample_docm: Path toexamples/macro_test.docm(Office file with macros).stub_requests(autouse): Monkey-patchesrequests.getandrequests.postto return dummy JSON, preventing real HTTP calls during unit tests.
-
test_pdf_parser_error.py
Creates a 1-byte “garbage.pdf” → expectsParserErrorand an ERROR log record. -
test_pdf_parser_ok.py
Usessample_pdf→ asserts presence of keys (type,urls,ips,embedded_files,js_count) and correct types.
test_doc_parser.py- Happy path:
parse_office(sample_docm)→macroflag true, URL/IP lists. - Failure path: Corrupt DOCM → raises
ParserError.
- Happy path:
test_analyze_dispatch.py
Ensuresanalyze()chooses PDF or Office parser based on file extension.
-
test_heuristics.py
Verifies scoring output for various combinations of IOCs (macro, embedded, JS). -
test_url_reputation.py
Tests the internal_vt_url_idfunction and early exit whenVT_API_KEYis unset. -
test_abuseipdb_check.py
Tests early-exit whenABUSEIPDB_API_KEYis unset or input list is empty.
test_report_generator.py
Generates both Markdown and JSON reports to a temporary directory and asserts key content.
# Install dev dependencies
pip install -r requirements-dev.txt
# Run all tests with coverage
pytestThe pytest.ini file automatically applies --cov=ioc_inspector_core --cov-fail-under=80.
🚀 Extending the Suite
-
Add new parser tests: Follow the test_* naming convention and use sample_pdf/sample_docm fixtures.
-
Mock external services: Use the stub_requests fixture or create similar stubs for new clients.
-
Increase coverage: Write tests for any untested functions in ioc_inspector_core/ (e.g., deeper abuseIPDB branches).
-
Integration tests: Create a new folder tests/integration/ for end-to-end scenarios using real API keys.