介绍
在这篇文章中,我们将介绍一下开源的Web-API自动化测试框架——Karate
Karate是基于另一个BDD测试框架Cucumber来建立的,并且共用了一些相同的思想。其中之一就是使用Gherkin文件,该文件描述了被测试的功能
与Cucumber不同的是测试用例不需要用Java编写,并且被完整的描述在Gherkin文件中
通过Karate,您可以编写任何类型的Web服务端的测试脚本,并检查响应是否符合预期
Karate的验证引擎可以灵活的比较两个JSON或XML文件内容,不受空格和数据顺序的影响
有关Karate的更详细的内容,请参考Karate官方介绍
特点
建立在Cucumber-JVM基础上
可以像标准的Java工程一样运行测试并且产生报告
测试代码的开发不需要掌握任何的Java知识
即使对非编程人员,测试代码也很容易编写
环境需求
JDK1.8及以上
Maven
IDEA
使用
创建工程
打开IDEA,File|New|Project
新建工程
选择Maven工程,点击Next
选择Maven
输入Maven基本信息,点击Next
输入基本信息
输入工程名称和存放路径,点击Finish
工程目录结构
添加依赖
要在Maven项目中使用Karate,需要将karate-apache依赖项添加到pom.xml,如果实现JUnit测试还需要添加karate-junit4依赖
com.intuit.karate
karate-apache
0.8.0
test
com.intuit.karate
karate-junit4
0.8.0
test
设置测试资源文件目录,建议测试用例文件和java文件放在同一个目录下,遇到庞大的工程的时候方便管理,不必在文件夹src/test/java和src/test/resources文件夹之间切换,可以在pom.xml的
src/test/java
**/*.java
服务端模拟
为了演示REST API,我们使用WireMock服务器
在pom.xml中添加mock服务依赖配置
com.github.tomakehurst
wiremock-standalone
2.18.0
test
编写一个启动服务的类
package server;
import com.github.tomakehurst.wiremock.WireMockServer;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
public class StartServer {
private static WireMockServer wireMockServer = new WireMockServer(8080);
public static void startServer(){
wireMockServer.start();
stubFor(
get(urlEqualTo("/user/get"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody("{ \"id\": \"1234\", name: \"John Smith\" }")));
stubFor(
post(urlEqualTo("/user/create"))
.withHeader("content-type", equalTo("application/json"))
.withRequestBody(containing("id"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody("{ \"id\": \"1234\", name: \"John Smith\" }")));
}
public static void main(String... args){
startServer();
}
}
用例文件编写
一个用例文件以“ .feature”扩展名保存。
文件以Feature关键字开头,在同一行跟着所测试的功能名称
一个用例文件包含不同的测试场景,每个场景都以关键字Scenario开头,并且包含多个步骤。这些步骤包含关键字Given,When,Then,And和But
有关Cucumber和Gherkin结构的更多信息,请点击此处
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类编写
建议放在用例文件同级目录下
我们可以通过将Karate与JUnit集成来运行我们的测试
我们将使用@CucumberOptions注解指定Feature文件的具体位置
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 {
}
运行用例
先启动服务
右击StartServer类选择Run StartServer.main()启动服务
运行用例
右击DemoRunner类选择Run DemoRunner运行测试
用例运行结果
查看报告
在项目的target/surfire-reports目录下有TEST-demo.demo.html文件,浏览器中打开即可看到结果
报告查看
持续集成
可以借助于jenkins完成自动化测试并且jenkins提供插件cucumber-reports可以展示可读性强的自动化测试报告
需要修改Runner继承KarateRunner,先引入Karate-testng依赖
com.intuit.karate
karate-testng
0.8.0
修改DemoRunner,注意配置CucumberOptions,要产生json格式的报告,cucumber-reports插件会去解析该文件并生成报告
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 {
}
jenkins中cucumber-reports配置请参考网络资源
jenkins配置命令行运行指令
rm -rf ${WORKSPACE}/report.json
cd /home/pateo/IdeaProjects/demo4karate
mvn test -Dtest=DemoRunner
cp report.json ${WORKSPACE}/report.json
jenkins报告展示
标签: 自动化测试
好文要顶 关注我 收藏该文
萌秀才
关注 - 5
粉丝 - 8
+加关注
0 0
« 上一篇:uiautomator+cucumber实现移动app自动化测试
posted @ 2018-09-03 17:20 萌秀才 阅读(178) 评论(0) 编辑 收藏
刷新评论刷新页面返回顶部
注册用户登录后才能发表评论,请 登录 或 注册,访问网站首页。
【推荐】超50万VC++源码: 大型组态工控、电力仿真CAD与GIS源码库!
【推荐】企业SaaS应用开发实战,快速构建企业运营/运维系统
【推荐】ActiveReports 报表控件,全面满足 .NET开发需求
腾讯云0831
最新IT新闻:
· 正大天晴与阿里云合作,AI制药提高化合物筛选准确率20%
· 快播正式破产 王欣:放下过天地 却从未放下过你
· 美国家庭去越南度假 飞行模式下半小时产生近10万元天价漫游费
· 华为英特尔联合发布FusionCube关键业务数据库解决方案
· 阿里巴巴联手新加坡培养AI博士:帮科学从实验室迈进现实
» 更多新闻...
华为CH0822
最新知识库文章:
· 如何招到一个靠谱的程序员
· 一个故事看懂“区块链”
· 被踢出去的用户
· 成为一个有目标的学习者
· 历史转折中的“杭派工程师”
» 更多知识库文章...
公告
昵称:萌秀才
园龄:4年9个月
粉丝:8
关注:5
+加关注
< 2018年9月 >
日 一 二 三 四 五 六
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 1 2 3 4 5 6
我的标签
自动化测试(12)
随笔档案
2018年9月 (1)
2017年4月 (1)
2016年5月 (1)
2016年4月 (1)
2014年1月 (1)
2013年12月 (1)
2013年11月 (7)
阅读排行榜
1. Selenium WebDriver + Grid2 + RSpec之旅(三) ----入门小例子(5484)
2. uiautomator+cucumber实现移动app自动化测试(1462)
3. yml文件数据的简洁表达方法(Hashes to OpenStruct)(804)
4. calabash-android Win10 入门笔记(495)
5. Selenium WebDriver + Grid2 + RSpec之旅(六) ----多浏览器的并行执行(482)
推荐排行榜
1. uiautomator+cucumber实现移动app自动化测试https://www.cnblogs.com/ouguangqian/p/karate_learn.html