DEV Community

Cover image for Junit Badge For Git Project
vishalmysore
vishalmysore

Posted on

Junit Badge For Git Project

What are badges?

They look like this Image description

Now lets create a custom badge for your git project

The Plugins to update the JUnit test number seem not working very well specially if you do not have JaCOCO , here is a simple work around

1) Create a GIST file

https://docs.github.com/en/get-started/writing-on-github/editing-and-sharing-content-with-gists/creating-gists

2)Get your GIST ID
https://docs.github.com/en/rest/gists/gists?apiVersion=2022-11-28

3)Create GIT TOken with GIST permission
https://docs.github.com/en/enterprise-server@3.9/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens

4)Add The token to your repo secret for action

5)Create shell script



#!/bin/bash

# Assuming Maven generates XML test reports in the "target/surefire-reports" directory
TEST_REPORT_DIR="target/surefire-reports"
# Print current directory
echo "Current directory: $(pwd)"

xml_files=$(find "$(pwd)" -name "TEST-*.xml")

# Concatenate all XML files and count the occurrences of "<testcase" using grep
test_count=$(cat $xml_files | grep -c "<testcase")
echo "Total number of tests: $test_count"




echo "GIST_ID: $GIST_ID"


json_data='{"schemaVersion": "1", "label": "testcount", "message": "'$test_count'", "color": "orange"}'

echo "JSON data: $json_data"

# Prevent any output from the script
exec > /dev/null
exec 2>&1

# Create or update the Gist with the test count
curl -s -X PATCH \
     -H "Authorization: token $GIST_TOKEN" \
     -H "Content-Type: application/vnd.github+json" \
     -d '{"files":{"test.json":{"content": "{\"schemaVersion\": 1,\"label\": \"testcount\", \"message\": \"'$test_count'\", \"color\":\"orange\"}" }}}' \
     "https://api.github.com/gists/$GIST_ID"


Enter fullscreen mode Exit fullscreen mode

7)Add script to your workflow



      - name: Upload test custom
        env:
          GIST_TOKEN: ${{ secrets.GIST_TOKEN }}
          GIST_ID: ${{ secrets.GIST_ID }}
        run: bash uploadnumber.sh


Enter fullscreen mode Exit fullscreen mode

8)Create the Shield badge from the raw gist file
https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/{gitusername}/{gist_id}/raw/{file_name}

Billboard image

Use Playwright to test. Use Playwright to monitor.

Join Vercel, CrowdStrike, and thousands of other teams that run end-to-end monitors on Checkly's programmable monitoring platform.

Get started now!

Top comments (0)

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay