Tencent director talks about unit testing

  Preface

  I have been in contact with programming for a long time, but every time there is an error in the code, it is nothing more than to choose debug or breakpoint debugging. In fact, it is not particularly convenient. With the increase in the amount of code and continuous reconstruction, in fact Error checking will become more and more troublesome, so this has always been a pain point. Is there a better way to help us avoid this problem and make our development process more secure? The answer is yes, it is unit testing.

  What is unit testing

  Unit testing is essentially code, and the difference from ordinary code is that it is the code to verify the correctness of the code. The popular meaning is: Unit test is the special code written by the developer to test whether the function of their ordinary code is correct. It doesn’t matter if you don’t understand this sentence, let’s give a simple example:

  Unit testing practice

  Different languages ​​have different unit testing frameworks, but their ideas are common. This article mainly explains the unit testing framework of javascript's mocha, hoping to let everyone have a more intuitive understanding of unit testing through the practice of simple unit testing.

  In Node.js, the most popular unit test combination is mocha+chai. Mocha is a testing framework, chai is an assertion library, so collectively called "Matcha". The main features of Mocha are: support for asynchronous test cases, such as Promise; support for code coverage coverage test reports; support for configuring different tests (such as assertion libraries), and so on.

  This article uses: Mocha+chai.

  ●The installation steps are as follows:

  ●First, we first write a functional script, which is consistent with the writing of ordinary scripts:

  ●Then you can start to write unit test scripts. Generally speaking, the test script has the same name as the source script, but the suffix name must be .test.js, for example, the test script of index.js is named index.test.js, and the test script is usually separate Put it in the test folder, the following is the code of the test script:

  It can be seen that the test code is very close to natural language. Even if we have not touched this framework, we can roughly see what the test code is doing. There is no grammatical difficulty. In order to better understand, it is better to carry out the grammar A note:

  There will be one or more describe blocks in the test script, and each describe block contains one or more it blocks

  The describe block is called a "test suite", which represents a set of related tests. It is also a function. The first parameter represents the name of the test suite, and the second parameter is the function that is actually executed.

  The it block is called a "test case", which means a single test. It is the smallest unit of test. It is also a function. The first parameter is the name of the test case, and the second parameter is the function that is actually executed.

  4. Assertion: expect(getBiggerNumber(2,2)).to.be.equal(2) in the above test script is a simple assertion, which is used to determine whether the actual execution result of the source code is consistent with the expected result. If an error is reported, the test is passed if it is consistent. The it block should contain assertions for testing. Assertions are implemented with an assertion library. Mocha itself does not have an assertion library, so an assertion library needs to be introduced. For example: varexpect=require('chai').expect, there are many kinds of assertion libraries, Mocha does not limit which one is used, the assertion library introduced by the above code is chai, and the expect assertion style to use it is specified, the following briefly introduces some Commonly used assertions:

  ●Basic use of mocha:

  1. The mocha command is followed by the test script path and file name (multiple test scripts can be followed): mochafile1(file2,file3):

  2. Mocha runs the test scripts in the test subdirectory by default. This is why it was said that the test script is usually placed in the test directory, and then no parameters are required to execute mocha:

  3. When mocha has no parameters, only the test scripts in the first-level directory of test are tested by default. If there is a folder in test and test scripts are placed in the folder, these test scripts will not be executed. If you want to test So the test scripts are executed, no matter at which level, you need to add the --recursive parameter:

  4. mocha can change the test report format, the default is spec format, which is the form above, which uses mochawesome

  Module, can generate beautiful HTML format report.

  How to use:

  The test result report is generated in the mochaawesome-reports subdirectory

  Test coverage

  Although we have written unit tests, can we guarantee that we have tested all the branches of the methods we want to test? Here is a metric called "code coverage", which includes four aspects:

  ● Statement coverage (statementcoverage): Is every statement executed?

  ●Branch coverage (branchcoverage): Is every if code block executed?

  ●Function coverage (functioncoverage): Is every function called?

  ●Line coverage (linecoverage): Is every line executed?

  The following Istanbul is that javascript is the code coverage tool:

  1. Installation

  npminstall-gistanbul

  2. Coverage test

  Change the previous source script slightly:

  Use **istanbulcover command to get the coverage:

  Briefly explain the results: index.js has 5 statements and 4 sentences are executed; there are 2 branches, and 1 is executed; there is 1 function, and 1 is executed; there are 5 lines of code, and 4 lines are executed

  This command also generates a coverage subdirectory. The coverage.json file contains the original coverage data. Coverage/lcov-report is a coverage report that can be opened in the browser. It contains detailed information and which codes are not. Covered to.

  3. Combine with mocha

  In fact, when we are testing, istanbul is often combined with the test framework. Taking mocha as an example to test the test scripts written before, generally use the command istanbulcover_mocha, but under the windows platform, you need to use istanbulcovernode_modules/mocha/bin/_mocha, which is required Pay special attention, the results are as follows:

  It can be seen that the results include both test results and test coverage. We can clearly see our test results and whether the test results have tested all conditions completely.

  postscript

  I have not really been in contact with unit testing for a long time, so my understanding of unit testing is not very deep, so this document is very simple, and there may be some deviations in understanding. Welcome to correct me; but I also I really feel the benefits of unit testing, which allows us to jump out of the traditional bad experience of writing code that is unsure, and have a clear understanding of the correctness of each step of our code, which greatly reduces our troubleshooting The wrong time improves efficiency; at the end of the article, I will also list some more detailed articles and official articles about unit testing and mocha that I have seen, so that everyone can have more understanding of unit testing and unit testing framework. Welcome Everyone and I communicate with each other and make progress together.


Those who are interested in software testing can also follow my official account: Programmer Erhei, focusing on software testing sharing, mainly sharing testing foundation, interface testing, performance testing, automated testing, TestOps architecture JmeterLoad, Runner, Fiddler, MySql, Linux , Resume optimization, interview skills, and actual video materials for large-scale test projects. Those who are interested can pay attention to it

Share the wonderful content with your friends

Guess you like

Origin blog.csdn.net/m0_52650621/article/details/113405234