Skip to content

Commit ed155af

Browse files
authored
remove asserts from catchable code for testing (microsoft#22210)
some asserts were inside functions / mocking and with this then the extension code catches the exception and doesn't error out as the test. Bring the asserts out of the functions into the test so the asserts work as expected.
1 parent eada0f1 commit ed155af

File tree

4 files changed

+62
-27
lines changed

4 files changed

+62
-27
lines changed

src/test/testing/common/testingAdapter.test.ts

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -653,17 +653,21 @@ suite('End to End Tests: test adapters', () => {
653653
if (data.error === undefined) {
654654
// Dereference a NULL pointer
655655
const indexOfTest = JSON.stringify(data).search('Dereference a NULL pointer');
656-
assert.notDeepEqual(indexOfTest, -1, 'Expected test to have a null pointer');
657-
} else {
658-
assert.ok(data.error, "Expected errors in 'error' field");
656+
if (indexOfTest === -1) {
657+
failureOccurred = true;
658+
failureMsg = 'Expected test to have a null pointer';
659+
}
660+
} else if (data.error.length === 0) {
661+
failureOccurred = true;
662+
failureMsg = "Expected errors in 'error' field";
659663
}
660664
} else {
661665
const indexOfTest = JSON.stringify(data.tests).search('error');
662-
assert.notDeepEqual(
663-
indexOfTest,
664-
-1,
665-
'If payload status is not error then the individual tests should be marked as errors. This should occur on windows machines.',
666-
);
666+
if (indexOfTest === -1) {
667+
failureOccurred = true;
668+
failureMsg =
669+
'If payload status is not error then the individual tests should be marked as errors. This should occur on windows machines.';
670+
}
667671
}
668672
} catch (err) {
669673
failureMsg = err ? (err as Error).toString() : '';
@@ -705,22 +709,32 @@ suite('End to End Tests: test adapters', () => {
705709
if (data.error === undefined) {
706710
// Dereference a NULL pointer
707711
const indexOfTest = JSON.stringify(data).search('Dereference a NULL pointer');
708-
assert.notDeepEqual(indexOfTest, -1, 'Expected test to have a null pointer');
709-
} else {
710-
assert.ok(data.error, "Expected errors in 'error' field");
712+
if (indexOfTest === -1) {
713+
failureOccurred = true;
714+
failureMsg = 'Expected test to have a null pointer';
715+
}
716+
} else if (data.error.length === 0) {
717+
failureOccurred = true;
718+
failureMsg = "Expected errors in 'error' field";
711719
}
712720
} else {
713721
const indexOfTest = JSON.stringify(data.result).search('error');
714-
assert.notDeepEqual(
715-
indexOfTest,
716-
-1,
717-
'If payload status is not error then the individual tests should be marked as errors. This should occur on windows machines.',
718-
);
722+
if (indexOfTest === -1) {
723+
failureOccurred = true;
724+
failureMsg =
725+
'If payload status is not error then the individual tests should be marked as errors. This should occur on windows machines.';
726+
}
727+
}
728+
if (data.result === undefined) {
729+
failureOccurred = true;
730+
failureMsg = 'Expected results to be present';
719731
}
720-
assert.ok(data.result, 'Expected results to be present');
721732
// make sure the testID is found in the results
722733
const indexOfTest = JSON.stringify(data).search('test_seg_fault.TestSegmentationFault.test_segfault');
723-
assert.notDeepEqual(indexOfTest, -1, 'Expected testId to be present');
734+
if (indexOfTest === -1) {
735+
failureOccurred = true;
736+
failureMsg = 'Expected testId to be present';
737+
}
724738
} catch (err) {
725739
failureMsg = err ? (err as Error).toString() : '';
726740
failureOccurred = true;

src/test/testing/common/testingPayloadsEot.test.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,20 @@ suite('EOT tests', () => {
165165
mockProc.emit('close', 0, null);
166166
client.end();
167167
});
168-
168+
let errorBool = false;
169+
let errorMessage = '';
169170
resultResolver = new PythonResultResolver(testController, PYTEST_PROVIDER, workspaceUri);
170171
resultResolver._resolveExecution = async (payload, _token?) => {
171172
// the payloads that get to the _resolveExecution are all data and should be successful.
172173
actualCollectedResult = actualCollectedResult + JSON.stringify(payload.result);
173-
assert.strictEqual(payload.status, 'success', "Expected status to be 'success'");
174-
assert.ok(payload.result, 'Expected results to be present');
174+
if (payload.status !== 'success') {
175+
errorBool = true;
176+
errorMessage = "Expected status to be 'success'";
177+
}
178+
if (!payload.result) {
179+
errorBool = true;
180+
errorMessage = 'Expected results to be present';
181+
}
175182

176183
return Promise.resolve();
177184
};
@@ -208,6 +215,7 @@ suite('EOT tests', () => {
208215
actualCollectedResult,
209216
"Expected collected result to match 'data'",
210217
);
218+
assert.strictEqual(errorBool, false, errorMessage);
211219
});
212220
});
213221
});

