Module test
The test module is a testing framework. Combined with the assertion module, assert
various test cases can be easily written.
fibjs
Introducing modules into test
often requires first test setup
operations:
1
2var test = require('test');
test.setup();
Before writing test cases, you usually need to define a test module to describe the test content.
- describe
describe is a container for all test groups, similar to the concept of a test suite, and is used to it
anchor tests under a specific category. describe can contain multiple it use cases or other subcategories represented by nested describe.
1describe(String name, Function block)
Call parameters: name: String, define module name block: Function, module initialization code
- it
Represents a single test case, and each description should only test a single situation to ensure the reliability of the test results.
1it(String name, Function block)
Call parameters: name: String, define project name block: Function, test content
- xit&it.skip
Represents skipped test cases.
1xit(String name, Function block)
Call parameters: name: String, define project name block: Function, test content
- oit & it.only
Indicates that only the current test case will be run and other test cases will be ignored so that the current test case can be debugged separately, which is very practical.
1
2oit(String name, Function block)
it.only(String name, Function block)
Call parameters: name: String, define project name block: Function, test content
- todo
Indicates the need to further improve the test case plan.
1todo(String name, Function block)
Call parameters: name: String, define project name block: Function, test content
When writing test cases, it is common to useassertAssertion module, checks the return of the function. How to use it:
1assert(condition, String message);
Among them, the first parameter is the condition that needs to be asserted, and the second parameter is the error message.
object
assert
Assert the test module. If the test value is false, an error will be reported. The error reporting behavior can be set to continue running or throw an error.
1assert test.assert;
static function
describe
Define a test module, which can be nested
1
2static test.describe(String name,
Function block);
Call parameters:
- name: String, defines module name
- block: Function, module initialization code
xdescribe
Module definition to pause testing,test.setupYou can then use describe.skip to call
1
2static test.xdescribe(String name,
Function block);
Call parameters:
- name: String, defines module name
- block: Function, module initialization code
odescribe
module definitions for independent testing,test.setupYou can then use describe.only to call
1
2static test.odescribe(String name,
Function block);
Call parameters:
- name: String, defines module name
- block: Function, module initialization code
it
Define a test project
1
2static test.it(String name,
Function block);
Call parameters:
- name: String, defines the project name
- block: Function, test content
xit
Project definition to pause testing,test.setupYou can then use it.skip to call
1
2static 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.setupYou can then use it.only to call
1
2static test.oit(String name,
Function block);
Call parameters:
- name: String, defines the project name
- block: Function, test content
todo
plan project definition,test.setupYou can then use it.todo to call
1
2static test.todo(String name,
Function block);
Call parameters:
- name: String, defines the project name
- block: Function, test content
before
Define the current test module entry event
1static test.before(Function func);
Call parameters:
- func: Function, event function
after
Define the exit event of the current test module
1static test.after(Function func);
Call parameters:
- func: Function, event function
beforeEach
Define the current test module test project entry event
1static test.beforeEach(Function func);
Call parameters:
- func: Function, event function
afterEach
Define the current test module test project exit event
1static test.afterEach(Function func);
Call parameters:
- func: Function, event function
mustCall
Test that a function must be called a specified number of times
1static Function test.mustCall(Function func);
Call parameters:
- func: Function, the function being tested
Return results:
- Function, returns the wrapped function
mustNotCall
Test that a function must not be called
1static Function test.mustNotCall(Function func);
Call parameters:
- func: Function, the function being tested
Return results:
- Function, returns the wrapped function
Test that a function must not be called
1static Function test.mustNotCall();
Return results:
- Function, returns the wrapped function
run
Start executing the defined test module
1static Object test.run(Integer mode = console.ERROR);
Call parameters:
- mode: Integer, specifies the test mode. When ERROR, the project error information is concentrated and displayed after the report. When it is lower than ERROR, the output information is displayed at any time. When it is higher than ERROR, only the report is displayed.
Return results:
- Object, return test results
After the test run is complete, the test results will be returned in the following form:
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{
"total": 2, // number of total test cases
"pass": 2, // number of passed test cases
"fail": 0, // number of failed test cases
"skip": 0, // number of skipped test cases
"todo": 0, // number of todo test cases
"time": 0.000000, // time elapsed in seconds
"cases": [ // details of test cases
{
"name": "test", // name of test case
"time": 0.000000, // time elapsed in seconds
"result": true, // result of test case
"error": null // message of error if test case failed
},
{
"name": "sub cases", // name of sub test case
"total": 1, // number of total test cases
"pass": 1, // number of passed test cases
"fail": 0, // number of failed test cases
"skip": 0, // number of skipped test cases
"todo": 0, // number of todo test cases
"time": 0.000000, // time elapsed in seconds
"cases": [ // details of test cases
{
"name": "test", // name of test case
"time": 0.000000, // time elapsed in seconds
"result": true, // result of test case
"error": null // message of error if test case failed
}
]
}
]
}
setup
Initialize the test environment of the current script and copy the test module method as a global variable of the current script
1static test.setup();
static properties
slow
Integer, set and query the slow test warning threshold, in ms, the default is 75
1static Integer test.slow;