空手道 - 测试不同的环境 - 有或没有 API 网关
Karate - Testing different environments - with or without API Gateway
我正在使用两种不同的环境进行测试 - Dev env 没有身份验证(因此我们不必通过 headers)并且 QA env 托管在 API Gateway 上。我试图设计我的测试,以便在任何一种环境中都可以将测试配置为 运行。
这是我所做的:
在 Karate-config.js 中定义客户端密码和客户端 ID。将这两个变量设置为 null for dev.
调用 user-login 场景文件生成授权令牌(从场景文件中获取 url、client_id 和 client_secret)和 return 脚本文件的令牌。
特点:使用空手道进行测试
Background:
* configure ssl = true
* url baseUrl
* def token = call read('classpath:endpoints/user-login.feature')
* def headerData = {Authorization: #(token.nextGen),Accept: 'application/json;v=1'}
* headers headerData
Scenario: Verify that status for retreiving endpoint
Given path 'abc'
When method get
Then status 200
功能:获取令牌
Scenario: Get authorization header
Given url 'https://api-it.cloud.xyz.com/oauth2/token?client_id=12121&client_secret=12121&grant_type=client_credentials'
When method get
Then status 200
And def tokenType = response.token_type
And def accessToken = response.access_token
* def nextGen = tokenType + ' '+ accessToken
* print nextGen
任何关于如何 运行 在同一项目上使用和不使用身份验证进行测试的指示将不胜感激。
请查看文档的这一部分:https://github.com/intuit/karate#conditional-logic
请注意,eval
从 0.7.0 开始,但您可以轻松使用 JS 函数,在 in 中设置 if
条件并执行您需要的操作 - 通过创建 karate.call()
从 JS 函数中设置 headers (或不)。
我正在使用两种不同的环境进行测试 - Dev env 没有身份验证(因此我们不必通过 headers)并且 QA env 托管在 API Gateway 上。我试图设计我的测试,以便在任何一种环境中都可以将测试配置为 运行。
这是我所做的:
在 Karate-config.js 中定义客户端密码和客户端 ID。将这两个变量设置为 null for dev.
调用 user-login 场景文件生成授权令牌(从场景文件中获取 url、client_id 和 client_secret)和 return 脚本文件的令牌。
特点:使用空手道进行测试
Background: * configure ssl = true * url baseUrl * def token = call read('classpath:endpoints/user-login.feature') * def headerData = {Authorization: #(token.nextGen),Accept: 'application/json;v=1'} * headers headerData Scenario: Verify that status for retreiving endpoint Given path 'abc' When method get Then status 200
功能:获取令牌
Scenario: Get authorization header Given url 'https://api-it.cloud.xyz.com/oauth2/token?client_id=12121&client_secret=12121&grant_type=client_credentials' When method get Then status 200 And def tokenType = response.token_type And def accessToken = response.access_token * def nextGen = tokenType + ' '+ accessToken * print nextGen
任何关于如何 运行 在同一项目上使用和不使用身份验证进行测试的指示将不胜感激。
请查看文档的这一部分:https://github.com/intuit/karate#conditional-logic
请注意,eval
从 0.7.0 开始,但您可以轻松使用 JS 函数,在 in 中设置 if
条件并执行您需要的操作 - 通过创建 karate.call()
从 JS 函数中设置 headers (或不)。