conftest.py
Overview
The conftest.py file is a configuration module used by the pytest testing framework to provide reusable fixtures for test cases. In this context, it supplies fixtures that automate the setup and teardown of dataset resources for tests that require interacting with datasets through an HTTP API. These fixtures create datasets before tests run and ensure their cleanup after tests complete, thereby maintaining test isolation and preventing resource leaks.
Detailed Explanation
Imports
pytest: The core testing framework used for defining fixtures.batch_create_datasets,delete_datasetsfromcommon: Utility functions used to create and delete datasets via an authenticated HTTP API.
Fixtures
Fixtures in this file provide pre-test setup and post-test cleanup for datasets in two different scopes.
1. add_datasets
Type:
pytest.fixtureScope:
"class"(runs once per test class)Parameters:
HttpApiAuth: A fixture or parameter representing an authenticated HTTP API client/session.request: A built-in pytest fixture providing information about the requesting test context.
Returns: A collection (likely list) of 5 dataset objects created by
batch_create_datasets.Functionality:
Before tests in a class run, it creates 5 datasets via the API.
Registers a finalizer function
cleanupwhich deletes all datasets after all tests in the class have finished.
Usage Example:
def test_dataset_processing(add_datasets):
# add_datasets provides 5 datasets created before the test runs
assert len(add_datasets) == 5
# perform tests on these datasets
2. add_datasets_func
Type:
pytest.fixtureScope:
"function"(runs once per test function)Parameters:
HttpApiAuth: Same as above.request: Same as above.
Returns: A collection (likely list) of 3 dataset objects created by
batch_create_datasets.Functionality:
Before each test function runs, it creates 3 datasets.
Registers a finalizer
cleanupto delete all datasets after the test function completes.
Usage Example:
def test_single_dataset_operation(add_datasets_func):
# add_datasets_func provides 3 datasets created before each test function
assert len(add_datasets_func) == 3
# test code utilizing these datasets
Important Implementation Details
Test Isolation:
Both fixtures ensure datasets are cleaned up after tests to prevent interference between tests and avoid polluting the backend service with test data.Use of
request.addfinalizer:
This mechanism ensures that the cleanup runs regardless of the test outcome (pass or fail).Reusability:
By abstracting dataset creation and deletion into fixtures, test code becomes cleaner and more maintainable.Parameter
{"ids": None}indelete_datasets:
Likely signals deletion of all datasets accessible by the authenticated API client, ensuring a complete cleanup.
Interaction with Other System Parts
HttpApiAuthFixture:
This file assumes the existence of anHttpApiAuthfixture or parameter that provides an authenticated HTTP API client/session. This client is used to make requests for dataset creation and deletion.commonModule:
Thebatch_create_datasetsanddelete_datasetsfunctions are imported from thecommonmodule. They encapsulate the logic for interacting with the backend API to manage datasets.Test Modules:
Test files use these fixtures to prepare their test environment. For example, test classes that require multiple datasets can depend onadd_datasets, while individual test functions requiring fewer datasets can depend onadd_datasets_func.
Mermaid Diagram
flowchart TD
A[conftest.py] --> B[add_datasets (class scope)]
A --> C[add_datasets_func (function scope)]
B --> D[Calls batch_create_datasets(HttpApiAuth, 5)]
B --> E[Registers cleanup: delete_datasets(HttpApiAuth, {"ids": None})]
C --> F[Calls batch_create_datasets(HttpApiAuth, 3)]
C --> G[Registers cleanup: delete_datasets(HttpApiAuth, {"ids": None})]
subgraph External Dependencies
H[HttpApiAuth fixture]
I[common.batch_create_datasets]
J[common.delete_datasets]
end
D --> I
E --> J
F --> I
G --> J
B --> H
C --> H
Summary
conftest.py provides two pytest fixtures for managing dataset lifecycle in tests:
add_datasets: Creates 5 datasets for an entire test class and deletes them afterward.add_datasets_func: Creates 3 datasets for each test function and deletes them afterward.
These fixtures leverage centralized dataset creation and deletion utilities for consistent, isolated testing of features relying on datasets managed via an HTTP API.
This setup promotes clean, maintainable test code and reliable test execution within the InfiniFlow testing framework.