question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Assert test coverage

See original GitHub issue

Quoting @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:

  1. 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.
  2. The gathered data could be placed into the store and then made available in handy AssertJ-esque assertion object.
  3. 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:open
  • Created 3 years ago
  • Reactions:2
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
verhascommented, Dec 19, 2020

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.

1reaction
lukasedercommented, Dec 14, 2020

That last one kind of sounds like could be solved with a spy from Mockito?

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.

Also, this only works (if it works) if your set of tests is in a single class (which might be problematic).

In my case, I’d like the entire test suite to be run in this mode. Simple to do with an agent…

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found