iOS Code Coverage test Tool

Source: Internet
Author: User

Based on lcov-1.11: Codecoverage4ios

Read Catalogue

    • Environment preparation
    • Xcode project configuration
    • Build and install programs
    • Collect Code Coverage
    • Filter results
    • Merge multiple coverage.info? into coverage report:
    • Reference documents

IOS Code Coverage Test Tool.

iOS Code Coverage test tool based on lcov-1.11, for use with iOS real-computer and simulator.

Environment preparation

Mac OS X : 10.8.5+ recommendation 10.9

Xcode : 5.0+ recommendation 6.1

Back to top Xcode project configuration
  1. Copy the Codecoverage4ios project to the main project root directory, which is the directory where ${your_proj.xcworkspace} resides

  2. Set the global variable nt_coverage=1in Xcode for code coverage switch control, such as iOSProj —> TARGTS -> MyApp -> Build Settings -> Preprocessor Macros -> Debug adding nt_coverage=1 in the configuration path

  3. The following configuration is done on the main engineering and dependency engineering in Build Settings :

    • Generate Debug Symbols configured as Yes
    • Generate Test Coverage Files configured to Yes
    • Instrument Program Flow is configured as Yes

    For example:iOSProj —> TARGTS -> MyApp -> Build Settings -> Generate Debug Symbols

    The above configuration recommendations are configured to Yes only under debug, to avoid affecting release packaging

  4. Add the following code to the APPDELEGATE.M:

    ?
    12345678910111213141516 - (void)applicationDidEnterBackground:(UIApplication *)application{    ...    #if NT_COVERAGE        #if !TARGET_IPHONE_SIMULATOR            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);            NSString *documentsDirectory = [paths objectAtIndex:0];            setenv("GCOV_PREFIX", [documentsDirectory cStringUsingEncoding:NSUTF8StringEncoding], 1);            setenv("GCOV_PREFIX_STRIP", "13", 1);        #endif        extern void __gcov_flush(void);        __gcov_flush();    #endif    ...}

    Called to generate the. gcda file when the program is pulled into the background, the __gcov_flush() code coverage is recorded in this file, the note __gcov_flush() can be called repeatedly, and is recorded as an append write.

      • IOS Simulator: . GCDA files are generated to the default path ~/Library/Developer/Xcode/DerivedData/iOSProj-cndbgdtazzzhaebuyvgjsqmkvwdr/Build/Intermediates/MyApp.build/Debug-iphonesimulator/MyApp.build/Objects-normal/i386 • Next
      • IPhone: . GCDA files are generated under the corresponding app sandbox Document/${CURRENT_ARCH}/
  5. Add the execution script in Build phases :

    In the TARGTS -> MyApp -> Build Phases -> New Run Script Phase edit Run Script , addCodeCoverage4iOS/exportenv.sh

    Note : This configuration is required for both the main project and the dependency project, and the primary script execution path is relative, such as dependency engineering and master Engineering sibling directory, so you need to modify the script path to a relative path../CodeCoverage4iOS/exportenv.sh

Back to top build and install programs

Complete the above configuration to build the Xcode project and make sure that the envs.sh file in the Codecoverage4ios directory is deleted before using Xcode to install the app

Note : if only Build Command+B will also produce a. envs.sh file, it is recommended to check the deletion of envs.sh after the build succeeds, and Command+R to ensure that the envs.sh does not have the remaining content from the last build.

Back to top collect code coverage
    1. After the app is successfully installed, you can perform the appropriate test operation, then click the Home button After completing the operation, and the program will generate the. gcda file to the corresponding directory.
    2. Generate coverage reports:

      • iOS Simulator: If your test device is an iOS simulator, you can directly double-click ExecuteCodeCoverage4iOS/getcov
      • iphone: If the test device is an iphone real machine, you first need to copy it from Document/${CURRENT_ARCH} the sandbox . GCDA file to the CodeCoverage4iOS/gcda next, then executeCodeCoverage4iOS/getcov

      During execution CodeCoverage4iOS/getcov , the Coverage.info file is generated in the directory CodeCoverage4iOS/coverage and the final report is generated according to the Coverage.info file. PS: If you need to merge test results, you need to keep this file

      Test report Generation Path:CodeCoverage4iOS/report/index.html

Back to top filter results

If you need to filter the collected coverage results, you can edit CodeCoverage4iOS/getcov the functions inexclude_data()

?
123456 exclude_data(){    LCOV --remove $COVERAGE_INFO_DIR/${LCOV_INFO} "Developer/SDKs/*" -d "${OBJ_DIR}" -o $COVERAGE_INFO_DIR/${LCOV_INFO}    LCOV --remove $COVERAGE_INFO_DIR/${LCOV_INFO} "main.m" -d "${OBJ_DIR}" -o $COVERAGE_INFO_DIR/${LCOV_INFO}    # Remove other patterns here...}
Back to the top merge multiple coverage.info? into coverage report:
    1. Place the Coverage.info file all CodeCoverage4iOS/coverage down, asCoverage1.info、Coverage2.info、Coverage3.info
    2. Perform CodeCoverage4iOS/mergecov a post-build consolidated report
Go back to the top of the bibliography

Https://developer.apple.com/library/ios/qa/qa1514/_index.html

http://qualitycoding.org/xcode-code-coverage/



Project home:http://www.open-open.com/lib/view/home/1417661077558

iOS Code Coverage test Tool

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.