Maitiro Ekuwedzera Cucumber Rukoko pamusoro peZVIMWE-yakasimbiswa API Miedzo

Iyi posvo inopa-nhanho-nhanho gwara rekuti ungawedzera sei cucumber rukoko pamusoro peapi bvunzo yakanyorwa muREST-yakavimbiswa.

REST-yakavimbiswa DSL yatove inopa BDD-dhizaini kunyora bvunzo mune Yakapihwa-Kana-Ipapo fomati, asi ichiri kuvigwa mukodhi. Mune mamwe mazwi, kana iwe uchida kuona kuti ndeapi mamiriro akafukidzwa, iwe uchiri kufanira kuchera pasi mukati meapi bvunzo uye kuverenga iyo kodhi. Iko hakuna maficha maficha.

Chinangwa cheichi posvo ndechekugadzirisa zviripo REST-yakavimbiswa api bvunzo nekuwedzera cucumber uye maficha maficha, kuitira kuti zviitiko zviverengeke zvakajeka pasina kutarisa kodhi yepasi.

REST-yakavimbiswa API Bvunzo

Mumuenzaniso uyu, isu tinonyora kodhi kuyedza mushandisi kugadzira api.

Kutanga, isu tine yakazvimiririra REST-yakavimbiswa uye JUnit Bvunzo, iyo inogara mu:

src/test/java/io.devqa/scenarios



import io.restassured.RestAssured; import io.restassured.http.ContentType; import io.restassured.response.Response; import org.junit.jupiter.api.*; import static io.restassured.RestAssured.given; public class UserTests {
private static String path;
private static String validRequest = '{ ' +

' 'username': 'test-api-user', ' +

' 'email': 'test-api-user@email.com', ' +

' 'password': 'Passw0rd123!', ' +

' 'name': 'Test Api-User' }';
@BeforeAll
public static void setConfig() {
RestAssured.baseURI = 'https://localhost:8080';
path = '/users';
}
@Test
public void shouldBeAbleToCreateNewUser() {
Response createUser = given()


.auth()


.preemptive()


.basic('MY_USERNAME', 'MY_PASSWORD')


.header('Accept', ContentType.JSON.getAcceptHeader())


.contentType(ContentType.JSON)


.body(validRequest)


.post(path)


.then().extract().response();
Assertions.assertEquals(201, createUser.getStatusCode());

String username = createUser.jsonPath().get('username');
String email = createUser.jsonPath().get('email');
String name = createUser.jsonPath().get('name');
String id = createUser.jsonPath().get('id');

Assertions.assertEquals('test-api-user', username);
Assertions.assertEquals('test-api-user@email.com', email);
Assertions.assertEquals('Test Api-User', name);
Assertions.assertNotNull(id);
} }

Iyo bvunzo iri pamusoro inogona kumhanyiswa yakananga kubva kukirasi sezvo inogona kukokwa neJUnit.

I setConfig() nzira inoisa pre-chinodiwa. Iyo bvunzo nzira inoita zviito (kutumira iyo chikumbiro) uyezve ichisimbisa pane yekupindura kodhi uye yekupindura mubhadharo

Tevere, isu ticha tarisa maitiro ekuisa cucumber rukoko pamusoro apa pamusoro REST-yakavimbiswa api bvunzo.

Cucumber uye REST-yakavimbiswa API Bvunzo

Chinhu chekutanga chatinofanira kuita kuwedzera kugadzikana kwecucumber muchirongwa chedu.

Tichishandisa Gradle, mune yedu build.gradle faira, tinoisa izvi pasi pe dependencies:

dependencies {
testCompile 'io.cucumber:cucumber-java:6.2.2'
testCompile 'io.cucumber:cucumber-junit:6.2.2'
testCompile 'io.rest-assured:rest-assured:3.3.0'
testCompile 'com.jayway.jsonpath:json-path:2.4.0' }

Uye izvi pasi configuration mu build.gradle faira:

configurations {
cucumberRuntime {
extendsFrom testImplementation
} }

Isu tinodawo kugadzira basa mu build.gradle faira kumhanyisa iyo cucumber mafaera ane izvo zviitiko:

