Cmockery Library Detailed

Source: Internet
Author: User

1, cmockery Library source code to compile links download link: https://code.google.com/p/cmockery/downloads/list download needs FQ, no conditions can be directly on GitHub to find a more complete version.
$./configure$ make clean && make$ make install prefix=~/share/code/cmockery$ tree ~/share/code/ cmockeryinstall├──include│   └──google│       └──cmockery.h├──lib│   ├──libcmockery.a│   ├── Libcmockery.la│   ├──libcmockery.so, libcmockery.so.0.0.0│   ├──libcmockery.so.0 libcmockery.so.0.0.0│   └──libcmockery.so.0.0.0└──share    └──doc        └──cmockery-0.11            ├──authors            ├──changelog            ├──copying            ├──index.html            ├──install            ├──news            └──readme 6 directories, 13 Files

The post-installation directory tree, including the header files, provides both static and dynamic libraries, as well as header files, as well as some user manuals.

2, the use of Cmockery library cmockery using less than 2000 lines of code, the implementation of the code unit Test framework. First, take a look at the official example and summarize the functions of the Cmockery Library: (1) Run the test-see RUN_TESTS.C Calculator_test.ccmockery provides two ways to add a test case:
    Unit_test (f)    Unit_test_setup_teardown (test, Setup, teardown)

You can add the Setup function and the teardown function separately by using Unit_test_setup_teardown, you can initialize it before test, release the resource after testing ends, and you can use NULL to represent the empty function.

Test Framework Flow:
    Const UnitTest tests[] = {        unit_test (f),        Unit_test_setup_teardown (test, Setup, teardown),    };    Run_tests (tests);
The test results are shown as follows: Test_func:starting Testtest_func:test completed successfully. All 1 tests passed (2) assertion test--see assert_module_test.c assert_macro_test.c using the Assert function provided by Cmockery:
//If Unit Testing is enabled for override assert with Mock_assert ().#ifUnit_testingextern voidMock_assert (Const intResultConst Char*ConstExpressionConst Char*ConstFileConst intLine );#undefAssert#defineASSERT (expression) \Mock_assert ((int) (expression), #expression, __file__, __line__);#endif //unit_testing
The Cmockery library provides the following assertion functions:
When you test your code, you can use assertions appropriately and extensively to ensure the reliability of your code. An assertion error will print out an error prompt, as in the following example: Get_status_code_string_test:starting test "Connection dropped"! = "Connection timed out" error:src/ example/assert_macro_test.c:29 failure!get_status_code_string_test:test failed.string_to_status_code_test: Starting Test2! = 1 (3) Memory request and release test--see allocate_module.c allocate_module_test.c also need to use the malloc, cmockery, and free functions provided by calloc
#ifUnit_testingextern void* _TEST_MALLOC (Constsize_t size,Const Char* File,Const intLine );extern void* _TEST_CALLOC (Constsize_t number_of_elements,Constsize_t size,Const Char* File,Const intLine );extern void_test_free (void*ConstPtrConst Char* File,Const intLine );#definemalloc (size) _test_malloc (size, __file__, __line__)#defineCalloc (num, size) _test_calloc (num, size, __file__, __line__)#defineFree (PTR) _test_free (PTR, __file__, __line__)#endif //unit_testing
When the memory is improperly manipulated or the malloc error prints an error message, as shown in the following example: Buffer_overflow_test:starting testguard block of 0x088e90e8 size=4 allocated by src/ Example/allocate_module.c:41 at 0x088e90ec is corrupterror:src/example/allocate_module.c:43 Failure!buffer_overflow    _test:test Failed.1 out of 1 tests failed!  Buffer_overflow_testblocks allocated ... 0x088e90b0:src/example/allocate_module.c:41guard block of 0x088e90e8 size=4 allocated by Src/example/allocate_ Module.c:41 at 0x088e90ec is corrupterror:src/cmockery.c:1463 failure! (4) Simulation test--see product_database_test.c customer_database_test.c by Expect_value and check_expected, you can determine if the value of the incoming function is the desired value, and will _return and mocks are the corresponding relationships, Will_return will put values into the queue, and each call to the mock will fetch the value of the front end of the queue.
intTestintValueChar*string) {check_expected (value); Check_expected (string); return(int) mock ();}voidTest_for_mock (void**State ) {expect_value (test, value,1); Expect_string (Test,string,"Test"); Will_return (Test,0x123456); Assert_int_equal (Test (1,"Test"),0x123456);}

Cmockery Library Detailed

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.