Test-driven Development (TDD) and test framework mocha.js Getting started learning

Source: Internet
Author: User

The group is now changing the development model from the traditional development model (developer development, QA testing) to the development models that try TDD(test-driven Development, test-driven development ). As a result, there is no QA role, or only a small amount of QA is used for integration testing between system modules.

Therefore, the testing and development of the code will be guaranteed by the developer (Developer).

  This requires the help of a good testing framework , especially the automated testing framework that supports TDD development patterns, because I use the programming language is node. JS, then the widely used mocha.js will be my first choice.

  

In the process of team transformation, many things have to be explored by us. For the TDD development model of node. JS, I also did some introductory studies.

Let's start by knowing what TDD is.

I. What is TDD?

Through Wikipedia's introduction to TDD, it is known that TDD, full name Test-driven development, Chinese test-driven development, is an extreme programming idea derived from agile development.

Roughly speaking, before the development of developer, write the test case, the test case is written, then to implement the method or function needed to implement, and then run through the test case, see whether it can pass. In the future when adding functionality, it is also a test case, and then the new functionality is implemented, then run through all the test cases, if all use cases are successful (Pass), then the code quality can be guaranteed.

is a TDD mode flowchart from Wikipedia.

  

This is probably a process.

In the TDD scenario, the test case is first and the first priority .

  

In addition to TDD, there are concepts of ATDD and BDD. The concept of BDD is a lot of simple introduction.

  1. TDD and BDD

BDD is Behaviour-driven development, behavior-driven development, which is more concerned with testing than TDD,BDD, observing that the program behaves correctly, so its interface is to use describe. In contrast to BDD, TDD is more biased with the ability to test the code to achieve the correct functionality, and its interface is suite. Because I am also a beginner, the difference understanding is not deep. But for me, good, fit is more important, so I will choose TDD as a point of entry, may later be adjusted according to the actual.

  

  2. Test Suite

From the above, TDD interfaces use suite. It's important to introduce the concept of test suite, in Wikipedia,

Test Suite:a Collection of test cases that is intented to being used to test a software program to show the It has same spe Cified set of behaviours.

In fact, Suite is a set of test cases that can be used to categorize test cases. The suite can be nested within a suite, like a set of test examples that measure a function, and then subdivide the unit test examples for different small functions.

  

  3. Test Case Structure

When we write test cases, a widely accepted structure is:

A. Setup: Prepare the environment and data before running this test case

B. execution: Executing the test (the main code for the implementation of the test case)

C. Validation: Validation Results

D. Cleanup: On-site recovery, generally contrary to a. Does not affect the test cases running behind.

  

After introducing the concept and interface of TDD, we can introduce mocha.js.

Two. Mocha.js-javascript test framework, support TDD,BDD and many other interfaces

Mocha.js is a widely used JavaScript testing framework, official website: http://mochajs.org/

The official definition of it is:

Mocha is a Feature-rich JavaScript test framework running on node. JS and the browser, making asynchronous testing simple a nd fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct T EST cases.

  

With Mocha.js, you can write test cases, run use cases to get results, and support multiple formats of report to display results. Support TDD,BDD and other interfaces, is a good helper in TDD development process.

Since TDD and Bdd,mocha provide different interfaces, my example here is mainly using TDD.

  

The first thing to install is mocha.js, which is available via NPM

1 npm install-g Mocha

  

After installation, you can use the Mocha command to use the features provided by Mocha.

For example, mocha-h can view command help, as follows.

  

  

Here, I implement a simple and common test case to illustrate how mocha.js is used.

  

Let's start by introducing a few important interfaces,

  Suite: Define a set of test cases.

  Suitesetup: This method executes one time before all of the suite's test cases are executed, only once, which is the difference from setup.

  Setup: This method executes one time before each test case executes.

  Test: The implementation code for the testing case that is executed specifically.

  teardown: This method executes once after each test case executes, as opposed to setup.

  Suiteteardown: This method executes once for all of the suite's test cases, as opposed to Suitesetup.

These interfaces are compatible with the interfaces in the TDD concept and are implemented to facilitate the organization of test cases. The interface of BDD is not to be mentioned here, but it can refer to official documentation.

  

The test case code is as follows:

1 varassert = require (' Assert ');2 varMocha = require (' Mocha '));3 4 varSuite =Mocha.suite;5 varSetup =Mocha.setup;6 varSuitesetup =Mocha.suitesetup;7 varTest =mocha.test;8 varTeardown =Mocha.teardown;9 varSuiteteardown =Mocha.suiteteardown;Ten  One //Test case ASuite (' Array ',function(){ -      -Suitesetup (function(){ the         //Suitesetup'll run only 1 time on Suite Array, before all suite -         //... -Console.log (' Suitsetup ... '); -     }); +      -Setupfunction(){ +         //Setup'll run 1 time before every suite runs in Suite Array A         //... atConsole.log (' Setup ... '); -     }); -  -Suite (' indexOf () ',function(){ -Test (' should return-1 when not present ',function(){ -Assert.equal ( -1, [1, 2, 3].indexof (4)); in         }); -     }); to  +Suite (' IndexOf2 () ',function(){ -Test (' should return not-1 when present ',function(){ theAssert.equal (0, [1, 2, 3].indexof (1)); *         }); $     });Panax Notoginseng  -Teardownfunction(){ the         //teardown'll run 1 time after every suite runs in Suite Array +         //... AConsole.log (' teardown ... '); the     }); +  -Suiteteardown (function(){ $         //Suiteteardown'll run 1 time in Suite Array, after all suits run over. $         //... -Console.log (' Suiteteardown ... ');  -     }); the});

Execute the test case with the following command:

1 Mocha Test.js

The results are as follows:

  

You can see the 2 test examples are pass, in addition, through the printed information, you can see different interfaces of the use of the difference. These are handy for writing the right test case later.

In short, it can be seen that with the help of Mocha.js, TDD's node. JS Development is easy to talk about.

Three. Some thoughts on TDD

I've seen an article in Chenhao before about TDD that doesn't look so beautiful. Through their own contact, found some difficult to do places or small problems, as follows:

  1. Test case difficult to write

It is not easy to write a use case that accurately tests the functionality of the module for different functions, which can take a lot of time and effort. And developers tend to focus on the realization of the function mainly.

  2. To maintain a large number of test cases

Complex module, the number of test cases are also many, although every time after the code changes, run all the examples are cool, but if the function code often needs to change, test cases must be constantly changed, need to spend energy maintenance.

  

  In any case, change will always bring good things and embrace change. It is important to explore a suitable development model in the cross-river. Hope that the real development tasks come, can continue to summarize the problems encountered, and according to their actual needs, to find the appropriate mode and path:-)

Four. Reference documentation

1. Http://en.wikipedia.org/wiki/Test-driven_development

2. Http://en.wikipedia.org/wiki/Test_suite

3. http://mochajs.org/

Kevin Song

June 9, 2015

  

Test-driven Development (TDD) and test framework mocha.js Getting started learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.