简体   繁体   English

对于集成测试,如何验证模式/结构的JSON和XML? 任何语言

[英]For Integration Tests, How to Verify JSON and XML for schema/structure ? Any Language

Are there any common tricks, strategies, or tools to natively compare two JSON and/or XML strings for their schema/structure only? 是否有任何通用的技巧,策略或工具可以仅将本机比较两个JSON和/或XML字符串作为其模式/结构? I'm hoping to find a technique that fairly primitive and universal, but have not found one so far. 我希望找到一种相当原始和通用的技术,但到目前为止还没有找到。 I'm also looking for one that will simply compare a stored output versus a live output without having to write field-level validations. 我也在寻找一种可以简单比较存储的输出与实时输出而不必编写字段级验证的方法。

When performing the most basic integration tests against REST API's we don't control, we often want to verify that a response has certain characteristics (certain fields exist, of certain value types, etc). 在我们无法控制的REST API上执行最基本的集成测试时,我们经常要验证响应是否具有某些特征(存在某些字段,某些值类型等)。 The responses are almost never completely equivalent to a stored sample response, often times because of changed timestamps, or new objects in arrays and such. 响应几乎永远不会完全等同于存储的采样响应,通常是由于时间戳更改或数组中的新对象等等。 I have frequently come across the problem in both Java and .NET, but a solution on any platform would be helpful to see. 我在Java和.NET中都经常遇到这个问题,但是在任何平台上都可以找到解决方案。

We have some methods we've come up with, but they all feel very sub-optimal and have downsides. 我们提出了一些方法,但是它们都感觉不是很理想,并且有缺点。 For example: 1. Parsing the response to a generic JSON object and then performing validations that fields exist using "getProperties()", and validating the type of the value using "instanceof", etc. 2. Fully Deserializing to a domain class and essentially doing the same as above. 例如:1.解析对通用JSON对象的响应,然后使用“ getProperties()”执行字段存在的验证,并使用“ instanceof”等验证值的类型。2.完全反序列化为域类,然后基本上与上述相同。 3. Comparing the raw body as a string to a stored body from a text file, and using string techniques to "strip out" the values and spaces. 3.将原始主体作为字符串与文本文件中的存储主体进行比较,并使用字符串技术“去除”值和空格。

We use method 2) 我们使用方法2)

  1. Fully Deserializing to a domain class and essentially doing the same as above. 完全反序列化为域类,并且基本上与上述操作相同。

We have a try...catch wrapped around our strongly typed call to the Deserialize method that uses the JavaScriptSerializer . 我们有一个try...catch围绕我们的包裹strongly typed调用Deserialize使用方法的JavaScriptSerializer

The exception handled will determine the nature of the error encountered. 处理异常将确定遇到的错误的性质。

We do store/keep record of past & current tests and corresponding API versions and date-test-passed information 我们会存储/保留过去和当前测试以及相应的API版本和date-test-passed信息的记录

It is stored in the DB as a domain class-to-API version mapping catalogue... It stores 它作为域类到API版本的映射目录存储在数据库中。

  • our last tested,versioned domain class library details(dll, type), their corresponding API version, base & resource paths... 我们最近测试的版本化域类库详细信息(dll,类型),其相应的API版本,基础和资源路径...
  • as well as last-passed datetime... 以及最后通过的日期时间...

Our testing system picks that information, reflects on the current version of the dll(using its type) containing the domain classess corresponding to the known API and its version. 我们的测试系统会选择该信息,并反映dll的当前版本(使用其类型),该版本包含与已知API及其版本相对应的域类。

Using Dependency Injection, it makes it easy for us to even swap out and in previous DLLs to test if they work against a possibly new API. 使用依赖注入,它使我们更容易进行交换,也可以在以前的DLL中测试它们是否可以与可能的新API兼容。

That way we actually have dates & information to refer to in our cmmunication with the third-party when things break. 这样,当发生问题时,我们实际上可以在与第三方的通讯中参考日期和信息。

Integrations are always hard, so we also make sure we also have 'heartbeat' pings that do simple requests to the API, integrated to our alert system(SCOM) that notifies us of failures. 集成总是很困难,因此我们还要确保我们也有“心跳” ping,它们对API进行简单的请求,并集成到通知我们失败的警报系统(SCOM)中。

A less programmatic but simpler approach to ensure a 3rd party provider API responses still comply with what you expect from them is to use tools like Postman . 确保第三方供应商API响应仍然符合您期望的一种较不编程的方法,而是使用诸如Postman之类的工具。

Postman enables you to create a suite or collection of API calls with assertions on the responses, so the idea would be to collect the basic calls you want to make sure work in the provider and write the assertions for them, focusing on the shape and values you expect in the responses. Postman使您可以创建带有响应声明的API调用套件或集合,因此,其想法是收集要确保在提供程序中工作的基本调用,并为它们编写声明,着重于形状和值您期望在回复中。

With that collection of calls and assertions, you can run the suite on a regular basis either manually or within your builds using Newman (a command-line tool to run Postman collections), detecting bugs, unavailability and API regressions on your providers when they happen, without needing to monitor the provider's status pages or bug tracking tools. 通过该调用和断言集合,您可以手动或在构建中使用Newman (运行Postman集合的命令行工具)定期运行该套件,并在提供程序发生时检测错误,不可用和API退化,而无需监视提供商的状态页或错误跟踪工具。

Being Postman a 3rd party tool itself, it would mean that, out of the box, your tests won't be stored in the same repository as your service. 作为Postman的第三方工具,这意味着开箱即用的测试不会与服务存储在同一存储库中。 Additionally, you now depend on Postman's availability and robustness. 此外,您现在还取决于Postman的可用性和可靠性。 On the other hand, you would be using a tool specifically designed for this need, which makes unnecessary to write (and test, and maintain) specific code utils that validate the responses of the external provider. 另一方面,您将使用专门为此需求而设计的工具,该工具无需编写(测试和维护)用于验证外部提供程序响应的特定代码工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM