In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Mocha: the Unit Test Framework of nodejs
When we write more complex projects, we need to track the test cases of the project for a long time, control the quality of the unit modules, and self-test the development results, so we need a useful tool to teach ourselves mocha. This note is almost copied from the English version of the official website for a preliminary presentation of self-understanding, mostly items, and there is no time for further practice. By forcing me to take notes, I urge myself to deepen understanding, learn to share, and improve my learning ability. I am working hard.
1. Install npm install-- global mocha2. Install as a project development dependency
Npm install-save-dev mocha
3. Considerations for version
Mocha v3.0.0 requires npm v2.14.2 +
After v3.0.0, you no longer rely on string matching to decide which tests to perform.
.only () can be used multiple times to define a subset of tests to run.
4. Coding
Can support before, after, beforeEach, afterEach
5.test directory
Mocha runs all tests under the test directory, and the directory name cannot be modified.
If you want to include subdirectory recruitment-- recursive
6. Generating package.json depends on mocha
Use the npm init command to generate package.json
7. One section for each test
It ("name", function () {… })
8. Testing principle
Test only one situation at a time, and the test code should be very simple cd mocha
9. Start the command (1) .node_modules/mocha/bin/mocha (2) first package.json plus script {"test": "mocha"}, and then npm test10. Configure the debug command 11. Paragraph code specifies specific execution of describe.only ("# indexOf ()", function () {}); skips descrip.skip ("# indexOf ()", function () {}); skips multiple test before (function () {if (/ * check test environment * /) {/ / setup code} else {this.skip ();}}); Diff view
Err.actual err.expected assertion success failure view
Require guide package var testCase = require ('mocha'). Describe;// can retry the test with more intuitive variable names. When this.retries (x) is called, the beforeEach and afterEach methods are called again to dynamically generate the slowly defined test method this.slow (10000); define the timeout method this.timeout (10000); the code level is divided into suite-level 12.mocha test interfaces in the it fragment hook-level beforeEach
BDD,TDD,Exports,QUnit,Require
The BDD API is more commonly used, and the previous examples are all BDD.
The TDD interface is called in a way similar to JUnit.
QUNIT flattening looks similar to TDD, but also supports before,after,beforeEach,afterEach
13. Testing tool
Wallaby.js
Mocha Mocha sidebar
14. Test report
Spec Reporter: you can output nested hierarchical views
Dot matrix reporter: the output is minimized, a series of characters can be used to represent the test case, the failure is a red exclamation mark, the pending blue comma, and the slow speed is × × display.
NYAN reporter: there is a cat making a progress bar, which is inexplicable
TAP reporter: displays the details of each line of unit tests
LANDING STRIP Reporter: the runway where the plane landed
LIST reporter: list of details
PROGRESS progress report: progress bar
MIN Reporter: show summary only
DOC reporter: output as body in html format
(1) install the reporter mocha.setup ({ui:'tdd',ignoreLeaks: true,asyncOnly: true}); (2) specify the parameter to specify the reporter mocha-- reporter list-- growl (3) the variable defines the reporter $REPORTER=nyan npm test
Multiple calls to callback will throw errors
15.require ('chai') is a library of assertions
Var should = require ('chai'). Should ()
This can judge should.exist (bitcore.lib); whether these contents exist or not
Language chain tobebeensinon can create fake servers
Let server = sinon.fakeServer.create ()
Sinon provides spy and Stub matching assertion framework chai
Simulated return value: sinon.fake.returns (42)
Simulate ajax:sinon.replace (jQuery, 'ajax', sinon.fake ())
Simulate xhr:sinon.useFakeXMLHttpRequest ()
Simulation server: sinon.fakeServer.create ()
Proxyquire override dependencies (dependency injection? ) simulate database
Sinon.mock (database)
Differences between 16.spies, stub and mock
Stub has all the features of spies, not to monitor calls, but to replace them completely.
Like stub, mock is used to replace a specified function, mock is used to replace multiple methods, and stub is used to replace a function.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.