0.7.1 • Published 9 years ago

testrun v0.7.1

Weekly downloads
3
License
MIT
Repository
github
Last release
9 years ago

testrun Build Status Build Status

A module to support a test.

NPM

Install

Install with npm

$ npm install testrun --save-dev

Usage

1. Create test cases.

(sample.js)
const testrun = require('testrun').mocha;

function testfn(testcase) {  // `testcase` is a property of the 3rd argument of `testrun`
  if (typeof testcase.in === 'string') {
    return testcase.in;
  } else {
    throw new TypeError();
  }
}

testrun('test name', testfn, [
  {
    name: 'test case group',
    cases: [  // `cases` is the keyword for specifying test case group.
      {
        name: 'test for input : ${testcase.in} => ${testcase.expected}',
        in: 'aaa',
        expected: 'aaa',   // `expected` is the keyword for specifying expected value.
      },
      {
        name: 'test for in:${testcase.in} => ${testcase.error}',
        in: ['a', 'b', 'c'],
        error: TypeError,  // `error` is the keyword for specifying expected error.
      },
      {
        name: 'sub test case group',
        cases: [ ... ],
      },
      {
        name: 'skip this test case',
        skip: true,        // `skip` is the keyword to skip a test case or a test case group.
        ...
      },
      /*
      {
        name: 'only run this test case',
        only: true,        // 'only' is the keyword to run only a test case or a test case group.
        ...
      },
      */
    ],
  },
]);

2. Run the test cases.

$ mocha sample.js

   test case group
     ✓ test for in: 'aaa' => 'aaa'
     ✓ test for in:[ 'a', 'b', 'c' ] => TypeError

   0 passing (16ms)

Supporting test frameworks

  • mocha

    const testrun = require('testrun').mocha;
  • lab

    const testrun = require('testrun').lab(exports);

    NOTICE: If using lab on node version 0.10 or 0.12, you have to use lab version 6.2.

APIs

testrun(testname, testfn, testcases) => void

runs the specified test function for each test cases.

Parameters:
  • testname string : a test name.
  • testfn function : a test function. (see below.)
  • testcases array : an array of test cases.

    testfn (testcase , done) => any

    is a function to execute a testcase. The argument testcase is each property in testcases passed to testrun function.

    Parameters:
    • testcase object : a test case object.
    • done function : a callback to end. - If this argument is not specified, testfn is required to return a result or to throw an error. - If this argument is specified, testfn is required to evaluate a testcase in own way and execute this argument to end it.

      testcase

      Reserved words for properties of testcase:
    • expected any : set an expected value of a test case.

    • error Error : set an error type which a test case throws
    • cases array : set a test case group.
    • skip boolean : set true if you want to skip a test case or a test case group.
    • only boolean : set true if you want to run only a test case or a test case group.

testrun.byPlatform(valuesByPlatforms) => any

returns a value for the current platform.

Parameters:
Example:
testrun.byPlatform({win32: 123, otherwise: 999 });
// => 123  (on Windows)
// => 999  (on other platform)

testrun.byPlatform('abc');
// => 'abc'

testrun.scriptrun(templateFile, testDir) => function

returns a function which executes javascript based on content of templateFile on child process.

Parameters:
  • templateFile string : a path string of a javascript template file for a test case.
  • testDir string : a path string of a directory to output a javascript file for a test case.
Example:
(template.js)
const assert = require('assert');
var testcase = ${testcase};
assert.strictEqual(testcase.input.toUpperCase(), testcase.expected);
const testfn = testrun.scriptrun(templateFile, testDir);

testrun('convert letter case', testfn, [
  { name: 'convert ${testcase.input} => ${testcase.expected}', input: 'abc', expected: 'ABC' },
  ...
]);

License

Copyright (C) 2016 Takayuki Sato

This program is free software under MIT License. See the file LICENSE in this distribution for more details.

0.7.1

9 years ago

0.7.0

9 years ago

0.6.0

9 years ago

0.5.0

9 years ago

0.4.0

9 years ago

0.3.0

9 years ago

0.2.0

9 years ago

0.1.0

9 years ago