
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.Example:
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).Example:
Information in Comments
- The comment MUST contain
UseTuskfor unit test generation andUseTusk APIfor API 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
UseTuskorUseTusk API-
Correct placement (inside the function)
-
Incorrect placement (outside the function)
-
Correct placement (inside the function)
Example Workflow
- Identify code that needs tests
- Add the appropriate
UseTuskorUseTusk APIcomment inside the target functions - Submit your PR
- Review the generated tests when Tusk completes the backfill
- Commit the tests Tusk generates with a click of a button (the
UseTuskcomments will be automatically removed when you commit)
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