Skip to content

Commit

Permalink
test method is correctly detectied with data provider (fixes #98)
Browse files Browse the repository at this point in the history
  • Loading branch information
remorhaz committed Nov 10, 2023
1 parent bd37424 commit 1eab991
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/Internal/TestLifecycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,26 @@ private function getCurrentTest(): TestInfo

private function buildTestInfo(string $test, ?string $host = null, ?string $thread = null): TestInfo
{
$dataLabelMatchResult = preg_match(
'#^([^\s]+)\s+with\s+data\s+set\s+(\#\d+|".+")\s+\(.+\)$#',
/** @var list<string> $matches */
$classAndMethodMatchResult = preg_match(
'#^(\S+)(.*)$#',
$test,
$matches,
);

/** @var list<string> $matches */
if (1 === $dataLabelMatchResult) {
$classAndMethod = $matches[1] ?? null;
$dataLabel = $matches[2] ?? '?';
} else {
$classAndMethod = $test;
[$classAndMethod, $dataSetInfo] = 1 === $classAndMethodMatchResult
? [$matches[1] ?? null, $matches[2] ?? null]
: [$test, null];
$dataLabelMatchResult = isset($dataSetInfo)
? preg_match(
'/^\s+with\s+data\s+set\s+(?:(#\d+)|"(.*)")$/',
$dataSetInfo,
$matches,
)
: false;
$dataLabel = 1 === $dataLabelMatchResult
? $matches[2] ?? $matches[1] ?? null
: null;
if ('' === $dataLabel) {
$dataLabel = null;
}

Expand Down
44 changes: 44 additions & 0 deletions test/report/Generate/DataProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Qameta\Allure\PHPUnit\Test\Report\Generate;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Qameta\Allure\Allure;
use Qameta\Allure\Attribute\DisplayName;

class DataProviderTest extends TestCase
{
#[DataProvider('providerNamed'), DisplayName('Test with named data set')]
public function testNamedDataSet(int $x, int $y): void
{
Allure::runStep(fn () => self::assertSame($x, $y));
}

public static function providerNamed(): iterable
{
return [
'Simple name' => [1, 1],
'' => [2, 2],
'"Double-quoted" name' => [3, 3],
"'Single-quoted' name" => [4, 4],
' ' => [5, 5],
];
}

#[DataProvider('providerListed')]
public function testListedDataSet(int $x, int $y): void
{
Allure::runStep(fn () => self::assertSame($x, $y));
}

public static function providerListed(): iterable
{
return [
[1, 1],
[2, 2],
];
}
}

0 comments on commit 1eab991

Please sign in to comment.