forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Closed
Labels
area-testingdebtCovers everything internal: CI, testing, refactoring of the codebase, etc.Covers everything internal: CI, testing, refactoring of the codebase, etc.
Description
Overview
This document outlines the best practices, structure, and reasoning for writing unit tests for the populateTestTree function. As part of this issue, tests should be made for the populateTestTree function.
Key Takeaways from Existing Tests
- Use of sinon for stubbing/mocking: Enables precise control and verification of function calls and arguments.
- Use of typemoq for VS Code API mocks: Facilitates realistic and flexible mocking of VS Code interfaces.
- Use of assert for validation: Ensures clear, reliable assertions.
- Clear test structure: Tests are grouped by scenario and use descriptive names.
- Setup/teardown: Each test starts with a clean state and restores mocks after execution.
Best Practices for Testing populateTestTree
1. Test Suite Structure
- Use Mocha's
describe/it(orsuite/test) structure for clarity. - Group tests by scenario: root creation, recursive population, mapping, cancellation, edge cases.
2. Mocking and Stubbing
- Use
sinonfor stubbing/mocking methods and tracking calls (e.g.,TestController.createTestItem,TestItem.children.add). - Use
typemoqfor more complex VS Code API mocks if needed. - Use
assertfor value and call validation.
3. Test Format
- Setup: Create fresh mocks for
TestController,TestItem, andresultResolverfor each test. - Teardown: Restore sinon after each test.
- Use clear, descriptive test names.
4. Test Scenarios to Cover
- Root Creation: If
testRootis undefined, a new rootTestItemshould be created and added to theTestController. - Recursive Tree Population: All children in
testTreeDataare processed recursively, building the correct hierarchy ofTestItems. - Test Item Creation: For leaf nodes, a
TestItemis created with the correct id, name, path, tags, and range (including correct handling oflineno = 0and nonzero). TheTestItemis added as a child to its parent. - Node Creation: For non-leaf nodes, a
TestItemis created if it does not already exist, with correct properties, and added as a child. - Mapping Updates:
resultResolver.runIdToTestItem,runIdToVSid, andvsIdToRunIdare updated correctly for each test item. - Cancellation: If the cancellation token is triggered, the function should not process further children.
- Tagging: All created
TestItemsand nodes have the correct tags (RunTestTag,DebugTestTag). - Edge Cases: Handles empty children arrays, duplicate nodes or test items, and missing properties gracefully.
5. Packages to Use
mocha(test runner)sinon(stubbing/mocking)typemoq(VS Code API mocks)assert(assertions)
6. Example Test Skeleton
describe('populateTestTree', () => {
let testController: sinon.SinonStubbedInstance<TestController>;
let resultResolver: ITestResultResolver;
let cancellationToken: CancellationToken;
beforeEach(() => {
// Create stubs/mocks for TestController, TestItem, resultResolver, etc.
});
afterEach(() => {
sinon.restore();
});
it('should create a root node if testRoot is undefined', () => {
// Arrange: setup testTreeData, stubs for createTestItem, etc.
// Act: call populateTestTree
// Assert: root node created, added to controller, correct properties
});
it('should recursively add children as TestItems', () => {
// Arrange: nested testTreeData
// Act: call populateTestTree
// Assert: all children added, correct hierarchy
});
// ...more tests for tags, range, mappings, cancellation, edge cases
});7. General Rules
- Keep each test focused on one behavior.
- Use stubs to verify method calls and arguments.
- Validate both structure (tree shape) and side effects (mappings, tags).
- Use
beforeEach/afterEachfor clean setup/teardown.
By following these guidelines, you will ensure that tests for populateTestTree are robust, maintainable, and provide high confidence in the correctness of the function.
Copilot
Metadata
Metadata
Assignees
Labels
area-testingdebtCovers everything internal: CI, testing, refactoring of the codebase, etc.Covers everything internal: CI, testing, refactoring of the codebase, etc.