【问题标题】:Karate tests pass or fail inconsistently空手道测试通过或失败不一致
【发布时间】: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


【解决方案1】:

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

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

当我使用 IntelliJ 中的测试运行器功能运行测试时,每次测试失败时,IntelliJ 都会在测试运行器窗口中记录失败的测试,但空手道会继续运行,重试并且测试通过。我现在可以在报告中看到这一点,但 IntelliJ 测试运行程序没有更新。

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

这里是一个本地测试的例子:

在 CI 中传递相同的提交:

我不确定这里有什么修复方法,如果有的话。如果 IntelliJ 知道这种行为会很好,或者空手道可能只能在处理完所有重试后报告测试结果 - 现在看起来空手道在处理第一次测试运行后立即报告。

这几天有点迷茫。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-06-18
  • 1970-01-01
  • 2020-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-15
  • 1970-01-01
相关资源
最近更新 更多