Skip to content

Commit

Permalink
bugfix: Can update the test result for cucumb tests
Browse files Browse the repository at this point in the history
Signed-off-by: Sheng Chen <sheche@microsoft.com>
  • Loading branch information
jdneo committed Jun 2, 2020
1 parent f2c227a commit 499af32
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 6 deletions.
16 changes: 11 additions & 5 deletions src/runners/junitRunner/JUnitRunnerResultAnalyzer.ts
Expand Up @@ -30,17 +30,23 @@ export class JUnitRunnerResultAnalyzer extends BaseRunnerResultAnalyzer {
return;
}
this.currentTestItem = testId;
this.testIds.add(testId);

let result: ITestResult | undefined = testResultManager.getResultById(testId);
if (!result) {
let result: ITestResult;
if (this.testIds.has(testId)) {
result = Object.assign({}, testResultManager.getResultById(testId), {
id: testId,
status: TestStatus.Running,
});
} else {
// the test has not been executed in current test session.
// create a new result object
result = {
id: testId,
status: TestStatus.Running,
};
} else if (result.status === TestStatus.Pending) {
result.status = TestStatus.Running;
this.testIds.add(testId);
}

const start: number = Date.now();
if (data.indexOf(MessageId.IGNORE_TEST_PREFIX) > -1) {
result.status = TestStatus.Skip;
Expand Down
31 changes: 31 additions & 0 deletions test/gradle-junit5-suite/codelens.test.ts
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

import * as assert from 'assert';
import * as fse from 'fs-extra';
import { CodeLens, Command, commands, TextDocument, window, workspace, extensions } from 'vscode';
import { ITestResult, TestCodeLensProvider, testResultManager, TestStatus, ITestItem } from '../../extension.bundle';
import { Token, Uris } from '../shared';
Expand Down Expand Up @@ -63,6 +64,36 @@ suite('Code Lens Tests', function() {
assert.ok(failedDetail!.duration !== undefined, 'Should have execution time');
});

test("Can correctly update the test results for cucumber tests", async function() {
const document: TextDocument = await workspace.openTextDocument(Uris.GRADLE_CUCUMBER_TEST);
await window.showTextDocument(document);

const codeLensProvider: TestCodeLensProvider = new TestCodeLensProvider();
const codeLens: CodeLens[] = await codeLensProvider.provideCodeLenses(document, Token.cancellationToken);

const command: Command | undefined = codeLens[0].command;

const testItem: ITestItem[] = command!.arguments as ITestItem[];

await commands.executeCommand(command!.command, testItem[0]);
const projectName: string = testItem[0].project;

let result: ITestResult| undefined = testResultManager.getResultById(`${projectName}@The calculator application#client wants to add 2 numbers`);
assert.equal(result!.status, TestStatus.Fail);

// Correct the test case
const fileContent: string = await fse.readFile(Uris.GRADLE_CUCUMBER_STEP.fsPath, 'utf-8');
await fse.writeFile(Uris.GRADLE_CUCUMBER_STEP.fsPath,
fileContent.replace('assertEquals(value + 1, 6);', 'assertEquals(value, 6);'), {encoding: 'utf-8'});

await commands.executeCommand(command!.command, testItem[0]);
result = testResultManager.getResultById(`${projectName}@The calculator application#client wants to add 2 numbers`);
assert.equal(result!.status, TestStatus.Pass);

// revert the file change
await fse.writeFile(Uris.GRADLE_CUCUMBER_STEP.fsPath, fileContent, {encoding: 'utf-8'});
});

teardown(async function() {
// Clear the result cache
testResultManager.dispose();
Expand Down
2 changes: 2 additions & 0 deletions test/shared.ts
Expand Up @@ -39,6 +39,8 @@ export namespace Uris {
const GRADLE_JUNIT5_TEST_PACKAGE: string = path.join('junit5', 'src', 'test', 'java', 'junit5');
export const GRADLE_JUNIT5_PARAMETERIZED_TEST: Uri = Uri.file(path.join(TEST_PROJECT_BASE_PATH, GRADLE_JUNIT5_TEST_PACKAGE, 'ParameterizedAnnotationTest.java'));
export const GRADLE_JUNIT5_PROPERTY_TEST: Uri = Uri.file(path.join(TEST_PROJECT_BASE_PATH, GRADLE_JUNIT5_TEST_PACKAGE, 'PropertyTest.java'));
export const GRADLE_CUCUMBER_TEST: Uri = Uri.file(path.join(TEST_PROJECT_BASE_PATH, GRADLE_JUNIT5_TEST_PACKAGE, 'cucumber', 'CucumberTest.java'));
export const GRADLE_CUCUMBER_STEP: Uri = Uri.file(path.join(TEST_PROJECT_BASE_PATH, GRADLE_JUNIT5_TEST_PACKAGE, 'cucumber', 'CucumberSteps.java'));
}

export async function getJavaVersion(): Promise<number> {
Expand Down
4 changes: 3 additions & 1 deletion test/test-projects/junit5/build.gradle
Expand Up @@ -8,7 +8,9 @@ repositories {

dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.6.0'
testImplementation "net.jqwik:jqwik:1.2.7"
testImplementation 'net.jqwik:jqwik:1.2.7'
testImplementation 'io.cucumber:cucumber-java:5.6.0'
testImplementation 'io.cucumber:cucumber-junit:5.6.0'
}

test {
Expand Down
@@ -0,0 +1,18 @@
package junit5.cucumber;

import static org.junit.jupiter.api.Assertions.assertEquals;

import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

public class CucumberSteps {

@When("the client add {int} and {int}")
public void addNumbers(Integer int1, Integer int2) {
}

@Then("the result should be {int}")
public void checkSumValue(Integer value) {
assertEquals(value + 1, 6);
}
}
@@ -0,0 +1,9 @@
package junit5.cucumber;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources")
public class CucumberTest {}
4 changes: 4 additions & 0 deletions test/test-projects/junit5/src/test/resources/Test.feature
@@ -0,0 +1,4 @@
Feature: The calculator application
Scenario: client wants to add 2 numbers
When the client add 2 and 4
Then the result should be 6

0 comments on commit 499af32

Please sign in to comment.