Option to mark test as risky when it does not contribute to code coverage · Issue #4501 · sebastianbergmann/phpunit · GitHub
Skip to content

Option to mark test as risky when it does not contribute to code coverage #4501

Closed

Description

@sebastianbergmann

Suggested by @spriebsch:

When code coverage analysis is active then a test that is not annotated with @coversNothing and does not contribute to code coverage should be marked as risky.

Motivation: We frequently see "tests" like this:

<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class ExampleTest extends TestCase
{
    public function testSomething(): void
    {
        $this->assertTrue(true);
    }
}

This is done in order to prevent the test from being marked as risky because it does not assert anything and/or to create the false impression that a project has tests.

Activity

  1. spriebsch commented on Oct 30, 2020

    @spriebsch

    Thanks for being my GitHub proxy, again ;-)

  2. R4c00n commented on Nov 2, 2020

    @R4c00n

    Hi @sebastianbergmann and @spriebsch,
    I think this is a nice idea! I would love to help and made a quick prove of concept.
    Is this the place and way you thought of, or would you tackle this completely different? https://github.com/sebastianbergmann/phpunit/compare/master...R4c00n:risky-because-no-coverage?expand=1

    As I said, this is just a quick POC. Still missing proper tests and configuration options and stuff.

  3. changed the title [-]Option to mark test as risky when it does not generate code coverage[/-] [+]Option to mark test as risky when it does not contribute to code coverage[/+] on Apr 12, 2026
  4. sebastianbergmann commented on Apr 12, 2026

    @sebastianbergmann
    OwnerAuthor

    I will implement requireCoverageContribution (XML) and --require-coverage-contribution (CLI) for PHPUnit 13.2.

    When code coverage analysis is active and this risky test check is enabled, PHPUnit will mark a test as risky when it does not execute any line of the code it is expected to cover.

    This helps identify tests that create the false impression of being useful, for example:

    #[CoversClass(Foo::class)]
    final class FooTest extends TestCase
    {
        public function testSomething(): void
        {
            $this->assertTrue(true);
        }
    }

    This test declares that it covers Foo, passes because of its trivial assertion, and would not be flagged by the existing "did not perform any assertions" check. But it never actually executes any code in Foo, so it does not contribute to code coverage.

    Tests annotated with #[CoversNothing] will not be affected by this new risky test check, as they explicitly declare that they are not expected to contribute to code coverage.

  5. locked as resolved and limited conversation to collaborators on May 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions