birchrest.unittest package
Submodules
birchrest.unittest.test_adapter module
- class birchrest.unittest.test_adapter.TestAdapter(app: BirchRest)[source]
Bases:
objectA test wrapper for simulating HTTP requests in a BirchRest application.
The TestAdapter class provides a convenient way to simulate HTTP requests such as GET, POST, PUT, PATCH, DELETE, HEAD, and OPTIONS during testing. It interacts directly with the BirchRest application instance and can be used to test routes and endpoints without requiring a live server.
This class generates Request objects with the appropriate HTTP method, headers, and body, and passes them to the application for processing. The responses returned by the application can then be validated in tests.
- Attributes:
app (BirchRest): The instance of the BirchRest application being tested.
- Methods:
get(path, headers, body): Simulates a GET request to the application. post(path, headers, body): Simulates a POST request to the application. put(path, headers, body): Simulates a PUT request to the application. patch(path, headers, body): Simulates a PATCH request to the application. delete(path, headers, body): Simulates a DELETE request to the application. head(path, headers): Simulates a HEAD request to the application. options(path, headers): Simulates an OPTIONS request to the application. _generate_request(method, path, headers, body): Helper method to create a Request object.
- async delete(path: str, headers: Dict[str, str] = {}, body: Any | None = None) Response[source]
Simulate a DELETE request.
- async get(path: str, headers: Dict[str, str] = {}, body: Any | None = None) Response[source]
Simulate a GET request.
- async options(path: str, headers: Dict[str, str] = {}) Response[source]
Simulate an OPTIONS request.
- async patch(path: str, headers: Dict[str, str] = {}, body: Any | None = None) Response[source]
Simulate a PATCH request.
Module contents
This module provides the TestAdapter, a wrapper around the BirchRest app to simplify unit testing.
TestAdapter: Facilitates testing by providing an easy way to simulate requests and inspect responses.
Exported components: - TestAdapter
- class birchrest.unittest.BirchRestTestCase(methodName='runTest')[source]
Bases:
IsolatedAsyncioTestCaseA custom TestCase class that adds helper methods for asserting BirchRest HTTP responses.
This class extends unittest.IsolatedAsyncioTestCase and provides additional assertion methods to validate HTTP status codes, headers, and response bodies in a simplified manner.
- assertBodyContains(response: Response, expected_key: str) None[source]
Assert that the response body contains a certain property
- assertHasHeader(response: Response, expected_key: str) None[source]
Assert that the response has a specific header.
- assertHeader(response: Response, header_name: str, expected_value: str) None[source]
Assert that a specific header in the response matches the expected value.
- Parameters:
response – The Response object to check
header_name – The name of the header to validate
expected_value – The expected value of the header
- assertNotOk(response: Response) None[source]
Assert that the response status code is not in the range of 2xx (indicating failure).
- assertOk(response: Response) None[source]
Assert that the response status code is in the range of 2xx (successful responses).
- assertRedirect(response: Response, expected_url: str) None[source]
Assert that the response status is a redirect (3xx) and the Location header matches the expected URL.
- class birchrest.unittest.TestAdapter(app: BirchRest)[source]
Bases:
objectA test wrapper for simulating HTTP requests in a BirchRest application.
The TestAdapter class provides a convenient way to simulate HTTP requests such as GET, POST, PUT, PATCH, DELETE, HEAD, and OPTIONS during testing. It interacts directly with the BirchRest application instance and can be used to test routes and endpoints without requiring a live server.
This class generates Request objects with the appropriate HTTP method, headers, and body, and passes them to the application for processing. The responses returned by the application can then be validated in tests.
- Attributes:
app (BirchRest): The instance of the BirchRest application being tested.
- Methods:
get(path, headers, body): Simulates a GET request to the application. post(path, headers, body): Simulates a POST request to the application. put(path, headers, body): Simulates a PUT request to the application. patch(path, headers, body): Simulates a PATCH request to the application. delete(path, headers, body): Simulates a DELETE request to the application. head(path, headers): Simulates a HEAD request to the application. options(path, headers): Simulates an OPTIONS request to the application. _generate_request(method, path, headers, body): Helper method to create a Request object.
- async delete(path: str, headers: Dict[str, str] = {}, body: Any | None = None) Response[source]
Simulate a DELETE request.
- async get(path: str, headers: Dict[str, str] = {}, body: Any | None = None) Response[source]
Simulate a GET request.
- async options(path: str, headers: Dict[str, str] = {}) Response[source]
Simulate an OPTIONS request.
- async patch(path: str, headers: Dict[str, str] = {}, body: Any | None = None) Response[source]
Simulate a PATCH request.