Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding all parametrised tests to zephyrscale_result.json #103

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@

<groupId>com.smartbear</groupId>
<artifactId>zephyrscale-junit-integration</artifactId>
<version>2.0.1</version>
<version>2.0.2-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Zephyr Scale JUnit Integration</name>
<description>JUnit utility to integrate JUnit tests with Zephyr Scale.</description>
<url>https://bitbucket.org/smartbeartm4j/zephyrscale-junit-integration</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>4.11</junit.version>
<jackson-databind.version>2.13.4.1</jackson-databind.version>
<commons-lang3.version>3.0</commons-lang3.version>
<slf4j-simple.version>1.7.25</slf4j-simple.version>
<lombok.version>1.18.26</lombok.version>
</properties>

<licenses>
<license>
<name>The Apache License, Version 2.0</name>
Expand All @@ -38,26 +47,35 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<version>${junit.version}</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.4.1</version>
<version>${jackson-databind.version}</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0</version>
<version>${commons-lang3.version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
<version>${slf4j-simple.version}</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>


</dependencies>

<profiles>
Expand Down Expand Up @@ -143,8 +161,4 @@
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Original file line number Diff line number Diff line change
@@ -1,74 +1,82 @@
package com.smartbear.zephyrscale.junit;

import com.smartbear.zephyrscale.junit.annotation.TestCase;
import com.smartbear.zephyrscale.junit.builder.CustomFormatContainerBuilder;
import com.smartbear.zephyrscale.junit.decorator.TestDescriptionDecorator;
import static com.smartbear.zephyrscale.junit.file.CustomFormatFile.generateCustomFormatFile;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import org.slf4j.Logger;

import java.util.ArrayList;
import java.util.List;

import static com.smartbear.zephyrscale.junit.file.CustomFormatFile.generateCustomFormatFile;
import static org.apache.commons.lang3.StringUtils.isEmpty;
import com.smartbear.zephyrscale.junit.annotation.TestCase;
import com.smartbear.zephyrscale.junit.customformat.CustomFormatContainer;
import com.smartbear.zephyrscale.junit.customformat.CustomFormatExecution;
import com.smartbear.zephyrscale.junit.customformat.CustomFormatExecution.CustomFormatExecutionBuilder;
import com.smartbear.zephyrscale.junit.customformat.CustomFormatTestCase;
import com.smartbear.zephyrscale.junit.customformat.CustomFormatTestCase.CustomFormatTestCaseBuilder;

public class ExecutionListener extends RunListener {

private final Logger logger = org.slf4j.LoggerFactory.getLogger(ExecutionListener.class);

private CustomFormatContainerBuilder customFormatContainerBuilder;
private List<String> errorMessages = new ArrayList<>();
List<CustomFormatExecution> executionList = null;

private boolean status = true;

@Override
public void testRunStarted(Description description) throws Exception {

super.testRunStarted(description);

customFormatContainerBuilder = new CustomFormatContainerBuilder();
executionList = Collections.synchronizedList(new ArrayList<CustomFormatExecution>());
}

@Override
public void testFailure(Failure failure) throws Exception {

super.testFailure(failure);

customFormatContainerBuilder.registerFailure(new TestDescriptionDecorator(failure.getDescription()));
status = false;
String message = failure.getException() != null && failure.getMessage() != null ? "Failed: " + failure.getMessage() : "Failed";
executionList.add(getExecution(failure.getDescription(), message));
}


@Override
public void testIgnored(Description description) throws Exception {
super.testIgnored(description);
status = false;
executionList.add(getExecution(description, "Ignored"));
}

@Override
public void testFinished(Description description) throws Exception {
super.testFinished(description);
checkTestCaseAnnotation(description);

customFormatContainerBuilder.registerFinished(new TestDescriptionDecorator(description));
if(status)
executionList.add(getExecution(description, "Passed"));
status = true;
}

@Override
public void testRunFinished(Result result) throws Exception {
super.testRunFinished(result);

if (!errorMessages.isEmpty()) {
printErrorMessages();
} else {
generateCustomFormatFile(customFormatContainerBuilder.getCustomFormatContainer());
}
}

private void checkTestCaseAnnotation(Description description) {
TestCase testCaseAnnotation = description != null ? description.getAnnotation(TestCase.class) : null;

if (testCaseAnnotation != null && allFieldsAreEmpty(testCaseAnnotation)) {
errorMessages.add("[ERROR - Zephyr Scale] You must inform at least one parameter to TestCase annotation in method " + description.getDisplayName());
}
}

private boolean allFieldsAreEmpty(TestCase testCaseAnnotation) {
return isEmpty(testCaseAnnotation.key()) && isEmpty(testCaseAnnotation.name());

generateCustomFormatFile(CustomFormatContainer.builder().executions(executionList).build());
}

private void printErrorMessages() {
logger.error("[ERROR - Zephyr Scale] Zephyr Scale Results JSON File was not generated due to the following errors");
errorMessages.forEach(errorMessage -> logger.error(errorMessage));
private CustomFormatExecution getExecution(Description description, String result) {

CustomFormatExecutionBuilder custFormatExecutionBuilder = CustomFormatExecution.builder();
custFormatExecutionBuilder.source(description.getTestClass().getName() + "." + description.getMethodName());
custFormatExecutionBuilder.result(result);

CustomFormatTestCaseBuilder customFormatTestCaseBuilder = CustomFormatTestCase.builder();

TestCase testCase = description.getAnnotation(TestCase.class);
if(testCase != null) {
customFormatTestCaseBuilder.key(testCase.key()).name(testCase.name());
custFormatExecutionBuilder.testCase(customFormatTestCaseBuilder.build());
}
return custFormatExecutionBuilder.build();
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,39 +1,32 @@
package com.smartbear.zephyrscale.junit.customformat;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class CustomFormatContainer {

private Integer version = 1;

private List<CustomFormatExecution> executions = new ArrayList<>();
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

public List<CustomFormatExecution> getExecutions() {
return executions;
}
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Builder.Default;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

public void setExecutions(List<CustomFormatExecution> executions) {
this.executions = executions;
}

public void addResult(CustomFormatExecution customFormatExecution) {
executions.add(customFormatExecution);
}

public Optional<CustomFormatExecution> getExecutionBySource(String source) {
return executions
.stream()
.filter(r -> r.getSource().equals(source))
.findFirst();
}
@Builder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(Include.NON_NULL)
public class CustomFormatContainer {

public Integer getVersion() {
return version;
}
@Default
private Integer version = 1;

public void setVersion(Integer version) {
this.version = version;
}
private List<CustomFormatExecution> executions;

// To mitigate javadoc compile time issue for lombok
// https://stackoverflow.com/questions/51947791/javadoc-cannot-find-symbol-error-when-using-lomboks-builder-annotation
public static class CustomFormatContainerBuilder {}
}
Loading