introduceRecently, I have been researching solutions for Swift-based code coverage detection solutions for my company's projects. I have tried hard and found the best practices. In this short article, I will introduce you to:
Practice using the command line Before we measure the code coverage of a complete App project, we need to create a simple Swift source code file and generate a Create a Swift file and include the following code: test ( ) Run the following command in the terminal: swiftc - profile - generate - profile - coverage - mapping hello .swift The options Then run the output binary: ./hello After the run is complete, execute xcrun llvm - profdata merge - sparse default .profraw - o hello .profdata Running the above command line in the terminal, we will get a new file named xcrun llvm - cov show ./hello - instr - profile = hello .profdata Now, we have learned the basic workflow of generating Swift code coverage reports. It seems that Swift source-based code coverage is not that difficult. However, the configuration of a complete Swift App project in Xcode is very different from the command line. Let's continue reading! Measuring Code Coverage for Swift App Projects in XcodeCreating a Swift Project Select Add If we try to compile now, we will get the following error message: To fix this, we have to enable code coverage for all targets: After enabling code coverage and running again, the project will build successfully. We learned that when the program exits, the compiler will write the original profile to the path specified by Although I set the same configuration in Build Settings, the default environment path in Xcode is empty. To solve this problem, we must create a new header file and declare some llvm C api functions for Swift to call. Calling C/C++ methods from SwiftSwift is a powerful language based on C/C++, which can call C/C++ methods directly. However, before we call the llvm C/C++ api, we must export the methods we need as a module. First, create a header file: Then, copy and paste the following code into the file: #ifndef PROFILE_INSTRPROFILING_H_ Create a // Actually we can’t create Build the project, then we can call llvm apis in Swift code. import UIKit Build and start the App and we will see the original configuration file path in the console. Finally, we have the original configuration file we need! 🎉 We can copy this file and the Mach-O (binary file) in the Swift App project to the temp directory so that we can check whether the configuration file can generate the correct report. Create a new Swift file: import Foundation Add the call to sqrt before the call to __llvm_profile_write_file in ViewController.swift. Then, build and run. print ( "√2=\( BasicMath ( ) .sqrt ( 2 ) )" ) Run the following command in the command line: mkdir TestCoverage We will see the final report. refer to
|
<<: Teach you step by step how to disable mobile phone updates using adb
>>: If you want to use the grid system well, you must master these eight tips
Produced by: Science Popularization China Author:...
Although wolves are basically harmless to humans ...
Through refined operations , tapping into the con...
There are two types of customization of Linyi Liq...
SEM delivery is quite simple in the early stages....
Xinxiang Mini Program Development Company, how mu...
Weibo was officially listed in the United States ...
[[233976]] As a programmer, finding a job can som...
Nokia sold its mobile phone business to Microsoft...
In the 2020 Snooker World Championship final, O...
I believe everyone is familiar with ZhanDuShu , w...
In order to better penetrate into various industr...
The first part [User Mind Insights] mainly discus...
As the largest information flow platform at prese...
On July 4, 2012, the European Organization for Nu...