node.js : Mocha is a powerful and easy to use testing framework for Node.js

in #node7 years ago (edited)

 // descript : test 할 suit 

describe('test suit', () => {

    // it : 테스트 코드 작성
   it('test name', () => {
       assert.equal(car.park(), 'stopped');
   });

});

// before() : descript 실행전 한번 수행

// beforeEach() : 모든 descript 실행전  수행

// after() : descript 실행후 한번 수행

// afterEach() : 모든 descript 실행후  수행



// package.json

{
"name": "inbox",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
    "test": "mocha"
 },
"author": "",
"license": "ISC",
"dependencies": {
"ganache-cli": "^6.1.0",
"mocha": "^5.1.1",
"solc": "^0.4.23",
"web3": "^1.0.0-beta.26"
 }
}


// run test

npm run test


 Mocha is a powerful and easy to use testing framework for Node.js. Its power comes from its flexibility. With support for all the popular test writing styles (BDD, TDD, Exports, QUnit or Require) and seemingly endless ways to output test results, it's no wonder Mocha has a 94% satisfaction rating among those who have used it.

Install and Setup

Unlike other frameworks, it's dead simple to get started with Mocha.To install Mocha globally:

$ npm install -g mocha

To install Mocha as a project dependency:

$ npm install --save-dev mocha

Once Mocha is installed, the only other setup you'll need to do is create a test directory in your project root. That's where Mocha will look for *.js (or, alternatively, *.coffee) files to run.

Optional: Install an Assertion library

You can use Node's built-in assert() function in your automated tests, but great tests require more nuanced assertions than assert() allows. If you have a favorite assertion library, go ahead and use it. If you don't, I recommend Chai. Install it the same way we installed Mocha:Globally:

$ npm install -g chai

Or for your project:

$ npm install --save-dev chai

Writing Tests

For this example we'll be testing a simple login page to make sure that it rejects bad login attempts, and accepts good login attempts.Definitely take a look at the example test in this repo! It's heavily commented, so hopefully it's easy to read and understand.The basic structure of each test is as follows:

  • Each test suite, or "feature", is enclosed in a describe() block.
  • For each describe() block, you can call special methods before(), after(), beforeEach(), and afterEach(). Those methods will run before the whole suite, after the whole suite, before each test case, and after each test case, respectively.
  • Tests are defined by calling it() inside a describe() block.
  • Each function inside a describe() block will block execution of other tests until done() is called.

Here's an example of a simple test of a function that adds two numbers together and returns a promise. Before each test we call a reset method on the adder. We also have a cleanUp function at the bottom to where we would put any test cleanup code.

describe("number adder"{

   beforeEach(function resetAdder(done){

       numberAdder.reset().then(done);

   });



   it("returns the sum of two positive numbers", function(done){

       numberAdder(7,3)

       .then(function(result){

           assert(result == 10);

       })

       .then(done);

   });



   it("returns the sum of a positive and negative number", {

       numberAdder(7,-3)

       .then(function(result){

           assert(result == 4);

       })

       .then(done);

   });



   after(function cleanUp(done){

       // any test cleanup

       done();

   })

});

Take a look at login.js and you'll see a very similar structure.

Running tests

To run your tests, just navigate to the root project folder and run $ mocha. It's that easy!

https://help.crossbrowsertesting.com/selenium-testing/frameworks/mocha/?_ga=2.150825782.1202929143.1525931692-1869093701.1525931692


http://blog.jeonghwan.net/mocha/