Testing Parsers
For each rule you add to your grammar, you should create tests that describe how syntax trees should look when parsing that rule. These tests serve as documentation and verify that your parser works correctly.Test Format
Tests are written in specially-formatted text files in thetest/corpus/ directory.
Basic Structure
Create test files liketest/corpus/statements.txt:
test/corpus/statements.txt
1
Test name
Written between two lines of
= (equal signs)2
Input source code
The code to parse
3
Separator
A line of three or more
- (dashes)4
Expected output
The expected syntax tree as an S-expression
The S-expression only shows named nodes. Anonymous nodes (strings and regexes from the grammar) are not shown.
With Field Names
Include field names for better documentation:Running Tests
Run All Tests
test/corpus/ folder.
Run Specific Tests
Use the-i flag to filter by name:
Update Tests
After modifying the grammar, update all expected outputs:Debug Tests
Show detailed parsing information:Test Attributes
Annotate tests with attributes below the test name::skip
Temporarily disable a test:
:error
Assert that parsing produces an error:
With
:error, you can omit the expected output section. The test passes if the parse tree contains an ERROR node.:fail-fast
Stop testing if this test fails:
:platform(PLATFORM)
Run only on specific platforms:
:language(LANG)
Use a different parser from a multi-parser repository:
The default parser is the first entry in the
grammars field in tree-sitter.json.:cst
Show the complete concrete syntax tree:
Custom Separators
If your language uses=== or ---, add a suffix:
||| can be any identical string on all separator lines.
Organizing Tests
By Feature
Organize test files by language feature:Comprehensive Coverage
For afor loop, test:
Example Test File
Here’s a complete example:test/corpus/expressions.txt
Test-Driven Development
1
Write the test first
Before implementing a grammar rule, write a test showing the expected behavior.
2
Run the test
The test will fail because the grammar doesn’t support the feature yet.
3
Implement the rule
Add or modify grammar rules to make the test pass.
4
Generate and test
Run
tree-sitter generate && tree-sitter test to verify.5
Refine
Adjust the grammar or test as needed until it works correctly.
Automatic Compilation
The first time you runtree-sitter test after generating your parser, it takes extra time to compile your C code into a dynamically-loadable library.
Tree-sitter automatically recompiles when:
- You run
tree-sitter generate - You modify
src/scanner.c(external scanner)
Compiled parsers are cached, so subsequent test runs are much faster.
Best Practices
1. Test Early and Often
2. Test Edge Cases
Include tests for:- Empty structures
- Nested constructs
- Ambiguous syntax
- Error cases
- Platform-specific behavior
3. Use Descriptive Names
4. Group Related Tests
Keep similar tests in the same file:5. Document Expected Behavior
Use tests to document how your parser handles specific cases:Debugging Failed Tests
View Parse Tree
When a test fails, examine the actual parse tree:Compare Output
The test output shows:Use Debug Mode
- Lexing output
- Parse states
- Conflict resolution
- Error recovery
Next Steps
Now that you know how to test your parser:- Learn about Publishing your parser
- Explore existing parsers for test examples
- Read about CI/CD workflows for automated testing