Unit testing with Mocha and Travis CI’s Build stages


Let’s get started with Unit testing with Mocha and travis CI’s build stages.

Unit test is to test the small unit (maybe function or class) in the program, the purpose is to ensure that each small screw in the program has a good operation, as long as each small unit is ok, then the entire large system is less problematic.

In fact, we haven’t written the test programs. The program was written and used without errors. It was found to be too convenient. Every time you change the code, you can know whether there is an error or not till the next instruction.

Many people may never write a test, so this article will use a simple example to take everyone to write a test and integrate it into Travis CI.

Project architecture

This is the existing directory structure.

Unit testing with Mocha and travis

This small project is very simple, only three small features average, max with min, respectively, written in lib the three js file.

We are going to add tests for these three small functions.

Add unit tests with mocha

Mocha is a unit testing framework, the use of fairly simple finish after the test he will help you generate a report, in addition to mocha other than we need should, he makes it easier to describe the content you want to test.

Unit testing with Mocha and travis

After installing a new directory in the test folder, add three files inside average.js, max.jswemin.js will write the test in their respective js file.

Unit testing with Mocha and travis

Start writing tests

First to write average.js the test, mocha the syntax is:

Unit testing with Mocha and travis

test/average.J’s

We want to average be the average value in the array, if the array is empty then return NaN.

Unit testing with Mocha and travis

test/max.js

Then write max tests, we hope the maximum array can be obtained, if the array is empty then return undefined.

test / min.js

Finally, to write min the test, in fact, min with max much the same, as long as the maximum into a minimum just fine.

Unit testing with Mocha and travis

So that we have finished testing these three functions.

Running test

How do we run after writing the test? Very simple, just run back to the project directory mocha on it.

Unit testing with Mocha and travis

As shown, he will automatically run the test folder all the tests, if there is an error, then president of this.

Integration into Travis CI

Travis CI is a platform that provides continuous integration services. If you are using Github you can integrate your project with Travis CI. Any updates to Travis CI will automatically test for you.

First, log in with Github in the upper right corner of the Travis CI website.

Unit testing with Mocha and travis

After logging in, it will list all your repositories on Github, choose you to do the CI project.

Then add a file .travis.yml to write Travis settings in the project directory.

Unit testing with Mocha and travis

Under this paradigm .travis.ymlis an automatic test 6,7,8 three node version.

Unit testing with Mocha and travis

Because Travis will be used npm test for testing, so should package.json the test script change mocha, which would run mocha tests on Travis, this is my package.json

Unit testing with Mocha and travis

In this way, all settings are set. As long as you push to Travis on Github, you will help the test. After the actual push, take a look at Travis. All three versions have passed the test.

You can put the above build status in the README, which will make the project look more reliable. If someone sends a pull request, you can also run a test to verify the correctness.

Conclusion

This article uses a very simple example to take you through mocha and should follow Travis CI. The actual case to be tested may not be so simple, but the methods are similar. For more in-depth study, you can look at their files:

Although writing a test is a bit cumbersome, it only needs to be written once. It is inevitable that there will be omissions and a waste of time when you manually test. It is often impossible to find real bugs.