Module basic module

Module test

Test suite module to define and manage test suite

Instructions:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
var test = require('test'); test.setup(); describe('test', () => { before(() => { // setup before the whole test }); beforeEach(() => { // setup before each test }); after(() => { // cleanup after the whole test }); afterEach(() => { // cleanup after each test }); it('case', () => { assert.ok(true); }); // ignored test case xit('case', () => { assert.ok(true); }); // ignored test case it.skip('case', () => { assert.ok(true); }); // only test case oit('case', () => { assert.ok(true); }); // only test case it.only('case', () => { assert.ok(true); }); }); // async function test describe('test async', () => { it('pass case', async () => { assert.ok(true); }); it('error case', async () => { throw new Error('some thing wrong!'); }); }); // callback function test // cannot use callback mode in jsc describe('test callback', () => { it('pass case', done => { setTimeout(() => { assert.ok(true); done(); }, 0); }); it('error case', done => { setTimeout(() => { done(new Error('some thing wrong!')); }, 0); }); it('check in done', done => { setTimeout(() => { done(() => { assert.ok(true); }); }, 0); }); }); process.exit(-test.run(console.DEBUG));

Object

assert

Assert the test module, if the test value is false, an error will be reported, and the error behavior can be set to continue running or throw an error

1
assert test.assert;

Static function

describe

Define a test module, can be nested definition

1 2
static test.describe(String name, Function block);

Call parameters:

  • name: String, defines the module name
  • block: Function, module initialization code

xdescribe

The definition of the module to suspend testing,test.setup Then you can use describe.skip to call

1 2
static test.xdescribe(String name, Function block);

Call parameters:

  • name: String, defines the module name
  • block: Function, module initialization code

odescribe

Module definition for independent testing,test.setup You can use describe.only to call later

1 2
static test.odescribe(String name, Function block);

Call parameters:

  • name: String, defines the module name
  • block: Function, module initialization code

it

Define a test project

1 2
static test.it(String name, Function block);

Call parameters:

  • name: String, defines the project name
  • block: Function, test content

xit

The definition of the project to suspend testing,test.setup Then you can use it.skip to call

1 2
static test.xit(String name, Function block);

Call parameters:

  • name: String, defines the project name
  • block: Function, test content

oit

Project definition for independent testing,test.setup It can be called later using it.only

1 2
static test.oit(String name, Function block);

Call parameters:

  • name: String, defines the project name
  • block: Function, test content

before

Define the current test module entry event

1
static test.before(Function func);

Call parameters:

  • func: Function, event function

after

Define the exit event of the current test module

1
static test.after(Function func);

Call parameters:

  • func: Function, event function

beforeEach

Define the test item entry event of the current test module

1
static test.beforeEach(Function func);

Call parameters:

  • func: Function, event function

afterEach

Define the exit event of the current test module test project

1
static test.afterEach(Function func);

Call parameters:

  • func: Function, event function

run

Start to execute the defined test module

1
static Integer test.run(Integer loglevel = console.ERROR);

Call parameters:

  • loglevel: Integer, specify the log output level during the test. When ERROR, the project error information will be displayed after the report. When it is lower than ERROR, the output information will be displayed at any time. When it is higher than ERROR, only the report will be displayed.

Return result:

  • Integer, Returns the statistical results of test cases, returns 0 if it is correct, and returns the number of errors if it is wrong

setup

Initialize the test environment of the current script, copy the test module method to the global variable of the current script

1
static test.setup();

Static properties

slow

Integer, set and query the slow test warning threshold, in ms, the default is 75

1
static Integer test.slow;