REST Assured API Automation Testing Ⅰ - Getting Started

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 image.png

Choose maven to build the Project and click Next button

image.png

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

image.png

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

image.png

猜你喜欢

转载自juejin.im/post/7112834800032940040