How to prepare automated tests
Whichever component of your application you are testing, there is a simple set of tools that you can use. This page looks at those tools and the dependencies that you must declare before you can start automated testing.
Tools for testing
Genesis uses the following tools for testing:
- Cucumber BDD
- JUnit5
- Rest Assured
- Playwright Java
- Jackson Databind
- Allure Report
The full dependencies are listed below:
dependencies {
compileOnly(genesis("script-dependencies"))
genesisGeneratedCode(withTestDependency = true)
testImplementation(genesis("dbtest"))
testImplementation(genesis("testsupport"))
testImplementation(genesis("pal-eventhandler"))
testImplementation("io.cucumber:cucumber-java:latest.release")
testImplementation("io.cucumber:cucumber-junit-platform-engine:latest.release")
testImplementation("org.junit.platform:junit-platform-suite:latest.release")
testImplementation("org.junit.jupiter:junit-jupiter-api:latest.release")
testImplementation("io.rest-assured:rest-assured:4.4.0")
testImplementation("io.rest-assured:json-schema-validator:latest.release")
testImplementation("com.fasterxml.jackson.core:jackson-databind:2.13.3")
testImplementation("com.microsoft.playwright:playwright:latest.release")
testImplementation(platform("io.qameta.allure:allure-bom:$allureVersion"))
testImplementation("io.qameta.allure:allure-cucumber7-jvm")
testImplementation("io.qameta.allure:allure-junit-platform")
agent("org.aspectj:aspectjweaver:${aspectJVersion}")
testImplementation("io.qameta.allure:allure-junit4-aspect")
testImplementation("ch.qos.logback:logback-classic:1.5.6")
implementation("ch.qos.logback:logback-core:1.5.6")
}
Feature files
The test steps are defined in Feature files, which are written in the Gherkin language. These can be found in the folder test/resources/features. Here is an example file:
@Permissions
@AmyAccess
@API-DataServer
Scenario Outline: Login with AmyAccess and retrieve trades
Given User connect with username "AmyAccess" and password "genesis"
When User sends request to dataserver gets response body "<endpoints>"
Then User compares it to expected "<result>"
Examples:
| endpoints | result |
| ALL_TRADES_1 | src/test/resources/result/AmyAccess/expected/ALL_TRADES_1_expected.json |
| ALL_TRADES_2 | src/test/resources/result/AmyAccess/expected/ALL_TRADES_2_expected.json |
| ALL_TRADES_3 | src/test/resources/result/AmyAccess/expected/ALL_TRADES_3_expected.json |
| ALL_TRADES_4 | src/test/resources/result/AmyAccess/expected/ALL_TRADES_4_expected.json |
Step definitions
CucumberRunner contains a configuration that must match the methods in the step definitions and feature files.
The correct paths of the step definitions and feature files must be passed into the configuration. The step definitions can be found in the folder test/java/stepdefinitions. Here is an example file:
@Given("User connect with username {string} and password {string}")
public void user_connect_with_username_and_password(String username, String password) {
USERNAME = username;
EventLoginAuth eventLoginAuth = new EventLoginAuth(username, password);
Response response = eventLoginAuth.post();
SESSION_AUTH_TOKEN = response.jsonPath().get("SESSION_AUTH_TOKEN");
response.then().assertThat().body(matchesJsonSchemaInClasspath(LOGIN_SCHEMA_PATH));
}
@When("User sends request to dataserver gets response body {string}")
public void user_sends_request_to_dataserver_with_endpoint(String endpoint) {
this.endpoint = endpoint;
DataServer dataServer = new DataServer(endpoint);
Response response = dataServer.post();
}
@When("User compares it to expected {string}")
public void user_compares_it_to_expected(String result) {
if (endpoint.contains("ALL")) {
DataServer dataServer = new DataServer(endpoint);
response = dataServer.post();
}
if (endpoint.contains("RR")) {
ReqRep reqRep = new ReqRep(endpoint, queryParam);
response = reqRep.get();
}
String actualFilePath = getTradeJSON(response, USERNAME, endpoint, "actual");
compareJSONFiles(result, actualFilePath, PRIMARY_KEY);
}
To perform assertion, we use expected and actual files, which are generated by the required steps.
The files can be found in the folders:
- test/resources/result/username/expected/
- test/resources/result/username/actual/
Allure report
Whenever a test fails, the last state of the GUI is captured and attached to the Allure report. If there is a file in the user/actual folder related to the test, it will also be attached to the Allure report.
Allure exports data to the folder build/allure-results. You can configure this in the allure.properties file in the test/resources folder.
To generate the Allure report, use the command:
allure generate build/allure-results/ --clean
This generates a folder called allure-report in the main the folder of the project.