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
[[127491]] Xu Kun, CTO of Testin Cloud Testing. H...
After launching the new Desire 8 series for the pr...
Produced by | Science Popularization China Author...
In modern life Sitting is a posture we are accust...
In the United States, the average annual salary o...
A few days ago, an expert revealed that Xiaohongs...
"500 Short Stories Collection" is a fan...
Some time ago, advertisements for BMW, vivo, Coke...
Rong Xuzheng still remembers that the day he lear...
As an innovative financial organization model, P2...
When we write soft articles for public accounts ,...
Due to its low technical barriers and easy dissem...
I have always believed that no matter what you do...
Apple has scheduled the much-anticipated iPhone 7 ...