What are Unit tests?
A unit is a module, component, or function. Unit tests exist to test individual units of software functionality. Unit testing is use to test the indivisual unit of the software/application. Unit of the software make sure that your code is written in the modular way, where it can achieve the cohesion and loosely couple system.If it’s hard to write unit tests for software without mocking lots of other things, that’s a sign that software is not modular enough. Every module should have unit tests, and every application should be made up of modules. In other words, if you’re not writing unit tests, you should be.
What are the tools to perform Unit testing?
There are many frameworks avaiable to test the node.js applications but we will talk about one particular "Mocha". Mocha is the unit test environment in the nodejs. Which provides the testing environment, assertion (third party modules - assert, should) and reporting etc.Even it is most popular testing framework there are few flows in it which are as follows.
- Too much configuration : Choose an assertion library, chose a reporting library.
- Globals : Mocha provides the global variables like describe, it which you never declared or initalised.
- Shared State : Functions like `beforeEach` and `afterEach` actively encourage you to do something you definitely should not do. This functions can be usually used to do test setup, teardown for individual test.
Tape VS Mocha
Tape(Test anything protocol) is the unit testing tool like mocha but, which comes with really simple syntax and avoiding the above mentioned flows in the mocha. Tape is light-weight and much faster and also supports browser.When you write a bug report, you should always provide a description, explain what you expected to see, and explain what you actually saw.
Test cases should be written in much the same way:
- Describe the feature that you’re testing in plain English.
- Provide the expected outcome of the test. This part is why many unit tests are called expectations.
- Compare that to the actual value.
Simple tests assertions provide:
- Better readability.
- Less code.
- Less maintenance.
No comments :
Post a Comment