Continuous integration unit testing with karma and Jenkins

Unit testing is generally not performed only locally, using continuous integration tools, and automated unit testing on a daily basis. For JavaScript, it needs to be performed in multiple browser environments. To test compatibility, we need to know how many test cases fail. Test coverage, etc.

1. Reference document
http://karma-runner.github.io/0.12/plus/jenkins.html
http://www.shenyanchao.cn/blog/2013/04/01/run-karma-in-jenkins-ci /

2. Install node.js on the Jenkins server
installation steps reference
quote



3. Configure test-related plug-ins Open the command line
in the project directory and execute
npm karma-chrome-launcher #chrome browser plugin
npm karma-firefox-launcher #firefox browser plugin
npm karma-junit-reporter #junit test report plugin
npm karma-coverage #unit test coverage plugin


4. Modify the configuration file karma.conf.js
to add test result reports and unit test coverage
module.exports = function(config) {
  config.set({
    // base path, that will be used to resolve files and exclude
    basePath: '.',

    frameworks: ['jasmine'],

    // list of files / patterns to load in the browser
    files: [
      'js/*.js'
    ],

    // list of files to exclude
    exclude: [
      'client/main.js'
    ],

    preprocessors: {
    	'js/*.js':'coverage'
    },
    
    coverageReporter:{
    	    type : 'cobertura',
    	    dir : 'coverage/'
    	},

    // use dots reporter, as travis terminal does not support escaping sequences
    // possible values: 'dots', 'progress'
    // CLI --reporters progress
    reporters: ['dots', 'junit','coverage'],

    junitReporter: {
      // will be resolved to basePath (in the same way as files/exclude patterns)
      outputFile: 'test-results.xml'
    },

    // web server port
    // CLI --port 9876
    port: 9876,

    // enable / disable colors in the output (reporters and logs)
    // CLI --colors --no-colors
    colors: true,

    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    // CLI --log-level debug
    logLevel: config.LOG_INFO,

    // enable / disable watching file and executing tests whenever any file changes
    // CLI --auto-watch --no-auto-watch
    autoWatch: true,

    // Start these browsers, currently available:
    // - Chrome
    // - ChromeCanary
    // - Firefox
    // - Opera
    // - Safari (only Mac)
    // - PhantomJS
    // - IE (only Windows)
    // CLI --browsers Chrome,Firefox,Safari
    //browsers: [process.env.TRAVIS ? 'Firefox' : 'Chrome'],
    browsers: ['IE'],

    // If browser does not capture in given timeout [ms], kill it
    // CLI --capture-timeout 5000
    captureTimeout: 20000,

    // Auto run tests on start (when browsers are captured) and exit
    // CLI --single-run --no-single-run
    singleRun: true,

    // report which specs are slower than 500ms
    // CLI --report-slower-than 500
    reportSlowerThan: 500,

    plugins: [
      'karma-jasmine',
      'karma-chrome-launcher',
      'karma-firefox-launcher',
      'karma-ie-launcher',
      'karma-junit-reporter',
      'karma-commonjs',
      'karma-coverage'
    ]
  });
};



The junit report test plugin will generate the test-results.xml file in the current project directory.
The coverage coverage plugin will generate a coverage directory in the current directory, which contains coverage data files.

5. Jenkins installation plugin
Jenkins Cobertura Plugin Test coverage plugin

6. Configure a free-style task
on Jenkins In the source code management, fill in the SVN address of the project, etc., so that Jenkins can download the source code normally

7. Build
command Select Execute windows for build batch command
set Path=%Path%;D:\Program Files\nodejs\
set IE_BIN=C:\Program Files\Internet Explorer\iexplore.exe
node ./node_modules/karma/bin/karma start


8. Test options After
building , operate the

Publish Cobertura Coverage Report option and
fill in the Cobertura xml report pattern
coverage/**/*.xml


Publish JUnit test result report option
Fill in Test report XMLs
test-results.xml


9. Test results









Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326626545&siteId=291194637