In this blog, we would discuss how we can create different annotations for Gradle test tasks and tag your testcases with these annotations. We would assume you are using Junit framework.
Creating annotation for Gradle test tasks:
Tasks are defined as Annotations in Gradle, so we need to create the annotations. Let’s create an annotations folder in your src/main under your package name.
To create at the annotations, right click on the folder -> New -> Java Class -> select Annotation.
Example:
Now Let’s create annotations for SmokeTest and Regression. Considering your package name is com.example.demo, the code would look like as below:
package com.example.demo.annotations;
import org.junit.jupiter.api.Tag;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Tag("smoketest")
public @interface SmokeTest {
}
and
package com.example.demo.annotations;
import org.junit.jupiter.api.Tag;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
public Regression {
}
Now that you have created the annotations, define the tasks in build.gradle.
Example:
task SmokeTest(type: Test, description: 'Run Smoke tests on App...') {
useJUnitPlatform() { includeTags 'smoketest' }
reports {
html.required.set(false)
junitXml.required.set(false)
}
}
task Regression(type: Test, description: 'Run Regression tests on App...') {
useJUnitPlatform() { includeTags 'regression' }
reports {
html.required.set(false)
junitXml.required.set(false)
}
}
Turn on or off html reporting option base don or need by setting it to true or false.
Tagging test cases with Gradle test tasks:
Now that you have created the Gradle test tasks, the only remaining thing is to tag your Junit tests with these tasks.
Example:
public void testcase1() throws Exception {
String tcName = new Object() {
}.getClass().getEnclosingMethod().getName();
log("Test Name" + tcName);
SoftAssertions.assertSoftly(
softAssertions -> {
softAssertions.assertThat(testAppScreen.isScreenTitleDisplayed()).as("The Screen title is displayed").isTrue();
}
);
testStatus = testAppScreen.isScreenTitleDisplayed() ? "Passed" : "Failed";
updateTCPassCount();
}
@Test annotation is used to define a test method in Junit. @Regression @Regression is the annotation we created in build.gradle and defined under annotations folder.