Skip to content

Commit

Permalink
Merge pull request #2337 from akshita31/improve_logging
Browse files Browse the repository at this point in the history
Improve logging of tests
  • Loading branch information
akshita31 authored May 24, 2018
2 parents aec919f + 9f7b820 commit 4be8950
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
32 changes: 27 additions & 5 deletions src/observers/DotnetTestLoggerObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ export default class DotNetTestLoggerObserver extends BaseLoggerObserver {
}

private handleDotnetTestDebugStart(event: DotNetTestDebugStart) {
this.logger.appendLine(`Debugging method ${event.testMethod}...`);
this.logger.appendLine(`----- Debugging test method ${event.testMethod} -----`);
this.logger.appendLine('');
}

private handleDotnetTestRunStart(event: DotNetTestRunStart): any {
this.logger.appendLine(`Running test ${event.testMethod}...`);
this.logger.appendLine(`----- Running test method "${event.testMethod}" -----`);
this.logger.appendLine('');
}

Expand All @@ -54,12 +54,14 @@ export default class DotNetTestLoggerObserver extends BaseLoggerObserver {
}

private handleReportDotnetTestResults(event: ReportDotNetTestResults) {
this.logger.appendLine("----- Test Execution Summary -----");
this.logger.appendLine('');
const results = event.results;
const totalTests = results.length;

let totalPassed = 0, totalFailed = 0, totalSkipped = 0;
for (let result of results) {
this.logger.appendLine(`${result.MethodName}: ${result.Outcome}`);
this.logTestResult(result);
switch (result.Outcome) {
case protocol.V2.TestOutcomes.Failed:
totalFailed += 1;
Expand All @@ -73,8 +75,28 @@ export default class DotNetTestLoggerObserver extends BaseLoggerObserver {
}
}

this.logger.appendLine('');
this.logger.appendLine(`Total tests: ${totalTests}. Passed: ${totalPassed}. Failed: ${totalFailed}. Skipped: ${totalSkipped}`);
this.logger.appendLine('');
}
}

private logTestResult(result: protocol.V2.DotNetTestResult) {
this.logger.appendLine(`${result.MethodName}:`);
this.logger.increaseIndent();
this.logger.appendLine(`Outcome: ${processOutcome(result.Outcome)}`);
if (result.ErrorMessage) {
this.logger.appendLine(`Error Message: ${result.ErrorMessage}`);

}

if (result.ErrorStackTrace) {
this.logger.appendLine(`Stack Trace: ${result.ErrorStackTrace}`);
}

this.logger.appendLine();
this.logger.decreaseIndent();
}
}

export function processOutcome(input: string) {
return input.charAt(0).toUpperCase() + input.slice(1);
}
22 changes: 14 additions & 8 deletions test/unitTests/logging/DotnetTestLoggerObserver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { expect } from 'chai';
import * as chai from 'chai';
import { getNullChannel } from '../testAssets/Fakes';
import { EventWithMessage, DotNetTestDebugWarning, DotNetTestDebugStart, BaseEvent, DotNetTestRunStart, DotNetTestDebugProcessStart, DotNetTestMessage, DotNetTestDebugComplete, ReportDotNetTestResults } from '../../../src/omnisharp/loggingEvents';
import DotNetTestLoggerObserver from '../../../src/observers/DotnetTestLoggerObserver';
import * as protocol from '../../../src/omnisharp/protocol';

const expect = chai.expect;
chai.use(require('chai-string'));

suite(`${DotNetTestLoggerObserver.name}`, () => {
let appendedMessage: string;
let observer = new DotNetTestLoggerObserver({
Expand Down Expand Up @@ -56,25 +59,28 @@ suite(`${DotNetTestLoggerObserver.name}`, () => {
suite(`${ReportDotNetTestResults.name}`, () => {
let event = new ReportDotNetTestResults(
[
getDotNetTestResults("foo", "failed", "assertion failed", ""),
getDotNetTestResults("failinator", "failed", "error occured", ""),
getDotNetTestResults("foo", "failed", "assertion failed", "stacktrace1"),
getDotNetTestResults("failinator", "failed", "error occured", "stacktrace2"),
getDotNetTestResults("bar", "skipped", "", ""),
getDotNetTestResults("passinator", "passed", "", ""),
]);

test(`Displays the outcome of each test`, () => {
observer.post(event);
event.results.forEach(result => {
expect(appendedMessage).to.contain(`${result.MethodName}: ${result.Outcome}`);
expect(appendedMessage).to.containIgnoreCase(`${result.MethodName}:\n Outcome: ${result.Outcome}`);
});
});

test(`Displays the total outcome`, () => {
observer.post(event);
expect(appendedMessage).to.contain(`Total tests: ${event.results.length}. Passed: 1. Failed: 2. Skipped: 1`);
event.results.forEach(result => {
expect(appendedMessage).to.contain(`${result.MethodName}: ${result.Outcome}`);
});
expect(appendedMessage).to.contain(`Total tests: 4. Passed: 1. Failed: 2. Skipped: 1`);
});

test('Displays the error message and error stack trace if any is present', () => {
observer.post(event);
expect(appendedMessage).to.contain("foo:\n Outcome: Failed\n Error Message: assertion failed\n Stack Trace: stacktrace1");
expect(appendedMessage).to.contain("failinator:\n Outcome: Failed\n Error Message: error occured\n Stack Trace: stacktrace2");
});
});
});
Expand Down

0 comments on commit 4be8950

Please sign in to comment.