如何测试GraphQl API? [英] How to test a GraphQl API?

查看:327
本文介绍了如何测试GraphQl API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个功能测试套件(它将测试GraphQl API).测试套件将与API放在单独的存储库和容器中.

I need to write a functional test suite (that will test a GraphQl API). The test suite will be in a separate repo and container from the API.

我想到的一种方法是在测试套件中使用BDD框架.该套件将在收到HTTP请求后运行所有BDD测试.

One approach I thought of would be to use a BDD framework within the test suite. The suite would run all the BDD tests after receiving a HTTP request.

我正在考虑将Cucumber.js用作BDD框架.我知道有npm test.我不确定如何执行测试.以这种方式使用单元测试框架有点尴尬.这种方法有意义吗?

I was considering using Cucumber.js as the BDD framework. I know there is npm test. I am not sure how I will execute the tests. It feels a bit awkward to use a unit testing framework in this way. Does this approach make sense?

存在哪些工具可以执行此类操作?我愿意考虑各种语言和工具.

What tooling exists to do something like this? I am open to consider various languages and tools.

推荐答案

空手道是一个相对较新的由于2种特定功能,Web服务测试自动化框架恰好非常适合测试GraphQL响应

Karate is a relatively new web-services test-automation framework that happens to be well suited for testing GraphQL responses because of 2 specific capabilities

  • 文本操作:内联GraphQL查询,从(可重复使用的)文件和这是一个很好的例子: graphql.feature ,下面是一个摘要:

    Here is a good example: graphql.feature with a snippet below:

    # you can also read this query from a file
    Given text query =
    """
    {
      pokemon(name: "Pikachu") {
        id
        number
        name
        attacks {
          special {
            name
            type
            damage
          }
        }
      }
    }
    """
    And request { query: '#(query)' }
    When method post
    Then status 200
    
    # json-path makes it easy to focus only on the parts you are interested in
    # which is especially useful for graph-ql as responses tend to be heavily nested
    * match $.data.pokemon.number == '025'
    
    # the '..' wildcard is useful for traversing deeply nested parts of the json
    * def attacks = get[0] response..special
    * match attacks contains { name: 'Thunderbolt', type: 'Electric', damage: 55 }
    

    对于非Java团队,空手道提供了二进制可执行文件仅需要JRE,Visual Studio代码为足以作为IDE .由于空手道如何嵌入JavaScript运行时,JavaScript程序员会特别有宾至如归的感觉.支持宽大" JSON(无需双引号,无需将JSON键括在引号中).

    For non-Java teams, Karate provides a binary executable that only requires the JRE, and Visual Studio Code is sufficient as an IDE. JavaScript programmers will especially feel at home because of how Karate embeds a JavaScript runtime and supports 'lenient' JSON (no double-quotes needed, no need to enclose JSON keys in quotes).

    这是团队在生产中使用该文章的链接: https://www.codemotion.com/magazine/dev-hub/web-developer/graphql-testing-with-karate/

    here's a link to an article by a team using it in production: https://www.codemotion.com/magazine/dev-hub/web-developer/graphql-testing-with-karate/

    免责声明:此处为开发人员.

    Disclaimer: dev here.

    这篇关于如何测试GraphQl API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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