Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--list-tests and --list-tests-xml CLI options do not report error when data provider method throws exception #5908

Closed
harentius opened this issue Jul 30, 2024 · 2 comments
Assignees
Labels
feature/data-provider Data Providers feature/test-runner CLI test runner type/bug Something is broken version/10 Something affects PHPUnit 10 version/11 Something affects PHPUnit 11

Comments

@harentius
Copy link

Q A
PHPUnit version 11.2.8 10.5.28 9.6.20
PHP version 8.2.7
Installation Method Composer

Summary

list-tests (list-tests-xml) command doesn't report any error if dataProvider throws an exception and just return smaller list of tests
As this command(s) often used for CI environment and parallel runs, skipping this exception leads to false positive CI result.

Current behavior

How to reproduce

  1. The test (based on the documentation example):
<?php declare(strict_types=1);
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

final class NumericDataSetsTest extends TestCase
{
    public static function additionProvider(): array
    {
       throw new \Exception();
       return [
            [0, 0, 0],
            [0, 1, 1],
        ];
    }

    #[DataProvider('additionProvider')]
    public function testAdd(int $a, int $b, int $expected): void
    {
        $this->assertSame($expected, $a + $b);
    }

    public function testAnother(): void
    {
        $this->assertSame(0, 0);
    }
}
  1. Run
    vendor/bin/phpunit tests --list-tests

Expected behavior

I would expect displaying an error and do not return 0 code.

@harentius harentius added the type/bug Something is broken label Jul 30, 2024
@sebastianbergmann sebastianbergmann changed the title list-tests (list-tests-xml) command doesn't show any error if dataProvider throws an exception --list-tests and --list-tests-xml CLI options do not report error when data provider method throws exception Jul 30, 2024
@sebastianbergmann sebastianbergmann added version/10 Something affects PHPUnit 10 version/11 Something affects PHPUnit 11 labels Jul 30, 2024
@sebastianbergmann sebastianbergmann self-assigned this Jul 30, 2024
@sebastianbergmann
Copy link
Owner

Simplified reproducing example:

Issue5908Test.php

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

final class Issue5908Test extends TestCase
{
    public static function provider(): array
    {
        throw new Exception('message');
    }

    #[DataProvider('provider')]
    public function testOne(int $value): void
    {
    }
}
$ ./phpunit --list-tests ~/Issue5908Test.php
PHPUnit 10.5.28-18-g98cbd0f7fc by Sebastian Bergmann and contributors.

Available test(s):
$ ./phpunit ~/Issue5908Test.php
PHPUnit 10.5.28-18-g98cbd0f7fc by Sebastian Bergmann and contributors.

Runtime:       PHP 8.3.9
Configuration: /usr/local/src/phpunit/phpunit.xml

There was 1 PHPUnit error:

1) Issue5908Test::testOne
The data provider specified for Issue5908Test::testOne is invalid
message
/usr/local/src/phpunit/src/Metadata/Api/DataProvider.php:174
/usr/local/src/phpunit/src/Metadata/Api/DataProvider.php:67
/usr/local/src/phpunit/src/Framework/TestBuilder.php:40
/usr/local/src/phpunit/src/Framework/TestSuite.php:517
/usr/local/src/phpunit/src/Framework/TestSuite.php:139
/usr/local/src/phpunit/src/TextUI/Configuration/TestSuiteBuilder.php:122
/usr/local/src/phpunit/src/TextUI/Configuration/TestSuiteBuilder.php:58
/usr/local/src/phpunit/src/TextUI/Application.php:373
/usr/local/src/phpunit/src/TextUI/Application.php:112

/home/sb/Issue5908Test.php:13

--

There was 1 PHPUnit test runner warning:

1) No tests found in class "Issue5908Test".

No tests executed!

@harentius
Copy link
Author

Thank you for solving it! 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/data-provider Data Providers feature/test-runner CLI test runner type/bug Something is broken version/10 Something affects PHPUnit 10 version/11 Something affects PHPUnit 11
Projects
None yet
Development

No branches or pull requests

2 participants