簡體   English   中英

如何將使用 testng 的放心測試更改為 cucumber?

[英]How can I change my restassured test using testng into cucumber?

我一直在嘗試使用 testng 將我放心的測試轉移到 cucumber 並且我幾乎破解了它,但是斷言讓我有點失望。 我遇到的這個問題是我斷言中的主體拋出了這個錯誤。

java: cannot find symbol
  symbol:   method body(java.lang.String,org.hamcrest.Matcher<java.lang.String>)
  location: class stepdefs.articlesCucumberTests

這是我目前的工作測試。

import static io.restassured.RestAssured.*;
import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;
import static org.hamcrest.Matchers.*;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.path.json.JsonPath;
import org.testng.annotations.Test;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("unchecked")
public class articlesTestNg extends enablers {

    //Checks articles individual ID's.
    @Test(dataProvider = "getId")
    public void TestAllArticles(String articlesId) {

        given().
                given().log().all().and().
                pathParam("id", articlesId).
                when().
                get(singleArticles).
                then().
                assertThat().
                statusCode(200).
                body(("id"), equalTo(articlesId));
    }

    //Checks the whole content of the json file using an expected outcome in the project structure.
    @Test
    public void GetArticlesJson() {
        JsonPath expectedJson = new JsonPath(new File(pathJson));

        given().
                when().
                get(allArticles).
                then().
                assertThat().
                statusCode(200).
                and().
                body("", hasItem(expectedJson.getMap("")));
    }

這是我迄今為止在黃瓜測試中的嘗試,我將收到一篇正文拋出錯誤的個人文章。 有一個更好的方法嗎?

package stepdefs;

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.response.Response;
import org.junit.jupiter.api.Assertions;


import org.testng.Assert;
import org.testng.annotations.Test;
import skySportsPages.enablers;


import static io.restassured.RestAssured.baseURI;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;


public class articlesCucumberTests extends enablers {

    private Response response;

    @Test
    @Given("I have a set of articles containing individual ID's")
    public void i_have_a_set_of_articles_containing_individual_id_s() {
        RestAssured.baseURI = singleArticles;
        System.out.println(baseURI);
    }

    @Test(dataProvider = "getId")
    @When("I get an the article ID")
    public void i_get_an_the_article_id(String articlesId) {
        given().
                given().log().all().and().
                pathParam("id", articlesId).
                when().
                get(singleArticles);

    }
    @Test
    @Then("I will receive an individual article")
    public void i_will_receive_an_individual_article(String articlesId) {
        Assert.assertEquals(body(("id"), equalTo(articlesId)));

    }

    @Test
    @Given("I have a set of articles")
    public void i_have_a_set_of_articles(String articlesId) {

    }

    @Test
    @When("I get the list of articles")
    public void i_get_the_list_of_articles() {

    }
    @Test
    @Then("It will match the json file")
    public void it_will_match_the_json_file() {

    }

}

不,我認為我們不在 cucumber 步驟中使用 @Test 注釋。 您應該在toolqa上查看更多教程。

錯誤在這里是因為Assert.assertEquals應該有兩個參數,在您的情況下,您只有一個body(("id"), equalTo(articlesId))可能返回 boolean,所以首先檢查您剛剛執行的操作:

@Test
@Then("I will receive an individual article")
public void i_will_receive_an_individual_article(String articlesId) {
   body(("id"), equalTo(articlesId));

}

然后嘗試更改為:

  Assert.assertTrue(body(("id"), equalTo(articlesId)));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM