zl程序教程

您现在的位置是:首页 >  后端

当前栏目

REST Assured API Automation Testing Ⅰ - Getting Started

API rest Started Getting Testing Automation
2023-06-13 09:11:07 时间

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第24天,点击查看活动详情

Section 1: What is Rest Assured?

Rest Assured is a Java Library that can testing Apis, also called RESTful Apis. You can write test cases using the given, when, then which human are readable and can be easily understood.

As introduced on the official website REST Assured brings the simplicity of using dynamic languages into the Java domain. Rest Assured uses groovy under the hood. Rest Assured can used along with unit testing framework like Junit, TestNG and also with the Cucumber BDD which BDD stand for Behavioral Driver Development.

You can find more documents on the Rest Assured homepage.

Section 2: Setup for Rest Assured Project

Below is the list of tools or libraries we gonna need to creating a Rest Assured project.

  • Java JDK - 8
  • TestNG
  • IntelliJ
  • Maven

We gonna use JDK - 8 as the default version of Java and the TestNG for the Unit Testing framework, IntelliJ as the IDE and build the project by Maven 3.6.3.

Launch the IntelliJ and click Create New Project

Choose maven to build the Project and click Next button

Then fill the project name and GoupId, ArtifactId, then click Finish button

pom.xml is the place where we add dependencies, first we created dependencies tag, then add Rest Assured and TestNG dependencies, you check the offical doucment Getting Stated page or search from the maven central repository. Below are the REST Assured and TestNG dependencies.

<dependencies>
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>5.1.1</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.6.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

REST Assured includes JsonPath and XmlPath as transitive dependencies, so we don't have to add those again, and you can check the libaries form External Dependices that automatically download by IntelliJ.

Create new package under test package named com.restassured, create a java class named QuickstartTest then create a Java class testAlpha and add TestNG annotation @Test.

public class QuickstartTest {

    @Test
    public void testAlpha(){

    }
}

We need to add the import, but as static import. Importing these rest assured classes as static imports has some advantages even though it's generally not recommendedto import classes as static imports, then we can using the given when then statement

import static io.restassured.RestAssured.*;
import static io.restassured.matcher.RestAssuredMatchers.*;
import static org.hamcrest.Matchers.*;

public class QuickstartTest {

    @Test
    public void testAlpha(){
        given().
                when().
                then();
    }
}

then Run the testAlpha method, we got the test reslut on the console bottom of the IntelliJ

Why use static imports

with static import, we don't have to use the class name along with the middle name. The given method class name is RestAssured, we are using static keyword for importing the classes