Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

DescribeThis Shared Library hosts a Custom Step which is an example implementation of the Unit Test step of a MettleCI Pipeline.

Here’s the pseudocode for the Shared Library:

Code Block
languagegroovy
def call(
    def ENVIRONMENTNAME
) {
	try {   mettleci properties config    bat label: 'Upload unit test specs',              script: '''          // Configure Properties
    %AGENTMETTLECMD%mettleci remote upload ^
                -host %IISENGINENAME% ^
                -username %MCIUSERNAME% -password %MCIPASSWORD% ^
                -source unittest ^
                -transferPattern "**/*" ^
                -destination %ENGINEUNITTESTBASEDIR%/specs/%DATASTAGE_PROJECT%
     execute "config/cleanup_unittest.sh"    // Cleanup results of previous unit tests
    mettleci remote '''upload          bat label: 'Configure Properties',              script: '''      // Unit test specifications and data
     %AGENTMETTLECMD% properties config ^
       mkdir         -baseDir datastage ^                 -filePattern "cleanup_unittest.sh" ^                 -properties var.%ENVID% ^                 -outDir config
            '''

        bat label: '// Create unit test report dir',

	try {
          script: "if not exist \"unittest-reports\" mkdir \"unittest-reports\""

        bat label: 'Run Unit Tests', 
            script: """
                %AGENTMETTLECMD% mettleci unittest test ^                 -domain %IISDOMAINNAME% ^          // Run Unit Tests
   -server %IISENGINENAME%}
^
    finally {           -username %IISUSERNAME% -password %IISPASSWORD% ^                 -project %DATASTAGE_PROJECT% ^             // Whether the above -specsis unittestsuccessfult ^or not...
        mettleci remote download     -reports unittest-reports ^                 -project-cache \"%AGENTMETTLEHOME%\\cache\\%IISENGINENAME%\\%DATASTAGE_PROJECT%\" ^
                -test-suite \"MettleCI Unit Tests - ${ENVIRONMENTNAME}\"
            """

        bat label: '  // Download unit test reports',
        if     script: '''
                %AGENTMETTLECMD% remote download ^
                -host %IISENGINENAME% ^
                -source %ENGINEUNITTESTBASEDIR%/reports ^
                -destination unittest-reports ^
                -transferPattern "%DATASTAGE_PROJECT%(exists 'unittest-reports/**/*.xml" ^
     ') {
          -username %MCIUSERNAME% -password %MCIPASSWORD%
            '''

        junit testResults: 'unittest-reports/**/*.xml', 
                allowEmptyResults: true

        bat label: 'Cleanup', 
            script: '''
                %AGENTMETTLECMD% remote execute ^
                -host %IISENGINENAME% ^
                -username %MCIUSERNAME% -password %MCIPASSWORD% ^
                -script "config/cleanup_unittest.sh"
      // Publish Unit Test results
        '''    }
    catch(e) {
        bat label: 'Cleanup', 
            script: '''
                %AGENTMETTLECMD% remote execute ^
                -host %IISENGINENAME% ^
                -username %MCIUSERNAME% -password %MCIPASSWORD% ^
                -script "config/cleanup_unittest.sh"
            '''
        throw e
    }
}}
}

Note that this Custom Step makes used of Jenkins' JUnit plugin.