You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Credo输出Cucumber JSON/JUnit XML适配CircleCI?

Great question! Let's break this down into two clear parts: whether Credo can output JUnit XML/Cucumber JSON directly, and how to convert its standard JSON output if not.

Can Credo Output JUnit XML/Cucumber JSON Directly?

First, check Credo's built-in formatters by running mix credo --help. The default options include json, flycheck, codeclimate, and a few others, but there's no native support for JUnit XML or Cucumber JSON as of the latest version.

That said, you have two practical options here:

  • Look for community-maintained Credo formatters: Some packages exist that add JUnit/Cucumber output support. You can search Hex.pm for packages tagged with credo-formatter to find these.
  • Write a custom Credo formatter: Credo allows you to define custom formatters by implementing the Credo.Formatter behaviour. This gives you full control over the output format, but requires a bit of Elixir code to map Credo's issue data to your desired structure.
Converting Credo's Standard JSON to JUnit XML (For CircleCI)

Since CircleCI has first-class support for JUnit XML reports, converting Credo's JSON output to this format is the most straightforward path. Here's how to do it:

Step 1: Generate Credo's JSON Output

Keep using your existing command to generate the raw JSON:

mix credo --all --format=json > credo-output.json

Step 2: Write a Conversion Script

Create an Elixir script (e.g., convert_credo_to_junit.exs) to parse the JSON and generate JUnit XML. We'll use the xml_builder library to handle XML generation (add it as a dev dependency first).

First, add xml_builder to your mix.exs:

defp deps do
  [
    # ... your existing dependencies
    {:xml_builder, "~> 2.2", only: :dev}
  ]
end

Run mix deps.get to install it.

Then, the conversion script:

#!/usr/bin/env elixir

# Load required dependencies
Mix.install([:jason, :xml_builder])

# Read and parse Credo's JSON output
credo_data = File.read!("credo-output.json") |> Jason.decode!()

# Map Credo issues to JUnit test case structure
test_cases = Enum.map(credo_data["issues"], fn issue ->
  %{
    name: "#{issue["category"]}: #{issue["message"]}",
    file: issue["filename"],
    line: issue["line_no"],
    failure_details: %{
      message: issue["message"],
      severity: issue["severity"],
      content: "#{issue["check"]}\nFile: #{issue["filename"]}:#{issue["line_no"]}"
    }
  }
end)

# Build the full JUnit XML document
junit_xml = XmlBuilder.document(
  XmlBuilder.element(:testsuite, %{
    name: "Credo Code Quality Checks",
    tests: length(test_cases),
    failures: length(test_cases)
  },
    Enum.map(test_cases, fn tc ->
      XmlBuilder.element(:testcase, %{name: tc.name, file: tc.file, line: tc.line},
        XmlBuilder.element(:failure, %{message: tc.failure_details.message, type: tc.failure_details.severity}, tc.failure_details.content)
      )
    end)
  )
)

# Write the final XML file
File.write!("credo-junit.xml", XmlBuilder.generate(junit_xml, indent: 2))

Step 3: Update Your CircleCI Config

Modify your .circleci/config.yml to run the conversion script after generating the Credo JSON, then tell CircleCI where to find the JUnit report:

jobs:
  build:
    steps:
      # ... existing steps (checkout, install dependencies)
      - run: mix credo --all --format=json > credo-output.json
      - run: elixir convert_credo_to_junit.exs
      - store_test_results:
          path: ./credo-junit.xml
Converting to Cucumber JSON

If you specifically need Cucumber JSON, the approach is similar: parse Credo's JSON output and map its fields to the Cucumber JSON structure (which includes features, elements, steps, etc.). You'd adjust the conversion script to output the correct JSON format instead of XML, mapping each Credo issue to a Cucumber step result.


内容的提问来源于stack exchange,提问作者anonymous-dev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 09:05:33