IntelliJ 2020.1中能否对JUnit Test Suite执行代码覆盖率检测?
Fixing JUnit Test Suite Code Coverage in IntelliJ 2020.1
Hey there! Let's work through why your Test Suite isn't generating code coverage, and how to fix it using that Code Coverage tab you're looking at.
First: Yes, That Code Coverage Tab Does Need a Quick Check
The tab you're viewing is crucial for ensuring coverage gets tracked for your Test Suite. Here's what to verify:
- Enable Coverage Tracking: Make sure the Code coverage enabled checkbox is checked (this is often the culprit—sometimes it doesn't get enabled by default for Test Suite configurations).
- Set the Coverage Scope: Double-check the "Packages and classes" section. You'll want to include your main source code directories (e.g.,
src/main/java) and exclude test directories (e.g.,src/test/java) to avoid skewed results. Use the+/-buttons to adjust this as needed. - Choose Coverage Mode: Pick between Sampling (faster, but less precise for short-running tests) or Tracing (slower, but fully accurate). For most Test Suites, Tracing is the better choice to get reliable coverage stats.
Run the Test Suite the Right Way
Even with correct config, you need to launch the Test Suite with coverage explicitly:
- Instead of clicking the regular "Run" button, right-click your Test Suite class (or its entry in the Run Configurations menu) and select Run [Test Suite Name] with Coverage. This triggers the coverage collection that's configured in that tab.
Troubleshooting If Coverage Still Doesn't Show Up
If you've checked the config and ran it correctly but still see no coverage:
- Clear IntelliJ Caches: Go to
File > Invalidate Caches... > Invalidate and Restart. Corrupted cache can sometimes block coverage data from being generated or displayed. - Verify Test Suite Configuration: Ensure your Test Suite class properly includes all the test classes you want to run. A missing or misconfigured suite won't generate coverage for the tests you expect.
- Check for Exclusions: Double-check the Code Coverage tab's "Exclude" list—if your main source code is accidentally excluded, no coverage will be recorded.
内容的提问来源于stack exchange,提问作者robbie70
相关产品推荐
相关产品推荐

