Assert test coverage
See original GitHub issueQuoting @lukaseder from Twitter:
Is there a simple JUnit based tool to check for code coverage by a test class X for a given tested class Y? E.g. a rule specifying that if I add a method m matching some pattern P to Y without adding a test to X, X should fail.
Some thoughts:
- During each test class’ execution, we need to gather coverage data. I’d prefer using a library for that but we could do it ourselves as well. Whichever way we get it done, it mustn’t impact performance if the feature isn’t used.
- The gathered data could be placed into the store and then made available in handy AssertJ-esque assertion object.
- Fittingly annotated methods that are executed at the after all extension point could get such an instance injected and use it to assert desired test coverage.
This could look as follows:
@Coverage(ProductionCode.class)
class MyTestCases
// [...] regular @Test methods
@CoverageTest
void allMethodsTested(CoverageAssertions assertThat) {
assertThat.allPublicMethodsCalled();
}
}
Steps 2. and 3. should be straightforward, the only challenge (to me) is 1.
Any thoughts?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:10 (4 by maintainers)
Top Results From Across the Web
How Do Assertions Impact Coverage-based Test-Suite ...
The experimental results show that both assertion coverage and assertion count are significantly correlated with the effectiveness (including the test-suite ...
Read more >Code coverage vs test coverage - codeburst
Test coverage is about the proportion of a story or feature covered by tests. Opinions on which is “better” do vary, but —...
Read more >Test Coverage: How to cover assertions? - java - Stack Overflow
So I've tested where it fails for both asserts and passes. If i'm not mistaken that covers all cases, but using the code...
Read more >Test class coverage using asserts - Salesforce Stack Exchange
asserts are nothing to do with coverage. they are to verify if your code is behaving as expected. In your case your class...
Read more >How Do Assertions Impact Coverage-Based Test-Suite ...
Abstract: Code coverage is the dominant criterion in test-suite reduction. Typically, most test-suite reduction techniques repeatedly remove tests covering ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I spent the last few days of my vacation writing a classloader
https://github.com/verhas/impostor
that may help you to solve this case. You never know what you inspire.
Run your test loaded by this classloader and configure the class implementing the expencive equals method to be impersonated by an impostor class that throws an AssertionError in case
equals()
is involved.This is not a JUnit feature, just a Q&D solution to your specific need.
You may also use AspectJ to run the test and get similar structure.
Unless I totally misunderstood something.
Interesting idea. First caveat, this is modular code, and it’s not public, so I cannot access the
T
type unless I use reflection. Perhaps there’s a modular hack for this, but it seems that reflection / instrumentation is cleaner, here. Interesting idea nonetheless.In my case, I’d like the entire test suite to be run in this mode. Simple to do with an agent…