Introduced
In this article, we will introduce the open source Web-api automated Testing Framework--karate
Karate was built on the basis of another BDD test framework cucumber, and shared some of the same ideas. One of these is the use of the Gherkin file, which describes the features being tested
Unlike cucumber, test cases do not need to be written in Java and are fully described in the Gherkin file
With karate, you can write test scripts of any type of Web service side and check if the response is as expected
Karate's validation engine provides a flexible comparison of two JSON or XML file contents, unaffected by whitespace and data order
For more detailed information about karate, please refer to the Karate official introduction
Characteristics
- Built on the basis of CUCUMBER-JVM
- Can run tests like standard Java projects and generate reports
- The development of test code does not require any knowledge of Java
Test code is easy to write, even for non-programmers
Environmental requirements
- JDK1.8 and above
- Maven
- Idea
Use to create a project
- Open idea,file| new| Project
- Select Maven Project and click Next
- Enter MAVEN basic information and click Next
Enter project name and storage path, click Finish
Add dependency to use Karate in a MAVEN project, you need to add Karate-apache dependencies to Pom.xml, and if you implement JUnit testing you also need to add KARATE-JUNIT4 dependencies
<dependencies> <dependency> <groupId>com.intuit.karate</groupId> <artifactId>karate-apache</artifactId> <version>0.8.0</version> <scope>test</scope> </dependency> <dependency> <groupId>com.intuit.karate</groupId> <artifactId>karate-junit4</artifactId> <version>0.8.0</version> <scope>test</scope> </dependency></dependencies>
Set the test resource file directory, it is recommended that test case files and Java files in the same directory, when encountering a large project convenient management, do not have to switch between folder Src/test/java and Src/test/resources folder, you can Pom.xml
<testResources> <testResource> <directory>src/test/java</directory> <excludes> <exclude>**/*.java</exclude> </excludes> </testResource></testResources>
Service-side simulation
To demonstrate the rest API, we use the Wiremock server
Adding a mock service dependency configuration in Pom.xml
<dependency> <groupId>com.github.tomakehurst</groupId> <artifactId>wiremock-standalone</artifactId> <version>2.18.0</version> <scope>test</scope></dependency>
Write a class that starts a service
Package Server;import Com.github.tomakehurst.wiremock.wiremockserver;import Static com.github.tomakehurst.wiremock.client.wiremock.*;p Ublic class StartServer {private static Wiremockserver Wiremockserver = new Wiremockserver (8080); public static void StartServer () {Wiremockserver.start (); Stubfor (Get (Urlequalto ("/user/get")). Willreturn (Aresponse () . Withstatus. Withheader ("Content-type", "Application/json") . withbody ("{\" id\ ": \" 1234\ ", Name: \" John smith\ "}"))); Stubfor (Post (Urlequalto ("/user/create")). Withheader ("Content-type", Equalto ("appli Cation/json ")). Withrequestbody (containing (" id ")). Willreturn (Aresponse () . Withstatus. Withheader ("Content-type", "Application/jsoN "). Withbody (" {\ "id\": \ "1234\", Name: \ "John smith\"} ")); public static void Main (String ... args) {startserver (); }}
Use case File Authoring
A use case file is saved with a ". Feature" extension.
The file begins with the feature keyword, followed by the name of the feature being tested on the same line
A use case file contains different test scenarios, each of which begins with the keyword scenario and contains multiple steps. These steps contain keywords Given,when,then,and and But
For more information about the cucumber and gherkin structures, click here
Feature: Learn How to use Karate for testing. Scenario: Testing valid GET endpoint Given url 'http://localhost:8080/user/get' When method GET Then status 200 Scenario: Testing the exact response of a GET endpoint Given url 'http://localhost:8080/user/get' When method GET Then status 200 And match $ == {id:"1234", name:"John Smith"} Scenario: Testing that GET response contains specific field Given url 'http://localhost:8080/user/get' When method GET Then status 200 And match $ contains {id:"1234"}
Runner class writing
Recommended in the use case file sibling directory
We can run our tests by integrating Karate with JUnit
We will use @cucumberoptions annotations to specify the exact location of the feature file
package demo;import com.intuit.karate.junit4.Karate;import cucumber.api.CucumberOptions;import org.junit.runner.RunWith;@RunWith(Karate.class)@CucumberOptions(features = "classpath:demo/demo.feature")public class DemoRunner {}
Run a use case
Start the service first
Right-click the StartServer class and select Run Startserver.main () to start the service
Run a use case
Right-click the Demorunner class and select Run Demorunner to test
View ReportsIn the project's Target/surfire-reports directory there is a test-demo.demo.html file, the browser opens to see the results
Continuous integration
Automated testing can be done with Jenkins, and Jenkins provides plug-ins cucumber-reports to display highly readable automated test reports
Need to modify runner inheritance Karaterunner, first introduce karate-testng dependency
<dependency> <groupId>com.intuit.karate</groupId> <artifactId>karate-testng</artifactId> <version>0.8.0</version></dependency>
Modify Demorunner, note configuration cucumberoptions, to produce a JSON-formatted report, Cucumber-reports plugin will parse the file and generate a report
package demo;import com.intuit.karate.junit4.Karate;import com.intuit.karate.testng.KarateRunner;import cucumber.api.CucumberOptions;import org.junit.runner.RunWith;@CucumberOptions(features = "classpath:demo/demo.feature",format={"pretty","html:reports","json:report.json"})public class DemoRunner extends KarateRunner {}
Please refer to network resources for cucumber-reports configuration in Jenkins
Jenkins Configuration command line run instructions
RM-RF ${workspace}/report.json
Cd/home/pateo/ideaprojects/demo4karate
MVN Test-dtest=demorunner
CP Report.json ${workspace}/report.json
Jenkins Report Show
Code Reference Address: