The beautiful solution to this problem (which I hypothesise the _coverageMarker() line in Dart does) is to track code coverage on the test-by-test basis. That is, when running a test, verify that specific lines of code were covered by this test.

I’ve put together a small Rust library to do this, called uncover . It provides two macros: covered_by and covers .

The first macro is used in the code under test, like this:

1 2 3 4 if ! self .keys .is_empty () { covered_by! ( "table_with_two_names" ); panic! ( "table header is already specified, can't reset to {:?}" , key ) }

The second macro is used in the corresponding test:

1 2 3 4 5 6 7 8 9 10 11 #[test] fn table_with_two_names () { covers! ( "table_with_two_names" ); let f = Factory :: new (); check_panics (|| { f .table () .with_name ( "foo" ) .with_name ( "bar" ) .build (); }) }

If the block where covers is used does not cause the execution of the corresponding covered_by line then the error will be raised at the end of the block.

Under the hood, this is implemented as a global HashMap<String, u64> which counts how many times each line was executed. So covered_by! increments the corresponding count, and covers! returns a guard object that checks in Drop that the count was incremented. It is possible to disable these checks at compile time. And yes, the library actually exposes a macro which defines macros :)

I haven’t had a chance to apply this technique in large projects (and it is less useful for smaller ones), but it looks very promising.

It’s now easy to navigate between code and tests: just ripgrep the string literal (or write a plugin for this for your IDE). You will be able to find the test for the specific if-branch! This should be especially handy for new contributors.

If this technique is used pervasively, you also get an idea about the overall test coverage.

During refactorings, you became aware of tests which might be affected. Moreover, because coverage is actually checked by the tests themselves, you’ll notice if some test stop to exercise the code it was intended to check.