task cucumber() {
dependsOn assemble, compileTestJava
doLast {
mkdir 'build/test-results/'
javaexec {

main = 'io.cucumber.core.cli.Main'

classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output

args = ['--plugin', 'pretty', '--plugin', 'html:build/test-results/functional.html', '--plugin', 'junit:build/test-results/functional.xml','--tags', '@functional', '--glue', 'scenarios', 'src/test/resources']
}
} }

Dhizaini yeProjekti yeCucumber

Isu tinodawo kugadzirisa yedu purojekiti dhizaini kuti igadzirise shanduko dzemucucumber.

Iwo mafaira emafaira achachengetwa mu:

src/test/resources/scenarios

Tsananguro dzedanho dzichachengetedzwa mu

src/test/java/scenarios

Tevere, isu tichagadzira iyo ficha faira inonzi UserScenarios.feature ndokuisa pasi pe src/test/resources/scenarios forodha.

Iyo ficha faira ichaita senge:

@functional Feature: User Scenarios Scenario: I should be able to create a new user
Given the users endpoint exists
When I send a valid create user payload
Then response status code should be 201
And create user response should be valid

Zvino isu tinofanirwa kubvisa yedu REST-yakavimbiswa JUnit bvunzo kunyora nhanho tsananguro dzinogona kunamirwa kune zvirevo mune yedu ficha faira.

import io.cucumber.java.en.And; import io.cucumber.java.en.Given; import io.cucumber.java.en.Then; import io.cucumber.java.en.When; import io.restassured.RestAssured; import io.restassured.http.ContentType; import io.restassured.response.Response; import org.junit.jupiter.api.Assertions; import static io.restassured.RestAssured.given; public class UserScenarios {
private String path;
private Response response;
private String validRequest = '{ ' +

' 'username': 'test-api-user', ' +

' 'email': 'test-api-user@email.com', ' +

' 'password': 'Passw0rd123!', ' +

' 'name': 'Test Api-User' }';
@Given('the users endpoint exists')
public void preReq() {
RestAssured.baseURI = 'https://localhost:8080';
path = '/users';
}
@When('I send a valid create user payload')
public void createUser() {
response = given()


.auth()


.preemptive()


.basic('MY_USERNAME', 'MY_PASSWORD')


.header('Accept', ContentType.JSON.getAcceptHeader())


.contentType(ContentType.JSON)


.body(validRequest)


.post(path)


.then().extract().response();
}
@Then('response status code should be {int}')
public void checkResponseStatusCode(int code) {
Assertions.assertEquals(code, response.getStatusCode());
}
@And('create user response should be valid')
public void verifyResponse() {
String username = response.jsonPath().get('username');
String email = response.jsonPath().get('email');
String name = response.jsonPath().get('name');
String id = response.jsonPath().get('id');

Assertions.assertEquals('test-api-user', username);
Assertions.assertEquals('test-api-user@email.com', email);
Assertions.assertEquals('Test Api-User', name);
Assertions.assertNotNull(id);
} }

Sezvinoonekwa mudanho riri pamusoro tsanangudzo, pamutsetse wega wega muchiitiko chiri mufaira refaira, isu tine dudziro inoenderana.

Maitiro ne Given chirevo chinoseta izvo zvekutanga-kukumbira. Maitiro ne When chirevo chinoita chiito chekutumira chikumbiro uye pakupedzisira nzira ne Then chirevo chinoita zvirevo pamhinduro.

Kuita zviri pamusoro, zvese zvatinofanira kuita kuita rairo ./gradle cucumber mune terminal kubva mudzi wepurojekiti.

Kana bvunzo dzangomhanya, mhedzisiro yacho yakachengetwa mu build/test-results/functional.html.

Mhedziso

Mune ino posvo, takafukidza nhanho-nhanho gwara rekuti ungawedzera sei cucumber rukoko pamusoro pe REST-yakavimbiswa API bvunzo. Nekuita izvi, isu tinokwanisa kunyora mamiriro edu mumafaira emafaira ayo anova anoverengwa nevanhu vasiri vehunyanzvi.