Why is it great in hotel business? What have I been through? Ctrip, Tencent, what does unit test test?

Why is it great in hotel business? What have I been through? Ctrip, Tencent, what does unit test test?

Original: Li Sheng

What does unit testing measure?

Of course, module module in test code

Why is it great in hotel business? What have I been through? Ctrip, Tencent, what does unit test test?

But how do you do a modular unit test?

The unit test cases of some project teams are exactly same as test suites of other levels of software testing - same functional tests

Same performance test and interface


After writing code, make sure syntax of code is correct by compiling it;

Using unit tests to check semantics of code

And a unit test, which is almost identical to configuration element test

Does this also make code semantically correct?


Good unit test design should not overlap with design of other test-level test suites

It should include following aspects of test content:

  1. Test unit interface parameters

Checking module interface parameters should include

Check if number of entered actual parameters matches number of formal parameters and if attributes match

Check if actual parameters given when calling other modules match number of formal parameters of called module, and if attributes match

Verify that number, attributes, and order of parameters used when calling a predefined function are correct

Check for extraneous parameters

Check if read-only setting has been changed

Check if global variable definitions are consistent in each module

Check if certain constraints are being passed as parameters

If a module includes external input and output, it must also include:

Check if file attributes and file OPEN/CLOSE statement are correct

Check if input/output format matches definition in code

Check if buffer size is correct.

Check if file was opened before use and if end-of-file was handled

Check if I/O errors are handled and if there are text errors in output message

  1. Test module data structure

Tests for modular data structures should include

Checking for inappropriate or incompatible data type declarations

Check if variable exists without initial value

Checking whether a variable has been initialized correctly or has a default value

Checking for bad variable names (mistakes or bad truncation)

Checking for deviations

  1. Test module execution path

The main task of unit testing is to make sure that each path in a unit test is executed at least once

These path tests should detect errors caused by miscalculations, bad comparisons, and bad control flow

Common mistakes include

Misunderstanding or using wrong prioritheta operator

Mixed Data Operations

Invalid variable initial value

Insufficient data accuracy

Invalid expression character

Other tests include

Checking for incorrect use of logical operators or precedence

Check if they are theoretically equal but not actually equal due to computer precision errors

Checking for errors in comparison operations or variables

Check if loop termination condition is reasonable

Is there a bug that can't exit when testing iterative divergence

Check for cases where loop variable has been incorrectly changed

The following issues should be considered for error handling path

Check if output error message is hard to understand

Check if recorded error matches actual error

Checking for incorrect exception handling.

Check if error message can provide enough information about positioning error

In addition

Unit testing should also focus on maintainability of software. Modular module maintainability test to be carried out

Code structure

Modularity, code comment quality, standards compliance, understandability, documentation, etc.

In short

Unit testing makes sure that semantics of code matches our expectations

Unit test cases cannot be exactly same as test cases at other levels, such as an integration test and a configuration item test