Tests#
BaseApiTestCases provides a reusable foundation for API integration tests.
It calls reset_routers() in setUp() before every test method to prevent
router state from leaking between test cases.
Usage example:
# -*- coding: utf-8 -*-
from fastapi import APIRouter
from core_apis.api.routers import add_router
from core_apis.tests import BaseApiTestCases
router = APIRouter()
add_router(router)
@router.get("/items")
def list_items():
return []
class ItemsTestCases(BaseApiTestCases):
def test_list_items(self):
response = self.client.get("/api/items")
self.assertEqual(response.status_code, 200)
Custom CORS or application configuration can be applied by overriding
setUpClass and calling init_client:
@classmethod
def setUpClass(cls):
cls.init_client(with_cors=False)
Base test classes for API testing.
This module provides base test case classes that can be inherited by other test modules to facilitate testing of FastAPI applications. It includes utilities for setting up test clients and managing test application instances.
- class core_apis.tests.base.BaseApiTestCases(methodName='runTest')[source]#
Bases:
TestCaseBase class for tests related to the API.
This class provides a reusable foundation for API testing by automatically setting up a FastAPI test application and test client. Tests that inherit from this class will have access to a configured TestClient instance.
- classmethod setUpClass() None[source]#
Set up the test class before any test methods run.
This method is called once per test class and initializes the test client and application instance.
- classmethod init_client(with_cors: bool = True)[source]#
Initialize the test client and FastAPI application.
Creates a new FastAPI application instance configured for testing and wraps it with a TestClient for making HTTP requests.
- Parameters:
with_cors – Whether to add CORS middleware to the test application. Defaults to True.
- _classSetupFailed = False#
- _class_cleanups = []#
CLI runner for API server operations.
This module provides Click-based command-line interface commands for running and managing the API server. It allows running the server with either a default or custom FastAPI application instance.