pip install -r requirements.txtpytestpytest --cov=. --cov-report=html --cov-report=term-missingpytest tests/test_health.py
pytest tests/test_chat.py
pytest tests/test_posts.py
pytest tests/test_agent.py
pytest tests/test_utils.py
pytest tests/test_integration.pypytest -m unit # Run only unit tests
pytest -m integration # Run only integration tests
pytest -m "not slow" # Skip slow testspytest -v # Verbose test names
pytest -vv # Extra verbose with full diff
pytest -s # Show print statementstest_health.py- Health endpoint teststest_chat.py- Chat endpoint teststest_posts.py- Posts CRUD teststest_agent.py- Agent creation teststest_utils.py- Utility function tests
test_integration.py- Cross-component flow tests
The test suite covers:
- ✅ All API endpoints (/health, /chat, /chat/history, /agent/create, /posts)
- ✅ Request validation
- ✅ Response structure validation
- ✅ Error handling
- ✅ CORS headers
- ✅ Utility functions
- ✅ Cross-component workflows
Tests use unittest.mock to mock:
- Azure AI Projects client
- Cosmos DB client
- Environment variables
- Agent availability
This allows tests to run without actual Azure resources.
Add to your CI/CD pipeline:
- name: Install dependencies
run: pip install -r requirements.txt
- name: Run tests
run: pytest --cov=. --cov-report=xml
- name: Upload coverage
uses: codecov/codecov-action@v3If you see import errors, make sure you're running pytest from the /src/api directory:
cd src/api
pytestMake sure all dependencies are installed:
pip install -r requirements.txtTests mock environment variables, so you don't need a .env file for testing.
- Create a new file
test_<feature>.pyin thetests/directory - Import pytest and necessary mocks
- Create a test class with descriptive name
- Write test methods starting with
test_ - Use assertions to validate behavior
Example:
import pytest
from unittest.mock import patch, MagicMock
class TestNewFeature:
def test_feature_works(self):
result = my_function()
assert result == expected_value