``如何为TeamCity中运行的JMeter编写JSON模式验证 [英] ​How can I write JSON schema validation for JMeter run in TeamCity

查看:98
本文介绍了``如何为TeamCity中运行的JMeter编写JSON模式验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JMeter进行回归API测试自动化. 我正在寻找一些基本的有机解决方案,以使用内置JMeter工具验证JSON模式. CI是使用 Team City 构建的,因此基本解决方案将会更快.

I'm working on regression API testing automation using JMeter. I'm searching for some basic organic solution to validate JSON schema using build-in JMeter tools. CI is built with Team City so a basic solution will be faster.

推荐答案

开箱即用JMeter不提供JSON Schema验证功能,但是您可以使用第三方库,例如

Out of the box JMeter doesn't provide JSON Schema validation functionality, however you can use a 3rd-party library like JSON Schema Validator to add this to JMeter

  1. 获取最新版本的 org.everit.json.schema.jar
  2. 获取相应版本的 Java中的JSON
  3. 获取相应版本的方便的URI模板
  4. 将上述.jar文件中的3拖放到JMeter安装的"lib"文件夹(或 JMeter类路径)
  5. JSR223声明添加为 JMeter范围规则)
  6. 将以下代码放入脚本"区域:

  1. Get the latest version of org.everit.json.schema.jar
  2. Get the appropriate version of JSON in Java
  3. Get the appropriate version of Handy URI Templates
  4. Drop 3 above .jar files to "lib" folder of your JMeter installation (or whatever place in JMeter Classpath)
  5. Add JSR223 Assertion as a child of the Sampler which returns the JSON which you need to check against the schema (or according to the JMeter Scoping Rules)
  6. Put the following code into "Script" area:

def schemaPath = '/path/to/your/schema.json'
def rawSchema = new org.json.JSONObject(new org.json.JSONTokener(org.apache.commons.io.FileUtils.readFileToString(new File(schemaPath), 'UTF-8')))
def schema = org.everit.json.schema.loader.SchemaLoader.load(rawSchema)
schema.validate(new JSONObject(prev.getResponseDataAsString()))

  • 就是这样,如果架构验证失败,则受影响的采样器将被标记为失败

  • That's it, if schema validation fails the affected Sampler(s) will be marked as failed

    这篇关于``如何为TeamCity中运行的JMeter编写JSON模式验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