{"id":23569297,"url":"https://github.com/fxbin/api-test","last_synced_at":"2025-05-16T08:34:17.870Z","repository":{"id":37344836,"uuid":"226028155","full_name":"fxbin/api-test","owner":"fxbin","description":"REST Assured、Groovy、Spock、Velocity 接口自动化测试学习案例, WireMock Demo","archived":false,"fork":false,"pushed_at":"2022-12-14T20:43:13.000Z","size":41,"stargazers_count":1,"open_issues_count":4,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-17T20:17:16.386Z","etag":null,"topics":["rest-assured","spock-groovy","spock-reports","velocity"],"latest_commit_sha":null,"homepage":"","language":"Groovy","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fxbin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-12-05T06:03:59.000Z","updated_at":"2023-12-22T16:58:52.000Z","dependencies_parsed_at":"2023-01-29T01:00:29.400Z","dependency_job_id":null,"html_url":"https://github.com/fxbin/api-test","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fxbin%2Fapi-test","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fxbin%2Fapi-test/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fxbin%2Fapi-test/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fxbin%2Fapi-test/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fxbin","download_url":"https://codeload.github.com/fxbin/api-test/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254496209,"owners_count":22080669,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["rest-assured","spock-groovy","spock-reports","velocity"],"created_at":"2024-12-26T19:13:48.634Z","updated_at":"2025-05-16T08:34:17.851Z","avatar_url":"https://github.com/fxbin.png","language":"Groovy","funding_links":[],"categories":[],"sub_categories":[],"readme":"\nGitChat \u003c接口自动化测试实战\u003e 专栏学习\n\n| 框架或语言 | 优势 |\n| - | - |\n| REST Assured | 采用 REST Assured 完成接口调用简单易上手； Github 上 Star 数已达到4096 |\n| Groovy | 使用 Groovy 语言让数据库增删改查，各类文件（ JSON 、TXT、 CSV 、yaml）的读取变得极其简单 |\n| Spock\tGroovy | 语言自带的 BDD 框架，让自动化 case 更加清晰，易于阅读 |\n| Velocity | 采用模板引擎工具，让接口的 Request Body 维护成本降到最低 |\n\n\n## WireMock\n\n[http://wiremock.org/docs/](http://wiremock.org/docs/)\n\n### 模拟 GET 请求接口\n\n* 如果模拟的接口无查询参数，那么使用 urlPattern 即可。\n* 如果模拟的接口有查询参数，但对查询参数的名字和个数无任何限制，那么使用 urlPathPattern 即可。\n* 如果模拟的接口有查询参数，且对查询参数的名称和值都有限制，那么需要在 mapping 文件中添加 queryParameters 进行控制。\n\n### 模拟 JSON 格式接口\n\n[http://wiremock.org/docs/verifying/](http://wiremock.org/docs/verifying/)\n\n### 模拟 XML 格式接口\n\n* 如果对接口的 Body 体内容有严格要求（字段名称，字段值都有限制）那么可以采用equalToXml方式定义 Request Body 。\n* 如果只对接口 Body 体局部字段值有控制，那么可以采用matchesXpath的方式进行定义。\n* 如果对 Request Body 无任何要求，那么不配置bodyPatterns，只需把 Header 中content-Type设置为application/xml 即可。\n\n## Rest Assured \n\n[Rest Assured官网](http://rest-assured.io/)\n\n针对接口调用的调试，REST Assured 提供了很简单的调试方式，\n* given()后面加入.log().all()即可打印 Request 的详细信息\n* then()后面加入.log().all()即可打印 Response 的详细信息\n\n[rest-assured/wiki](https://github.com/rest-assured/rest-assured/wiki/Usage)\n\n## Spock \n\n[Spock官网](http://spockframework.org/)\n\nSpock是一个BDD框架，每一个case以def开头，\n* def这里可以添加该case所覆盖的业务场景描述信息，\n* case内容上支持given-when-then三段式。\n* 为了使用spock框架所有测试Class都需要继承Specification\n\n## Groovy 基础知识\n\n### Groovy 语法\n\n* 1、代码结尾处无需使用\";\"\n* 2、可以不用显示定义数据类型，所有数据类型都可以用 def 定义\n* 3、方法返回值前无需添加 return 关键字，如果方法不用 void 修饰，方法内的最后一行返回值即函数的返回值\n* 4、可以指定方法中参数默认值，方法中的参数可以不指定数据类型\n* 5、所有方法默认都是 public，无需添加 public 关键字\n* 6、方法的()可以取消\n* 7、Gstring：字符串中支持变量解析和换行处理\n* 8、任何对象都可以被强制转换为布尔值，任何为 null、void 的对象，等同于 0 或空值都会解析为 false，反之则为 true\n* 9、Groovy 中的“ == ”是 Java 中的 equal，如果需要判断两个对象值是否相等使用“==”\n\n### Groovy 闭包\n\n\u003e Groovy 官方对闭包的定义是“闭包是一个匿名代码块，可接受参数，返回值并可分配给变量”，\n闭包使用{},{clouserParameter-\u003estatement}，clouserParameter指闭包接受的参数，\n多个参数用逗号隔开，statement指闭包中的代码。\n\n### Groovy 集合处理\n\ncn.fxbin.groovy.CollectionTest\n\n### Groovy 操作数据库\n\ncn.fxbin.groovy.DataSource\ncn.fxbin.groovy.DataSourceNew\ncn.fxbin.groovy.ConstantSql\ncn.fxbin.groovy.DataRepository\n\n### Groovy 文件操作\n\ncn.fxbin.groovy.FileService\n\n### Groovy 脚本文件\n\ncn.fxbin.apitest.*\n\n## Schema 文件定义详解\n\n* properties 中 type 总共包含六种类型（integer、string、number、object、array、boolean），number 和 integer 的区别是 integer 只可以匹配整数类型\n* type 为 number 的数据，可以通过 maxinum，minimum 指定数据取值范围\n* type 为 string 的数据，可以通过 maxLength，minLength 指定字符串长度范围，通过pattern正则表达式定义该field值\n* type为array的数据，可以通过 minItems,maxItems 指定数组长度，uniqueItems是个布尔值，true表示数组值之间不可重复\n* type为object的数据，可以通过 maxProperties，minProperties 指定该object 包含的属性个数范围，required 中指定 object 的哪些属性是必须的，properties 里面指定每个属性的类型，长度范围等 根据上面的规则，我们重新定义 getResume 这个接口的 Response Schema，假设接口的 response 中每个字段有如下规则\n* name feild：type 是 string，假设长度范围是1-100的字符，必须是大写字母\n* age feild：type 是 number，假设长度范围是20-50\n* birthPlace field：type 是 object，包含四个属性country，city，state，street都是必填项\n* contacts filed：type 是 array，假设数组长度范围是 1-3，每个 item 又是个object，object 中的 phone，address 都是必填字段 按照上面的假设，定义的 schema 如下，大家在定义 schema 的时候可以参考到getResume 的 Response Body 一起看","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffxbin%2Fapi-test","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffxbin%2Fapi-test","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffxbin%2Fapi-test/lists"}