Skip to content

Commit

Permalink
test method is correctly detectied with data provider (fixes #98, via #…
Browse files Browse the repository at this point in the history
  • Loading branch information
remorhaz authored Nov 10, 2023
1 parent bd37424 commit 1b800cd
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 31 deletions.
12 changes: 10 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,17 @@ jobs:
${{ matrix.composer-options }}

- name: Run tests
if: ${{ matrix.os != 'windows-latest' }}
if: ${{ matrix.os != 'windows-latest' && !contains(matrix.composer-options, '--prefer-lowest') }}
run: composer test

- name: Run tests (lowest)
if: ${{ matrix.os != 'windows-latest' && contains(matrix.composer-options, '--prefer-lowest') }}
run: composer test-lowest

- name: Run tests (windows)
if: ${{ matrix.os == 'windows-latest' }}
if: ${{ matrix.os == 'windows-latest' && !contains(matrix.composer-options, '--prefer-lowest') }}
run: composer test-windows

- name: Run tests (windows-lowest)
if: ${{ matrix.os == 'windows-latest' && contains(matrix.composer-options, '--prefer-lowest') }}
run: composer test-windows-lowest
23 changes: 23 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,52 @@
"scripts": {
"test-cs": "vendor/bin/phpcs -sp",
"test-unit": "vendor/bin/phpunit --coverage-text",
"test-unit-lowest": "vendor/bin/phpunit --configuration=phpunit.10.0.xml --coverage-text",
"clear-allure-results": "rm -rf ./build/allure-results",
"test-report": [
"@clear-allure-results",
"vendor/bin/paratest --processes=3 --configuration=phpunit.report.xml --testsuite=positive",
"vendor/bin/paratest --processes=3 --configuration=phpunit.report.xml --testsuite=negative; exit 0"
],
"test-report-lowest": [
"@clear-allure-results",
"vendor/bin/paratest --processes=3 --configuration=phpunit.10.0.report.xml --testsuite=positive",
"vendor/bin/paratest --processes=3 --configuration=phpunit.10.0.report.xml --testsuite=negative; exit 0"
],
"test-report-windows": [
"@clear-allure-results",
"vendor/bin/paratest --processes=3 --configuration=phpunit.report.xml --testsuite=positive",
"vendor/bin/paratest --processes=3 --configuration=phpunit.report.xml --testsuite=negative & exit 0"
],
"test-report-windows-lowest": [
"@clear-allure-results",
"vendor/bin/paratest --processes=3 --configuration=phpunit.10.0.report.xml --testsuite=positive",
"vendor/bin/paratest --processes=3 --configuration=phpunit.10.0.report.xml --testsuite=negative & exit 0"
],
"test-psalm": "vendor/bin/psalm --shepherd",
"test": [
"@test-cs",
"@test-unit",
"@test-report",
"@test-psalm"
],
"test-lowest": [
"@test-cs",
"@test-unit-lowest",
"@test-report-lowest",
"@test-psalm"
],
"test-windows": [
"@test-cs",
"@test-unit",
"@test-report-windows",
"@test-psalm"
],
"test-windows-lowest": [
"@test-cs",
"@test-unit-lowest",
"@test-report-windows-lowest",
"@test-psalm"
]
}
}
25 changes: 25 additions & 0 deletions phpunit.10.0.report.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
colors="true"
displayDetailsOnIncompleteTests="true"
displayDetailsOnSkippedTests="true"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerWarnings="true"
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerErrors="true"
defaultTestSuite="positive">
<testsuites>
<testsuite name="positive">
<directory>test/report/Generate</directory>
<exclude>test/report/Generate/NegativeTest.php</exclude>
</testsuite>
<testsuite name="negative">
<file>test/report/Generate/NegativeTest.php</file>
</testsuite>
</testsuites>
<extensions>
<bootstrap class="Qameta\Allure\PHPUnit\AllureExtension" />
</extensions>
</phpunit>
23 changes: 23 additions & 0 deletions phpunit.10.0.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
colors="true"
displayDetailsOnIncompleteTests="true"
displayDetailsOnSkippedTests="true"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerWarnings="true"
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerErrors="true"
defaultTestSuite="unit">
<testsuites>
<testsuite name="unit">
<directory>test/unit/</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
</phpunit>
8 changes: 7 additions & 1 deletion phpunit.report.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd"
colors="true"
displayDetailsOnIncompleteTests="true"
displayDetailsOnSkippedTests="true"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerWarnings="true"
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerErrors="true"
defaultTestSuite="positive">
<testsuites>
<testsuite name="positive">
Expand Down
17 changes: 13 additions & 4 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd"
colors="true"
displayDetailsOnIncompleteTests="true"
displayDetailsOnSkippedTests="true"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerWarnings="true"
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerErrors="true"
defaultTestSuite="unit">
<testsuites>
<testsuite name="unit">
<directory>test/unit/</directory>
</testsuite>
</testsuites>
<coverage>
<source>
<include>
<directory suffix=".php">src/</directory>
<directory>src/</directory>
</include>
</coverage>
<exclude>
<directory>vendor/</directory>
</exclude>
</source>
</phpunit>
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],
];
}
}
22 changes: 7 additions & 15 deletions test/report/Generate/NegativeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@

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

use PHPUnit\Event;
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
use PHPUnit\Framework\TestCase;
use Qameta\Allure\Allure;
use Qameta\Allure\Attribute\DisplayName;
use Qameta\Allure\PHPUnit\ExceptionDetailsTrait;
use RuntimeException;

use function trigger_error;

use const E_USER_WARNING;

class NegativeTest extends TestCase
{
use ExceptionDetailsTrait;
Expand Down Expand Up @@ -51,22 +55,10 @@ function () {
);
}

#[DisplayName('Test that emits warning is reported as broken')]
#[DisplayName('Test that emits warning is reported as broken'), DoesNotPerformAssertions]
public function testWarning(): void
{
/**
* @psalm-suppress InternalMethod
* @psalm-suppress InternalClass
* @psalm-suppress TooManyArguments
*/
Event\Facade::emitter()->testTriggeredWarning(
$this->valueObjectForEvents(),
"Test triggered warning",
__FILE__,
__LINE__,
false,
);
self::assertTrue(true);
trigger_error('"Test triggered warning"', E_USER_WARNING);
}

#[DisplayName('Skipped test is reported as skipped')]
Expand Down
1 change: 1 addition & 0 deletions test/unit/Event/EventTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ protected function createTestWarningTriggeredEvent(
'file',
1,
false,
false,
);
}
}

0 comments on commit 1b800cd

Please sign in to comment.