test graphql server - bqconf · graphql是个查询语,由 facebook开发,于替换 restful...

33
1 ©ThoughtWorks 2018 Commercial in Confidence GraphQL 测试实践

Upload: others

Post on 23-May-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

  • 1©ThoughtWorks 2018 Commercial in Confidence

    GraphQL 测试实践⻬齐磊磊

  • 什什么是 GraphQL?

    GraphQL是⼀一个查询语⾔言,由Facebook开发,⽤用于替换RESTful API。服务端可以⽤用任何的语⾔言实现。具体的你可以查看Facebook关于GraphQL的⽂文档和各种语⾔言的实现;

    查询语⾔言

    API查询语⾔言

  • 描述需求与功能:

    GraphQL给出了了正确的职责划分。

  • 早期web架构

  • 当今的应⽤用程序可能很复杂

    ●有针对不不同平台的多个前端

    ●数据可能存在于多个后端

    ●需要跨不不同语⾔言、技术和上下⽂文的团队协作

  • API兴起

  • GraphQL: API 的进化

  • GraphQL servers in any language

    var { makeExecutableSchema } = require('graphql-tools');

    var typeDefs = [`type Query { hello: String}

    schema { query: Query}`];

    var resolvers = { Query: { hello(root) { return 'world'; } }};

  • 总结下

    ● Query 代表的是查询的统称,也就是⼀一次查询可以称为⼀一个 Query,⽽而 query 是 Query 的⼀一个类型种类,当前有三种类型,分别是 query,mutation,subscription

    ● 四者关系简单讲是这样的,Schema 由 Type 来描述,Query 的查询语法和格式受 Schema 约束,⽽而 query,mutation,subscription 是 Query 的三种类型,分别对应不不同的业务场景

  • Rest vs graphql

  • Graphql 只需要在sever端定义好schema

    query MoviesAndActors { 
 movies { 
 title 
 image 
 actors { 
 image 
 name 
 } 
 } 
}

  • ⼿手动测试GraphQL

  • 打开chrome dev tools 查看graphql 请求

  • graphQL⾃自动化测试

  • 26©ThoughtWorks 2018 Commercial in Confidence

    单击添加简介、要点

    单击此处添加副标题。

    单击添加要点。

    利利⽤用 apollo client / nodejs/ mocha chai 等,可以通过编写代

    码的形式,来测

    试graphql server。

  • ©ThoughtWorks 2018 Commercial in Confidence 27

    karate

  • Karate 是什什么?

    Karate is the only open-source tool to combine API test-automation, mocks and performance-testing into a single, unifiedframework. The BDD syntax popularized by Cucumber is language-neutral, and easy for even non-programmers. Besides powerful JSON & XML assertions, you can run tests in parallel for speed - which is critical for HTTP API testing.

    https://github.com/intuit/karate/blob/master/karate-nettyhttps://github.com/intuit/karate/blob/master/karate-gatling

  • Feature: test graphql end point Background: * url 'http://localhost:4000/graphql' Scenario: simple graphql request # note the use of text instead of def since this is NOT json Given text query = """ { product(id: 1) { name id } \ } """ And request { query: '#(query)' } When method post Then status 200

    * print 'response:', response

  • 运⾏行行测试

    java -jar karate-0.9.4.jar src/test/java/demo/graphql/graphql.feature

  • ©ThoughtWorks 2018 Commercial in Confidence 32

    Q&A