Skip to content

Commit 69d133b

Browse files
committed
clear counters before tests #239
1 parent 2355f37 commit 69d133b

File tree

1 file changed

+61
-62
lines changed

1 file changed

+61
-62
lines changed

src/client/unittests/unittest/runner.ts

Lines changed: 61 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ interface TestStatusMap {
1717
status: TestStatus;
1818
summaryProperty: string;
1919
}
20+
2021
const outcomeMapping = new Map<string, TestStatusMap>();
2122
outcomeMapping.set('passed', { status: TestStatus.Pass, summaryProperty: 'passed' });
22-
outcomeMapping.set('failed', { status: TestStatus.Fail, summaryProperty: 'failed' });
23-
outcomeMapping.set('error', { status: TestStatus.Error, summaryProperty: 'error' });
23+
outcomeMapping.set('failed', { status: TestStatus.Fail, summaryProperty: 'failures' });
24+
outcomeMapping.set('error', { status: TestStatus.Error, summaryProperty: 'errors' });
2425
outcomeMapping.set('skipped', { status: TestStatus.Skipped, summaryProperty: 'skipped' });
2526

2627
interface ITestData {
@@ -29,14 +30,52 @@ interface ITestData {
2930
outcome: string;
3031
traceback: string;
3132
}
32-
const summary = {
33-
passed: 0,
34-
failed: 0,
35-
error: 0,
36-
skipped: 0
37-
};
3833

3934
export function runTest(rootDirectory: string, tests: Tests, args: string[], testsToRun?: TestsToRun, token?: CancellationToken, outChannel?: OutputChannel): Promise<Tests> {
35+
tests.summary.errors = 0;
36+
tests.summary.failures = 0;
37+
tests.summary.passed = 0;
38+
tests.summary.skipped = 0;
39+
40+
const testLauncherFile = path.join(__dirname, '..', '..', '..', '..', 'pythonFiles', 'PythonTools', 'visualstudio_py_testlauncher.py');
41+
const server = new Server();
42+
server.on('error', (message: string, ...data: string[]) => {
43+
console.log(`${message} ${data.join(' ')}`);
44+
});
45+
server.on('log', (message: string, ...data: string[]) => {
46+
});
47+
server.on('connect', (data) => {
48+
});
49+
server.on('start', (data: { test: string }) => {
50+
});
51+
server.on('result', (data: ITestData) => {
52+
const test = tests.testFunctions.find(t => t.testFunction.nameToRun === data.test);
53+
if (test) {
54+
const statusDetails = outcomeMapping.get(data.outcome);
55+
test.testFunction.status = statusDetails.status;
56+
test.testFunction.message = data.message;
57+
test.testFunction.traceback = data.traceback;
58+
tests.summary[statusDetails.summaryProperty] += 1;
59+
}
60+
});
61+
server.on('socket.disconnected', (data) => {
62+
});
63+
64+
return server.start().then(port => {
65+
let testPaths: string[] = getIdsOfTestsToRun(tests, testsToRun);
66+
for (let counter = 0; counter < testPaths.length; counter++) {
67+
testPaths[counter] = '-t' + testPaths[counter].trim();
68+
}
69+
let testArgs = buildTestArgs(args);
70+
testArgs = [testLauncherFile].concat(testArgs).concat(`--result-port=${port}`).concat(testPaths);
71+
return run(settings.pythonPath, testArgs, rootDirectory, token, outChannel);
72+
}).then(() => {
73+
updateResults(tests);
74+
return tests;
75+
});
76+
}
77+
78+
function buildTestArgs(args: string[]): string[] {
4079
let startDirectory = '.';
4180
let pattern = 'test*.py';
4281
const indexOfStartDir = args.findIndex(arg => arg.indexOf('-s') === 0);
@@ -67,74 +106,34 @@ export function runTest(rootDirectory: string, tests: Tests, args: string[], tes
67106
}
68107
}
69108
}
109+
const failFast = args.some(arg => arg.trim() === '-f' || arg.trim() === '--failfast');
70110
const verbosity = args.some(arg => arg.trim().indexOf('-v') === 0) ? 2 : 1;
71-
args = [`--us=${startDirectory}`, `--up=${pattern}`, `--uvInt=${verbosity}`];
72-
73-
summary.error = 0;
74-
summary.failed = 0;
75-
summary.skipped = 0;
76-
summary.passed = 0;
77-
let testPaths: string[] = [];
111+
const testArgs = [`--us=${startDirectory}`, `--up=${pattern}`, `--uvInt=${verbosity}`];
112+
if (failFast) {
113+
testArgs.push('--uf');
114+
}
115+
return testArgs;
116+
}
117+
function getIdsOfTestsToRun(tests: Tests, testsToRun: TestsToRun): string[] {
118+
const testIds = [];
78119
if (testsToRun && testsToRun.testFolder) {
79120
// Get test ids of files in these folders
80121
testsToRun.testFolder.map(folder => {
81122
tests.testFiles.forEach(f => {
82123
if (f.fullPath.startsWith(folder.name)) {
83-
testPaths.push(f.nameToRun);
124+
testIds.push(f.nameToRun);
84125
}
85126
});
86127
});
87128
}
88129
if (testsToRun && testsToRun.testFile) {
89-
testPaths = testPaths.concat(testsToRun.testFile.map(f => f.nameToRun));
130+
testIds.push(...testsToRun.testFile.map(f => f.nameToRun));
90131
}
91132
if (testsToRun && testsToRun.testSuite) {
92-
testPaths = testPaths.concat(testsToRun.testSuite.map(f => f.nameToRun));
133+
testIds.push(...testsToRun.testSuite.map(f => f.nameToRun));
93134
}
94135
if (testsToRun && testsToRun.testFunction) {
95-
testPaths = testPaths.concat(testsToRun.testFunction.map(f => f.nameToRun));
96-
}
97-
for (let counter = 0; counter < testPaths.length; counter++) {
98-
testPaths[counter] = '-t' + testPaths[counter].trim();
136+
testIds.push(...testsToRun.testFunction.map(f => f.nameToRun));
99137
}
100-
const testLauncherFile = path.join(__dirname, '..', '..', '..', '..', 'pythonFiles', 'PythonTools', 'visualstudio_py_testlauncher.py');
101-
const server = new Server();
102-
server.on('error', (message: string, ...data: string[]) => {
103-
console.log(`${message} ${data.join(' ')}`);
104-
});
105-
server.on('log', (message: string, ...data: string[]) => {
106-
});
107-
server.on('connect', (data) => {
108-
});
109-
server.on('start', (data: { test: string }) => {
110-
if (!data || typeof data.test !== 'string' || data.test.length === 0) {
111-
return;
112-
}
113-
const testFn = data.test.substring(data.test.lastIndexOf('.') + 1);
114-
const testClass = data.test.substring(0, data.test.lastIndexOf('.'));
115-
});
116-
server.on('result', (data: ITestData) => {
117-
const test = tests.testFunctions.find(t => t.testFunction.nameToRun === data.test);
118-
const statusDetails = outcomeMapping.get(data.outcome);
119-
if (test) {
120-
let statusToLog = '';
121-
test.testFunction.status = statusDetails.status;
122-
test.testFunction.message = data.message;
123-
test.testFunction.traceback = data.traceback;
124-
summary[statusDetails.summaryProperty] += 1;
125-
}
126-
});
127-
server.on('socket.disconnected', (data) => {
128-
});
129-
130-
return server.start().then(port => {
131-
return run(settings.pythonPath, [testLauncherFile].concat(args).concat(`--result-port=${port}`).concat(testPaths), rootDirectory, token, outChannel);
132-
}).then(() => {
133-
tests.summary.errors = summary.error;
134-
tests.summary.failures = summary.failed;
135-
tests.summary.passed = summary.passed;
136-
tests.summary.skipped = summary.skipped;
137-
updateResults(tests);
138-
return tests;
139-
});
138+
return testIds;
140139
}

0 commit comments

Comments
 (0)