Simple unit testing ideas

Source: Internet
Author: User


A project is composed of many modules. When we finish a module each time, we should try again to check whether the function is
The execution is correct. Then write the next module. Do not wait until all the modules are written and try again.

Difficult to locate (except for experts ).


Therefore, as a project engineer, writing a token trial is a major skill.
So how do I write a trial example of a notebook?

In fact, we use our own modules to check whether the execution results are consistent with the expected results.

For example, if you have written an add function and want to test it, we will write a test_add function.


# Include <stdio. h> int add (int A, int B) {return a + B;} int test_add (void) {int ret = 0; ret = add (1,1); If (Ret! = 2) {return 1;} ret = add (2, 2); If (Ret! = 4) {return 2;} ret = add (3, 3); If (Ret! = 6) {return 3;} return 0;} int main () {int ret = 0; ret = test_add (); If (Ret! = 0) {// here, based on the returned value, determine which sequence is incorrect. Printf ("test failed, ret = % d \ n", RET);} else {printf ("test OK! ");}}

Of course, we can also use some open-source trial code. Below are two simple language unit trial frameworks.

For example, cutest, a simple C unit worker test see the link http://pan.baidu.com/s/1hqeg7qO

Cunit: It is provided to users in the form of a static library. Users can directly connect to this static library when coding. It provides a simple unit testing framework and supports a wide range of asserted statements for frequently used data types. See link http://pan.baidu.com/s/1gd9WCgV



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.