如何在iOS模拟器或设备运行时生成代码覆盖率(无需单元测试)
Hey there! I totally feel your pain—when you're cranking out app updates left and right, writing tons of unit tests just isn't feasible. Tracking code coverage during your manual release tests is a smart move, and luckily iOS makes this pretty straightforward. Here's how to generate code coverage for your manual testing sessions on simulators or physical devices:
First, you need to tell Xcode to collect coverage data when you run your app (not just when running unit tests):
- Open your project in Xcode, then click the scheme dropdown next to the run button (it shows your app's name) and select Edit Scheme...
- Switch to the Run tab (this is key—don't use the Test tab unless you're running unit tests)
- Navigate to the Options section (scroll down if you don't see it right away)
- Check the box labeled Code Coverage
- Hit Close to save your changes
Now launch your app with coverage tracking enabled:
- Connect your physical device (if testing on hardware) or select a simulator from the device dropdown
- Press
Command + Rto run your app—Xcode will start collecting coverage data in the background - Go through your full manual test suite as you normally would: tap buttons, navigate screens, trigger edge cases, etc. Every line of code your app executes during this time will be logged.
Once you're done testing, stop the app (click the stop button in Xcode or press Command + .), then:
- Open the Report Navigator (the last icon in the left sidebar—it looks like a bar chart)
- Find the most recent run session (it'll be named after your app and timestamped)
- Expand that session and select Coverage
- You'll see a detailed breakdown:
- Coverage percentages per target, file, and even individual lines of code
- Red-highlighted lines mean they weren't touched during your manual tests
- You can sort files by coverage percentage to quickly spot untested code paths
If you want to share or archive the coverage results:
- In the Coverage report, click the Export button (square with an upward arrow) in the top-right corner
- Choose where to save the data. Xcode will generate an
.xcresultbundle with full coverage details, and you can also export it as a CSV or HTML report for easier sharing.
For more granular, real-time coverage insights while you test, use Xcode's Instruments tool:
- Open Instruments via Xcode > Open Developer Tool > Instruments
- Select the Code Coverage template from the list
- Choose your app target and device/simulator, then click Record
- As you perform manual tests, Instruments will show you exactly which methods and lines are being executed in real time
- When finished, stop recording to review the full coverage report, or export it for later analysis
Quick Notes to Keep in Mind
- Always use a Debug build—Release builds have optimizations that can make coverage data inaccurate or incomplete
- If testing on a physical device, make sure it's connected, unlocked, and recognized by Xcode
- Coverage data is tied to your current build—if you make code changes, you'll need to rebuild and rerun your manual tests to get updated results
内容的提问来源于stack exchange,提问作者Michael Ozeryansky

