Test Backfill
Generate a backfill of unit and API tests for existing code
Overview
This guide explains how to use Tusk to generate unit and API tests for existing code through the backfill process. Tusk generates backfills at the PR/MR level.
There are two types of backfill testing supported by Tusk:
- Unit Test Backfill: For utility functions, classes, and other standard code components
- API Test Backfill: For functions that make API calls
Symbol Limit: Tusk currently supports backfilling tests for a maximum of 10 symbols per PR/MR.
Unit Test Backfill
The unit test backfill is used for standard code components such as utility functions, classes, and helper methods.
How to Request a Regular Function Backfill
Add comment in code
In your IDE, add a comment inside each function/method you want to test. The comment can be single-line or multi-line.
Make sure the comment is inside the function definition and includes the keyword UseTusk
.
Example:
Create a PR/MR
Raise a PR/MR that modifies the target functions/classes you want to test.
Wait for test generation
Tusk will detect the modified symbols and auto-generate appropriate unit tests.
API Test Backfill
API test backfill is used when you need to generate tests for API endpoints. This requires more specific configuration to ensure proper test generation.
Prerequisites
- You must have a testing environment set up for API tests before using API backfill
- If you don’t have an API testing environment configured, please contact us for assistance
How to Request an API Test Backfill
Add special comment
In your IDE, add a special comment inside the function that calls the API (can be single-line or multi-line).
Make sure the comment is inside the function definition and includes the keyword UseTusk
.
Example:
Create a PR/MR
Create a PR/MR with the special comment that modifies the target API(s).
Wait for test generation
Tusk will detect the modified symbols and auto-generate appropriate API tests.
Information in Comments
- The comment MUST contain
UseTusk
to trigger the test generation - Source File (Recommended for API testing)
- Indicate where the API implementation is located
- Example:
source file: src/api/userService.js
- This helps Tusk trace to the API code and traverse the codebase for better test generation
- Test File (Recommended for API testing)
- Specify where the generated test should be saved
- Example:
test file: tests/api/userService.test.js
- Additional Guidance (Optional)
- You can include free-text guidance to help Tusk generate better tests. For example:
- Specify edge cases to test
- Provide context on the best way to write tests
- You can reference additional context files to help guide test generation
- Example:
check src/hooks/__tests__/useAuth.test.js for inspiration on how to mock authentication
- Example:
- For more comprehensive customization options, refer to our Customization documentation where you can find information on providing global context for how to best write tests for your codebase.
- You can include free-text guidance to help Tusk generate better tests. For example:
Note on Comments and File Paths
- Tusk uses intelligent processing to extract information from your comments, so the exact format isn’t rigid
- You can use either single-line or multi-line comment styles
- If file paths are not provided, Tusk will attempt to determine the best locations, but we strongly recommend explicitly specifying paths to ensure tests are generated in the correct location
- Important: All file paths (source, test, and context files) must be specified from the project root directory
- Important: Ensure the comment is inside of the symbol definition and includes the keyword
UseTusk
-
Correct placement (inside the function)
-
Incorrect placement (outside the function)
-
Example Workflow
- Identify code that needs tests
- Add the appropriate
UseTusk
comment inside the target functions - Submit your PR
- Review the generated tests when Tusk completes the backfill
- Incorporate the tests Tusk generates with a click of a button
Best Practices
- Be specific in your comments about what aspects of the code need testing
- For API backfill, specify source and test file paths
- Keep the number of symbols for backfill under the 10 symbol limit per PR
Was this page helpful?