src/test/testing/testController/server.unit.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,15 @@ suite('Python Test Server, DataWithPayloadChunks', () => {
117117
const dataWithPayloadChunks = testCaseDataObj;
118118

119119
await server.serverReady();
120-
120+
let errorOccur = false;
121+
let errorMessage = '';
121122
server.onRunDataReceived(({ data }) => {
122123
try {
123124
const resultData = JSON.parse(data).result;
124125
eventData = eventData + JSON.stringify(resultData);
125126
} catch (e) {
126-
assert(false, 'Error parsing data');
127+
errorOccur = true;
128+
errorMessage = 'Error parsing data';
127129
}
128130
deferred.resolve();
129131
});
@@ -143,6 +145,7 @@ suite('Python Test Server, DataWithPayloadChunks', () => {
143145
await deferred.promise;
144146
const expectedResult = dataWithPayloadChunks.data;
145147
assert.deepStrictEqual(eventData, expectedResult);
148+
assert.deepStrictEqual(errorOccur, false, errorMessage);
146149
});
147150
});
148151
});

src/test/testing/testController/unittest/testExecutionAdapter.unit.test.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@ suite('Unittest test execution adapter', () => {
3131

3232
test('runTests should send the run command to the test server', async () => {
3333
let options: TestCommandOptions | undefined;
34-
34+
let errorBool = false;
35+
let errorMessage = '';
3536
const stubTestServer = ({
3637
sendCommand(opt: TestCommandOptions, runTestIdPort?: string): Promise<void> {
3738
delete opt.outChannel;
3839
options = opt;
39-
assert(runTestIdPort !== undefined);
40+
if (runTestIdPort === undefined) {
41+
errorBool = true;
42+
errorMessage = 'runTestIdPort is undefined';
43+
}
4044
return Promise.resolve();
4145
},
4246
onRunDataReceived: () => {
@@ -60,6 +64,7 @@ suite('Unittest test execution adapter', () => {
6064
testIds,
6165
};
6266
assert.deepStrictEqual(options, expectedOptions);
67+
assert.equal(errorBool, false, errorMessage);
6368
});
6469
});
6570
test('runTests should respect settings.testing.cwd when present', async () => {
@@ -69,12 +74,16 @@ suite('Unittest test execution adapter', () => {
6974
}),
7075
} as unknown) as IConfigurationService;
7176
let options: TestCommandOptions | undefined;
72-
77+
let errorBool = false;
78+
let errorMessage = '';
7379
const stubTestServer = ({
7480
sendCommand(opt: TestCommandOptions, runTestIdPort?: string): Promise<void> {
7581
delete opt.outChannel;
7682
options = opt;
77-
assert(runTestIdPort !== undefined);
83+
if (runTestIdPort === undefined) {
84+
errorBool = true;
85+
errorMessage = 'runTestIdPort is undefined';
86+
}
7887
return Promise.resolve();
7988
},
8089
onRunDataReceived: () => {
@@ -99,6 +108,7 @@ suite('Unittest test execution adapter', () => {
99108
testIds,
100109
};
101110
assert.deepStrictEqual(options, expectedOptions);
111+
assert.equal(errorBool, false, errorMessage);
102112
});
103113
});
104114
});

0 commit comments

Comments
 (0)