GithubHelp home page GithubHelp logo

mnjstwins / nunitjs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fmontanari/nunitjs

0.0 1.0 0.0 45 KB

NUnitJS is a unit test framework for NodeJS

Home Page: nunitjs.org

JavaScript 100.00%

nunitjs's Introduction

NUnitJS

NUnitJS provides easy async unit testing for NodeJS. This framework was inspired by NUnit and NodeUnit.

  • Simple to use
  • Just run the single file script.
  • Runs automatically all fixture modules with filename ends with "_fixture.js".
  • Works with node.js.
  • Helps you avoid common pitfalls when testing asynchronous code
  • Easy to add test cases with setUp and tearDown functions if you wish
  • Easy to add fixtureSetUp and fixtureTearDown functions if you wish

Usage

Here is an example unit test fixture module: fixtures/sample_fixture.js

var assert = require("assert");

exports.testSomething = function(test){
    assert.ok(true, "this assertion should pass");
    test.done();
};

exports.testSomethingElse = function(test){
    assert.ok(false, "this assertion should fail");
    test.done();
};

Here the command line to run:

node nunit.js --path="fixtures/sample_fixture.js"

When run, this will output the following xml report:

<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="fixtures.sample_fixture" time="0.006" tests="2" failures="1">
    <testcase name="testSomething" time="0"/>
    <testcase name="testSomethingElse" time="0.162">
        <failure message="this assertion should fail" type="AssertionError"><![CDATA[actual: false
expected: true
operator: ==
AssertionError: this assertion should fail
    at Object.exports.testSomethingElse (/path/to/project/fixtures/sample_fixture.js:9:12)
    at Context.startDelegate (/path/to/project/nunit.js:501:29)
    at Context.start (/path/to/project/nunit.js:608:18)
    at runTest (/path/to/project/nunit.js:542:17)
    at /path/to/project/nunit.js:450:13
    at setUp (/path/to/project/nunit.js:468:13)
    at TestRunner.run (/path/to/project/nunit.js:449:9)
    at FixtureRunner.runNextTest (/path/to/project/nunit.js:347:20)
    at /path/to/project/nunit.js:354:13
    at /path/to/project/nunit.js:452:21]]></failure>
    </testcase>
</testsuite>

Command-line arguments

--path=[path],[path]

Path, folder or file, where to find fixture. Only file that ends with "_fixture.js" are executed. Multiple Path are allowed with comma separated.

node nunit.js --path=testfolder

node nunit.js --path=folder1,folder2

node nunit.js --path=fixtures/sample1_fixture.js,fixtures/sample2_fixture.js

--test=[test name]

Test function name to execute alone.

node nunit.js --path=fixtures/sample_fixture.js --test=testSomething

--delay=[Milliseconds]

delay to start tests. Used for debug mode on waiting for debug attached.

node nunit.js --delay=1000

--reports-path=[path]

Path where to generate reports parsable by Jenkins. Defaults to reports.

node nunit.js --reports-path=reportfolder

--sonar-reports-path=[path]

Path where to generate SonarQube reports. Defaults to sonar-reports.

node nunit.js --sonar-reports-path=reportfolder

--verbose

print passed tests too

--version

print the current version

Asynchronous Testing

NUnitJS is designed to help you testing asynchronous code. All context (test, setUp, tearDown, fixtureSetUp and fixtureTearDown) can explicity complete with done() method on context object:

Example 1: complete on same tick

exports.testSomething = function(context){
    
    context.done();
};

Example 2: complete on different tick

exports.testSomething = function(context){
  
    setTimeout(function(){
    
        context.done();
    
    }, 500);
};

Timeout

if the done() method was never called, test failed on default timeout, 1000ms. You can change timeout if your test spends much time.

exports.testSomething = function(context){

    context.setTimeout(2000);
  
    setTimeout(function(){
    
        context.done();
    
    }, 1500);
};

setUp and tearDown

NUnitJS allows you to define a setUp function, which is run before each test, and a tearDown function, which is run after each test calls test.done():

exports.setUp = function (setUp) {
    this.foo = 'bar';
    setUp.done();
};

exports.tearDown = function (tearDown) {
    // clean up
    tearDown.done();
};

exports.test1 = function (test) {
    assert.equals(this.foo, 'bar');
    test.done();
};

fixtureSetUp and fixtureTearDown

NUnitJS allows you to define a fixtureSetUp function, which is run before each fixture, and a fixtureTearDown function, which is run after each fixture completed.

exports.fixtureSetUp = function (fixtureSetUp) {
    this.foo = 'bar';
    fixtureSetUp.done();
};

exports.fixtureTearDown = function (fixtureTearDown) {
    // clean up
    fixtureTearDown.done();
};

exports.test1 = function (test) {
    assert.equals(this.foo, 'bar');
    test.done();
};

NUnitJS plugin for WebStorm

NUnitJS plugin provides easy integration with WebStorm IDE

  • Simple to use.
  • Easy access actions in toolbar, main menu and context menu.
  • Runs and debug all tests.
  • Runs and debug current fixture.
  • Runs and debug fixture selected.
  • Runs and debug multiple selection folders.
  • Runs and debug current test.

http://github.com/fmontanari/NUnitJSPlugin

Donations

The Author invests a time and effort to make NUnitJS a useful tool. In addition, we have expenses. We have to purchase domain names and arrange for web site hosting. Making a financial contribution is one way in which you can help him ensure that NUnitJS continues to develop.

Donate with PayPal

nunitjs's People

Contributors

vorktanamobay avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.