Summary of front-end unit testing and introduction of testing tools, and unit testing tools

Source: Internet
Author: User

Summary of front-end unit testing and introduction of testing tools, and unit testing tools
1. Why unit testing?

  • Correctness: The test can verify the correctness of the code, so you have a thorough understanding before going online.
  • Automation: Of course, manual tests can also be performed. Internal information can be printed through the console, but this is a one-time task. The next test still needs to start from scratch, and the efficiency cannot be guaranteed. By writing test cases, you can write and run multiple times at a time.
  • Explanatory: When a test case is used to test interfaces and modules, how to use these Apis is involved in the test case. If other developers want to use these APIs, reading test cases is a good way, sometimes clearer than instructions
  • Driving development and guiding design: the premise of code testing is the testability of the Code itself. To ensure code testability, you must pay attention to the API design during development, TDD moves tests forward.
  • Guaranteed reconstruction: the Internet industry products are quickly iterated, and there must be a code reconstruction process after iteration. How can we ensure the quality of the Code after reconstruction? With the support of test cases, you can perform bold refactoring.
2. Front-end Unit Testing Technology

2.1 testing framework

Currently, there are many front-end testing frameworks, such as QUnit, jasmine, mocha, jest, and intern. These frameworks have their own characteristics. In a brief description, you can take a look at these frameworks:

  • Qunit
  • Jasmine: Behavior-Drive development (BDD)-style testing framework, which is popular in the industry and has comprehensive functions. It comes with the asssert and mock functions.
  • Mocha: works by corner stone of the node community, which can be used on the node and browser. It has great flexibility. You can select your preferred assertion library and select the report of the test result.
  • Intern: according to the official introduction, the testing framework has comprehensive functions and seems to include all test-related functions in the industry.

2.2 assertion Library

  • Chai: it should be a popular set of assert libraries. It supports two types of assert libraries: TDD and BDD.
    Var chai = require ('chai'); var assert = chai. assert; // typef assert === 'object' chai. shocould (); // pair Obejct. expand prototype
  • Shocould. js: Another open source contribution of corner stone
  • Except CT. js: another assertion library in the BDD style. Based on shocould. js, It is a mini version of BDD library.
  • Assert (core module of node): The asserted module that can be used in node

2.3 mock Library

Let's talk about the need for mock: the units to be tested depend on external modules. These dependent modules have some characteristics, such as uncontrollable, high implementation costs, and operational risks, you cannot directly use the dependent module. In this case, you need to mock it, that is, the module with forged dependency. For example, when XMLHttpRequest is used, it is necessary to simulate the situation where the http statusCode is 404. In this case, it is very difficult to implement the test through mock.

  • Sinon. js: the mock library that is currently the most used. It can be divided into spies, stub, fake XMLHttpRequest, Fake server, and Fake time. It can be selected based on different scenarios.

2.4 test runner

  • Karma: sets the framework, environment, source file, and test file required for the test. After configuration, you can easily perform the test.
3. Implementation principle of unit test technology 4. How to Write unit test cases

4.1 Principles

  • When testing code, only test is taken into account, and internal implementation is not considered.
  • Simulate the reality as much as possible. The closer the reality is, the better.
  • Fully consider the data Boundary Conditions
  • Focuses on key, complex, and core code tests
  • Use AOP (beforeEach and afterEach) to reduce the number of test codes and avoid useless functions.
  • Combining testing and function development helps design and code Reconstruction

4.2 TDD

To put it simply, write test first and then write function implementation. TDD aims to guide Functional Development through test cases, so that developers can first look at the needs from a global perspective. You can view the Wikipedia for specific definitions;

Personally, TDD is not a technology, but a guiding ideology for development. In the current Internet development environment, it is difficult for business development to achieve TDD development. First, it requires more time to write unit test cases. Second, it requires a good understanding of business needs; third, developers are required to have strong code design capabilities. However, TDD can be used well when we write components, tool methods, and class libraries.

4.3 BDD

Behavior-driven development requires more people to participate in software development and encourages developers, QA, and related business personnel to collaborate with each other. BDD is driven by business value and understands applications through user interfaces (such as GUI. For details, see Wikipedia.

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.