This directory contains comprehensive test cases for all modules in the project.
tests/
├── __init__.py # Test package initialization
├── conftest.py # Shared pytest fixtures
├── test_load_ontology.py # Tests for load_ontology.py
├── test_add_data.py # Tests for add_data.py
├── test_reasoning_demo.py # Tests for reasoning_demo.py
├── test_api.py # Tests for Flask API endpoints
└── README.md # This file
pip install -r requirements.txtThis will install:
pytest- Test frameworkpytest-cov- Coverage reportingpytest-mock- Mocking utilities
pytestpytest tests/test_load_ontology.py
pytest tests/test_api.pypytest --cov=. --cov-report=htmlThis generates an HTML coverage report in htmlcov/index.html.
pytest -vpytest tests/test_api.py::TestAPIRestaurantsEndpoint
pytest tests/test_api.py::TestAPIRestaurantsEndpoint::test_get_restaurants_success# Run only unit tests
pytest -m unit
# Run only integration tests
pytest -m integration
# Run ontology-related tests
pytest -m ontology- Test individual functions in isolation
- Use mocks for external dependencies
- Fast execution
- Test complete workflows
- Use real ontology files
- May require
add_data.pyto be run first
- Test Flask endpoints
- Use Flask test client
- Mock ontology data for isolated testing
Shared fixtures are defined in conftest.py:
base_ontology_path- Path to base ontology fileupdated_ontology_path- Path to updated ontology fileloaded_base_ontology- Loaded base ontology instanceloaded_updated_ontology- Loaded updated ontology instancetest_ontology- Temporary in-memory ontology for isolated testssample_restaurant_data- Sample restaurant datasample_dish_data- Sample dish data
- Base Ontology File:
restaurant-ontology.owlmust exist in project root - Updated Ontology File: For some integration tests, run
python add_data.pyfirst to createrestaurant-ontology-updated.owl - Python Environment: Python 3.8+ with all dependencies installed
The test suite covers:
✅ load_ontology.py
- Ontology file loading
- Class enumeration
- Individual inspection
- Property access
- Error handling
✅ add_data.py
- Creating restaurant individuals
- Creating dish individuals
- Creating chef individuals
- Setting properties
- Creating relationships
- Saving ontology to file
✅ reasoning_demo.py
- Running OWL reasoner
- Automatic classification
- VegetarianDish classification
- Querying inferred knowledge
✅ api.py
- All 5 REST endpoints
- Request/response formats
- Error handling (404, 500)
- Query parameter filtering
- CORS configuration
- Some integration tests require the ontology files to exist
- API tests use mocking - may not catch all real-world issues
- Reasoner tests depend on Owlready2's internal implementation
To run tests in CI/CD:
# Install dependencies
pip install -r requirements.txt
# Run tests with coverage
pytest --cov=. --cov-report=xml
# Or with HTML report
pytest --cov=. --cov-report=htmlSolution: Make sure restaurant-ontology.owl exists in the project root. Run python add_data.py to create the updated ontology file.
Solution: Make sure all dependencies are installed: pip install -r requirements.txt
Solution: Verify the ontology files are valid OWL files. Try loading them manually with Owlready2.
Solution: The API tests use mocking. Make sure pytest-mock is installed and fixtures are set up correctly.
When adding new tests:
- Follow existing naming conventions (
test_*.py,test_*functions) - Use appropriate fixtures from
conftest.py - Add docstrings to test functions
- Use descriptive test names
- Add appropriate markers (
@pytest.mark.unit, etc.) - Update this README if adding new test categories