The test suite for ThreatDetect lives in theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/jazbengu/ThreatDetect/llms.txt
Use this file to discover all available pages before exploring further.
unit testing/ directory and covers every major stage of the pipeline: feature preparation, model inference, SHAP explainability, confidence scoring, and input validation. All tests are written for pytest and rely on lightweight in-memory fixtures so you do not need a real trained model to run them.
Install test dependencies
The core app dependencies are listed inrequirements.txt. To run the tests you also need pytest and pytest-mock:
Run the tests
Run all tests from the repository root by pointing pytest at theunit testing/ directory:
Test structure
| File | What it covers |
|---|---|
test_prep_features.py | prepare_features() — normal case, unseen categories, inf ratio handling |
test_integrate.py | End-to-end prediction path with a mocked load_model |
test_explainability.py | results_explainability() output shape and SHAP extraction |
confidence_test.py | Model package construction and confidence scoring |
testing_validation.py | validate_input_columns() — success and missing-column error |
conftest.py | Shared fixtures used across all test files |
Shared fixtures
conftest.py provides two fixtures that most tests depend on.
sample_raw_data returns a minimal two-row DataFrame with all required input columns:
mock_model_package builds a fully functional but lightweight model package in memory. It fits a LabelEncoder for the categorical campus column, a StandardScaler for numerical columns, an IsolationForest, and a two-estimator XGBClassifier on random data — enough for the real pipeline code to run without loading a saved model file. It also attaches a shap.TreeExplainer and sets best_threshold to 0.5.
Feature preparation tests
These three tests intest_prep_features.py exercise prepare_features() against the shared fixtures.
Normal case — verifies output shapes and that engineered ratio columns are added:
ValueError rather than silently producing a wrong encoding:
prepare_features replaces any resulting inf values so the feature matrix stays finite:
End-to-end prediction test
test_integrate.py mocks load_model so the test never touches the filesystem, then runs the full inference pipeline and checks that predictions are valid binary values:
pytest-mock must be installed for the mocker fixture used in test_end_to_end_prediction. Install it with pip install pytest-mock.