Skip to content

Commit

Permalink
Retain state of tests when auto discovering tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DonJayamanne committed Mar 5, 2019
1 parent e006680 commit 418a1b2
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 11 deletions.
1 change: 1 addition & 0 deletions news/1 Enhancements/4576.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Retain state of tests when auto discovering tests.
137 changes: 137 additions & 0 deletions src/test/unittests/common/services/storageService.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

'use strict';

import * as assert from 'assert';
import { instance, mock } from 'ts-mockito';
import { Uri } from 'vscode';
import { IWorkspaceService } from '../../../../client/common/application/types';
import { WorkspaceService } from '../../../../client/common/application/workspace';
import { TestCollectionStorageService } from '../../../../client/unittests/common/services/storageService';
import { TestResultsService } from '../../../../client/unittests/common/services/testResultsService';
import { FlattenedTestFunction, FlattenedTestSuite, ITestCollectionStorageService, ITestResultsService, TestFile, TestFolder, TestFunction, Tests, TestStatus, TestSuite, TestType } from '../../../../client/unittests/common/types';
import { createMockTestDataItem } from '../testUtils.unit.test';

// tslint:disable:no-any max-func-body-length
suite('Unit Tests - Storage Service', () => {
let storageService: ITestCollectionStorageService;
let resultsService: ITestResultsService;
let workspaceService: IWorkspaceService;
const workspaceUri = Uri.file(__dirname);
let testData1: Tests;
let testData2: Tests;
setup(() => {
resultsService = mock(TestResultsService);
workspaceService = mock(WorkspaceService);
storageService = new TestCollectionStorageService([], instance(resultsService), instance(workspaceService));
setupTestData1();
setupTestData2();
});

function setupTestData1() {
const folder1 = createMockTestDataItem<TestFolder>(TestType.testFolder, '1');
const file1 = createMockTestDataItem<TestFile>(TestType.testFile, '1');
folder1.testFiles.push(file1);
const suite1 = createMockTestDataItem<TestSuite>(TestType.testSuite, '1');
const suite2 = createMockTestDataItem<TestSuite>(TestType.testSuite, '2');
const fn1 = createMockTestDataItem<TestFunction>(TestType.testFunction, '1');
const fn2 = createMockTestDataItem<TestFunction>(TestType.testFunction, '2');
const fn3 = createMockTestDataItem<TestFunction>(TestType.testFunction, '3');
file1.suites.push(suite1);
file1.suites.push(suite2);
file1.functions.push(fn1);
suite1.functions.push(fn2);
suite2.functions.push(fn3);
const flattendSuite1: FlattenedTestSuite = {
testSuite: suite1,
xmlClassName: suite1.xmlName
} as any;
const flattendSuite2: FlattenedTestSuite = {
testSuite: suite2,
xmlClassName: suite2.xmlName
} as any;
const flattendFn1: FlattenedTestFunction = {
testFunction: fn1,
xmlClassName: fn1.name
} as any;
const flattendFn2: FlattenedTestFunction = {
testFunction: fn2,
xmlClassName: fn2.name
} as any;
const flattendFn3: FlattenedTestFunction = {
testFunction: fn3,
xmlClassName: fn3.name
} as any;
testData1 = {
rootTestFolders: [folder1],
summary: { errors: 0, skipped: 0, passed: 0, failures: 0 },
testFiles: [file1],
testFolders: [folder1],
testFunctions: [flattendFn1, flattendFn2, flattendFn3],
testSuites: [flattendSuite1, flattendSuite2]
};
}

function setupTestData2() {
const folder1 = createMockTestDataItem<TestFolder>(TestType.testFolder, '1');
const file1 = createMockTestDataItem<TestFile>(TestType.testFile, '1');
folder1.testFiles.push(file1);
const suite1 = createMockTestDataItem<TestSuite>(TestType.testSuite, '1');
const suite2 = createMockTestDataItem<TestSuite>(TestType.testSuite, '2');
const fn1 = createMockTestDataItem<TestFunction>(TestType.testFunction, '1');
const fn2 = createMockTestDataItem<TestFunction>(TestType.testFunction, '2');
const fn3 = createMockTestDataItem<TestFunction>(TestType.testFunction, '3');
file1.suites.push(suite1);
file1.suites.push(suite2);
suite1.functions.push(fn1);
suite1.functions.push(fn2);
suite2.functions.push(fn3);
const flattendSuite1: FlattenedTestSuite = {
testSuite: suite1,
xmlClassName: suite1.xmlName
} as any;
const flattendSuite2: FlattenedTestSuite = {
testSuite: suite2,
xmlClassName: suite2.xmlName
} as any;
const flattendFn1: FlattenedTestFunction = {
testFunction: fn1,
xmlClassName: fn1.name
} as any;
const flattendFn2: FlattenedTestFunction = {
testFunction: fn2,
xmlClassName: fn2.name
} as any;
const flattendFn3: FlattenedTestFunction = {
testFunction: fn3,
xmlClassName: fn3.name
} as any;
testData2 = {
rootTestFolders: [folder1],
summary: { errors: 0, skipped: 0, passed: 0, failures: 0 },
testFiles: [file1],
testFolders: [folder1],
testFunctions: [flattendFn1, flattendFn2, flattendFn3],
testSuites: [flattendSuite1, flattendSuite2]
};
}

test('Merge Status from existing tests', () => {
testData1.testFunctions[0].testFunction.passed = true;
testData1.testFunctions[1].testFunction.status = TestStatus.Fail;
testData1.testFunctions[2].testFunction.time = 1234;

assert.notDeepEqual(testData1.testFunctions[0].testFunction, testData2.testFunctions[0].testFunction);
assert.notDeepEqual(testData1.testFunctions[1].testFunction, testData2.testFunctions[1].testFunction);
assert.notDeepEqual(testData1.testFunctions[2].testFunction, testData2.testFunctions[2].testFunction);

storageService.storeTests(workspaceUri, testData1);
storageService.storeTests(workspaceUri, testData2);

// Function 1 is in a different suite now, hence should not get updated.
assert.notDeepEqual(testData1.testFunctions[0].testFunction, testData2.testFunctions[0].testFunction);
assert.deepEqual(testData1.testFunctions[1].testFunction, testData2.testFunctions[1].testFunction);
assert.deepEqual(testData1.testFunctions[2].testFunction, testData2.testFunctions[2].testFunction);
});
});
24 changes: 13 additions & 11 deletions src/test/unittests/common/testUtils.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,38 @@ import { getParent, getTestFile, getTestFolder, getTestFunction, getTestSuite, g
import { FlattenedTestFunction, FlattenedTestSuite, TestFile, TestFolder, TestFunction, Tests, TestSuite, TestType } from '../../../client/unittests/common/types';
import { TestDataItem } from '../../../client/unittests/types';

export function createMockTestDataItem<T extends TestDataItem>(type: TestType) {
// tslint:disable:prefer-template

export function createMockTestDataItem<T extends TestDataItem>(type: TestType, nameSuffix: string = '') {
const folder: TestFolder = {
folders: [],
name: 'Some Folder',
nameToRun: ' Some Folder',
name: 'Some Folder' + nameSuffix,
nameToRun: ' Some Folder' + nameSuffix,
testFiles: [],
time: 0
};
const file: TestFile = {
name: 'Some File',
nameToRun: ' Some File',
name: 'Some File' + nameSuffix,
nameToRun: ' Some File' + nameSuffix,
fullPath: __filename,
xmlName: 'some xml name',
xmlName: 'some xml name' + nameSuffix,
functions: [],
suites: [],
time: 0
};
const func: TestFunction = {
name: 'Some Function',
nameToRun: ' Some Function',
name: 'Some Function' + nameSuffix,
nameToRun: ' Some Function' + nameSuffix,
time: 0
};
const suite: TestSuite = {
name: 'Some Suite',
nameToRun: ' Some Suite',
name: 'Some Suite' + nameSuffix,
nameToRun: ' Some Suite' + nameSuffix,
functions: [],
isInstance: true,
isUnitTest: false,
suites: [],
xmlName: 'some name',
xmlName: 'some name' + nameSuffix,
time: 0
};

Expand Down

0 comments on commit 418a1b2

Please sign in to comment.