forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
sinon
for stubbing/mocking methods and tracking calls (e.g.,TestController.createTestItem
,TestItem.children.add
). - Use
typemoq
for more complex VS Code API mocks if needed. - Use
assert
for value and call validation.
3. Test Format
- Setup: Create fresh mocks for
TestController
,TestItem
, andresultResolver
for each test. - Teardown: Restore sinon after each test.
- Use clear, descriptive test names.
4. Test Scenarios to Cover
- Root Creation: If
testRoot
is undefined, a new rootTestItem
should be created and added to theTestController
. - Recursive Tree Population: All children in
testTreeData
are processed recursively, building the correct hierarchy ofTestItems
. - Test Item Creation: For leaf nodes, a
TestItem
is created with the correct id, name, path, tags, and range (including correct handling oflineno = 0
and nonzero). TheTestItem
is added as a child to its parent. - Node Creation: For non-leaf nodes, a
TestItem
is created if it does not already exist, with correct properties, and added as a child. - Mapping Updates:
resultResolver.runIdToTestItem
,runIdToVSid
, andvsIdToRunId
are updated correctly for each test item. - Cancellation: If the cancellation token is triggered, the function should not process further children.
- Tagging: All created
TestItems
and 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
/afterEach
for 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.