A smart test filtering tool that defines component usage relationships and runs tests based on those relationships, with specific support for MVC architecture patterns.
The test-filter tool is designed to optimize CI/CD pipelines by intelligently determining which tests need to be executed based on the files that have changed in a pull request. Instead of running all tests for every change, it analyzes the relationships between different components of your application and only runs the tests that are potentially affected by the changes.
The tool operates by:
- Component Type Definition: Application files are categorized into component types (e.g.,
model_type
,view_type
,controller_type
) based on their file paths - Dependency Mapping: Component types can declare dependencies on other component types using the
uses
field - Change Analysis: When files are modified, the tool determines which component types are affected
- Test Selection: Using a dependency graph, it identifies which test component types need to be executed
- Test Execution: Only the necessary tests are run, significantly reducing CI execution time
The tool is configured using a YAML file (typically .circleci/test-filter.yml
) with the following structure:
Define how your application files are categorized:
Define how your test files are categorized:
test_path_pattern: spec/**/*_spec.rb
base_branch: main
ignores:
- README.md
app_component_types:
- name: model_type
paths:
- app/models/
- app/forms/
- name: view_type
paths:
- app/views/
- app/assets/
- app/helpers/
- name: controller_type
paths:
- app/controllers/
- name: job_type
paths:
- app/jobs/
- lib/tasks/
test_component_types:
- name: test_model_type
paths:
- spec/models/
- spec/forms/
- name: test_view_type
paths:
- spec/helpers/
- name: test_controller_type
paths:
- spec/controllers/
- spec/requests/
- name: test_job_type
paths:
- spec/jobs/
- spec/tasks/
You can use regular expressions for more complex matching:
test_component_types:
- name: test_model_type
paths:
- spec/models/
- format: regexp
value: spec/gateways/.*?course_.*+_spec.rb
# Run the test filter with a configuration file
# ./bin/test_filter_run.sh [config-yml-path] [test-command]
./bin/test_filter_run.sh .circleci/test-filter.yml circleci tests run ...
The tool is designed to integrate seamlessly with CircleCI workflows:
# In your .circleci/config.yml
- run:
name: Run filtered tests
command: |
.circleci/test-filter/bin/test_filter_run.sh \
.circleci/test-filter.yml \
circleci tests run
- Orb Integration: CircleCI Orb packaging for easier reuse across projects
- GitHub Actions Support: Native GitHub Actions integration and marketplace publication
The tool is currently embedded directly in the repository and must be copied manually to other projects.