docs/versioned_docs/version-1.8.0/Contributing/contributing-component-tests.mdx
This guide outlines how to structure and implement tests for application components to ensure consistency and adequate coverage.
The test file should follow the same directory structure as the component being tested, but should be placed in the corresponding unit tests folder.
For example, if the file path for the component is src/lfx/src/lfx/components/data/, then the test file should be located at src/backend/tests/unit/components/data.
The test file name should use snake case and follow the pattern test_<file_name>.py.
For example, if the file to be tested is FileComponent.py, then the test file should be named test_file_component.py.
Test<ClassName>.
For example, if the component being tested is FileComponent, then the test class should be named TestFileComponent.To standardize component tests, base test classes have been created and should be imported and inherited by all component test classes. These base classes are located in the file src/backend/tests/unit/base.py.
To import the base test classes:
from tests.base import ComponentTestBaseWithClient
from tests.base import ComponentTestBaseWithoutClient
These base classes enforce mandatory methods that the component test classes must implement. The base classes ensure that components built in previous versions continues to work in the current version. By inheriting from one of these base classes, the developer must define the following methods decorated with @pytest.fixture:
component_class: Returns the class of the component to be tested. For example:
@pytest.fixture
def component_class(self):
return FileComponent
default_kwargs: Returns a dictionary with the default arguments required to instantiate the component. For example:
@pytest.fixture
def default_kwargs(self):
return {"file_path": "/tmp/test.txt", "_session_id": "123"}
file_names_mapping: Returns a list of dictionaries representing the relationship between version, module, and file_name that the tested component has had over time. This can be left empty if it is an unreleased component. For example:
@pytest.fixture
def file_names_mapping(self):
return [
{"version": "1.0.15", "module": "data", "file_name": "File"},
{"version": "1.0.16", "module": "data", "file_name": "File"},
{"version": "1.0.17", "module": "data", "file_name": "File"},
{"version": "1.0.18", "module": "data", "file_name": "File"},
{"version": "1.0.19", "module": "data", "file_name": "File"},
]
Once the basic structure of the test file is defined, implement test methods for the component's functionalities. The following guidelines must be followed:
test_<case_name>.Arrange: Prepare the data.
It is recommended, but not mandatory, that you use the fixtures defined in the basic structure.
def test_post_code_processing(self, component_class, default_kwargs):
component = component_class(**default_kwargs)
Act: Execute the component.
Call the .to_frontend_node() method of the component prepared during the Arrange step.
def test_post_code_processing(self, component_class, default_kwargs):
component = component_class(**default_kwargs)
frontend_node = component.to_frontend_node()
Assert: Verify the result.
After executing the .to_frontend_node() method, the resulting data is available for verification in the dictionary frontend_node["data"]["node"]. Assertions should be clear and cover the expected outcomes.
def test_file_component_processing(self, component_class, default_kwargs):
component = component_class(**default_kwargs)
frontend_node = component.to_frontend_node()
node_data = frontend_node["data"]["node"]
assert node_data["template"]["path"]["file_path"] == "/tmp/test.txt"
assert "path" in node_data["template"]
assert node_data["display_name"] == "File"