【发布时间】:2020-06-02 20:52:04
【问题描述】:
我在 Gradle 中使用空手道,并且有 8 个功能文件来测试我的 Spring Boot API 的读取/GET 功能。
我看到这些测试以一种非常随机的方式失败。 失败与授权有关,但我看不出有什么问题。
这是一个例子,
失败了
Feature: Get Objectives
Background:
* url baseUrl
* def objectiveEndpoint = '/objectives'
* header Authorization = token
Scenario: Get an Objective that exists
Given path objectiveEndpoint + '/37376564-3139-6232-2d66-6631392d3466'
When method GET
Then status 200
And match response contains { objectiveId: '37376564-3139-6232-2d66-6631392d3466' }
这就过去了
Feature: Get Assessments
Background:
* url baseUrl
* def assessmentEndpoint = '/assessments'
* header Authorization = token
Scenario: Get an assessment that exists
Given path assessmentEndpoint + '/2900b695-d344-4bec-b25d-524f6b22a93a'
When method GET
Then status 200
And match response contains { odiAssessmentId: '2900b695-d344-4bec-b25d-524f6b22a93a' }
由于 401 导致客观测试失败,并显示以下消息:
com.intuit.karate.exception.KarateException: objectives-read.feature:12 - status code was: 401, expected: 200, response time: 74, url: http://localhost:8080/objectives/37376564-3139-6232-2d66-6631392d3466, response: {"path":"/objectives/37376564-3139-6232-2d66-6631392d3466","error":"Unauthorized","message":"Unauthorized","timestamp":"2020-06-02T08:04:57.231+0000","status":401}
评估测试通过。
我通过运行授权功能获取令牌,并将结果存储到令牌变量中。
身份验证功能是:
Feature: Log In
Background:
* url 'https://$URL/oauth/token'
* def localSecret = secret
* def localClientId = clientId
Scenario: Get token
Given url 'https://$URL/oauth/token'
And form field grant_type = 'client_credentials'
And form field client_id = localClientId
And form field client_secret = localSecret
And form field audience = 'http://localhost:8080'
When method post
Then status 200
And match response contains { access_token: '#string' }
And def access_token = $.access_token
然后我将令牌添加到配置中,并将其传递给每个测试,如下所示:
var result = karate.callSingle('classpath:login.feature', config);
config.token = 'Bearer ' + result.access_token;
我已经确认上述两个功能中使用的令牌是有效的。我已经使用失败测试的输出中打印的令牌手动测试了我的 API,当我在 Postman 中重新创建它们时,上述两个测试都可以正常工作。这对我的 API 来说不是问题,因为如果我重新运行测试套件,失败的测试会有所不同,并且在我的 CI 上,我有一个包含这些测试的绿色构建。
我在像这样单独运行测试套件时都遇到了这个问题:
@Karate.Test
Karate testAssessments() {
return Karate.run().relativeTo(AssessmentsRunner.class);
}
当像这样并行运行我的所有测试时:
public class SpecTestParallel {
public static void generateReport(String karateOutputPath) {
Collection<File> jsonFiles = FileUtils.listFiles(new File(karateOutputPath), new String[]{"json"}, true);
List<String> jsonPaths = new ArrayList(jsonFiles.size());
jsonFiles.forEach(file -> jsonPaths.add(file.getAbsolutePath()));
Configuration config = new Configuration(new File("target"), "Test API");
ReportBuilder reportBuilder = new ReportBuilder(jsonPaths, config);
reportBuilder.generateReports();
}
@Test
void testParallel() {
Results results = Runner.path("classpath:specTests").tags("~@ignore").parallel(5);
generateReport(results.getReportDir());
assertEquals(0, results.getFailCount(), results.getErrorMessages());
}
}
以前有没有人遇到过类似的问题?
【问题讨论】:
-
不,一定是你做错了什么。也许您正在使用此处未显示的 Java 代码,它不是线程安全的。任何人都无法通过查看此代码来解决任何问题(或帮助我们缩小错误修复范围),请遵循以下流程:github.com/intuit/karate/wiki/How-to-Submit-an-Issue
标签: java integration-testing karate