Skip to content

Instantly share code, notes, and snippets.

@keicoon
Created April 21, 2021 02:32
Show Gist options
  • Save keicoon/2e4e47ecb6bd5e8e4048485bb784a4e7 to your computer and use it in GitHub Desktop.
Save keicoon/2e4e47ecb6bd5e8e4048485bb784a4e7 to your computer and use it in GitHub Desktop.
Export dotent test result to JUnit and Notify to external service in jenkins.
/// <summary>
/// environment : jenkins, dotnet
/// jenkins plugin : junit, mstest
/// </summary>
import hudson.tasks.test.AbstractTestResultAction
import hudson.model.Actionable
def webhook = "" // set your webhook url
def DOTNET_PATH = "C:\\Program Files\\dotnet"
def ROOT_WORKSPACE = ""
def CUR_STAGE = "Beginning"
def notifyMessage = ""
println("Enforced node: $ENFORCED_NODE")
node(ENFORCED_NODE) {
try {
stage ('Cleanup') {
CUR_STAGE = "Cleanup"
dir (ROOT_WORKSPACE) {
// write your project root path
dir ("UnitTest\\bin") {
deleteDir()
}
dir ("UnitTest\\obj") {
deleteDir()
}
dir ("UnitTest\\TestResults") {
deleteDir()
}
}
}
stage ('Prepare Test') {
CUR_STAGE = "Prepare Test"
dir (ROOT_WORKSPACE) {
/// fetch project from git
gitVars = url: ""
}
}
stage ('Unit Test') {
CUR_STAGE = "Unit Test"
dir (ROOT_WORKSPACE) {
bat "\"${DOTNET_PATH}\\dotnet.exe\" test UnitTest --logger \"trx;LogFileName=unit_tests.xml\""
step([$class: 'MSTestPublisher', testResultsFile:"**/unit_tests.xml", failOnError: true, keepLongStdio: true])
def testStatus = ""
def allTestPass = 0
AbstractTestResultAction testResultAction = currentBuild.rawBuild.getAction(AbstractTestResultAction.class)
if (testResultAction != null) {
def total = testResultAction.totalCount
def failed = testResultAction.failCount
def skipped = testResultAction.skipCount
def passed = total - failed - skipped
testStatus = "Test Status:\n Passed: ${passed}, Failed: ${failed} ${testResultAction.failureDiffString}, Skipped: ${skipped}"
if (failed == 0) {
notifyMessage = "test succ : ${testStatus}"
}
else {
notifyMessage = "test fail : ${testStatus}"
}
}
else
{
notifyMessage = "test fail : Didn't find any tests..."
}
}
}
stage('Junit') {
junit '**/unit_tests.xml'
}
}
catch (err) {
println "catch: ${err}"
notifyMessage = "plan failed : ${err}"
throw err
}
finally {
def now = new Date()
// TODO: write your webhook service
// parameters is webhook, notifyMessage
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment