空手道测试不一致地通过或失败

Karate tests pass or fail inconsistently

我将空手道与 Gradle 一起使用,并且有 8 个功能文件可以测试我的 Spring 启动 API.

的读取/获取功能

我看到这些测试以一种感觉相当随机的方式失败。 失败以某种方式与授权有关,但我看不出有什么问题。

这是一个例子,

这失败了

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' }

objective 测试由于 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());
    }
}

有没有人遇到过类似的问题?

事实证明,这是 IntelliJ 中的意外行为。

所以空手道似乎有重试政策。如果测试失败,它将重试几次。

当我 运行 使用 IntelliJ 中的测试 运行ner 函数进行测试时,每次测试失败时,IntelliJ 都会在测试 运行ner window 中记录为它应该用于失败的测试,但空手道保持 运行ning,重试并且测试通过。我现在可以在报告中看到,但是 IntelliJ 测试 运行ner 没有更新。

这会导致令人困惑的情况,即测试通过,但在本地似乎失败,但测试通过 CI。

这里是本地测试的例子:

并且相同的提交传入 CI:

我不太确定这里有什么修复程序,如果有的话。如果 IntelliJ 意识到这种行为,那就太好了,或者 Karate 只能在处理完所有重试后报告测试结果 - 现在看起来 Karate 在处理第一个测试 运行 后立即报告.

这几天很混乱。