PHP code example of allure-framework / allure-phpunit

1. Go to this page and download the library: Download allure-framework/allure-phpunit library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

allure-framework / allure-phpunit example snippets




return [
    // Path to output directory (default is build/allure-results)
    'outputDirectory' => 'build/allure-results',
    'linkTemplates' => [
        // Class or object must implement \Qameta\Allure\Setup\LinkTemplateInterface
        'tms' => \My\LinkTemplate::class,
    ],
    'setupHook' => function (): void {
        // Some actions performed before starting the lifecycle
    },
     // Class or object must implement \Qameta\Allure\PHPUnit\Setup\ThreadDetectorInterface
    'threadDetector' => \My\ThreadDetector::class,
    'lifecycleHooks' => [
        // Class or object must implement one of \Qameta\Allure\Hook\LifecycleHookInterface descendants.
        \My\LifecycleHook::class,
    ],
];

namespace Example\Tests;

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

#[DisplayName("Human-readable test class title")]
class SomeTest extends TestCase
{
    #[DisplayName("Human-readable test method title")]
    public function testCaseMethod(): void
    {
        //Some implementation here...
    }
}

namespace Example\Tests;

use PHPUnit\Framework\TestCase;
use Qameta\Allure\Attribute\Description;

#[Description("Detailed description for **test** class")]
class SomeTest extends TestCase
{
    #[Description("Detailed description for <b>test class</b>", isHtml: true)]
    public function testCaseMethod(): void
    {
        //Some implementation here...
    }
}

namespace Example\Tests;

use PHPUnit\Framework\TestCase;
use Qameta\Allure\Attribute\Severity;

class SomeTest extends TestCase
{
    #[Severity(Severity::MINOR)]
    public function testCaseMethod(): void
    {
        //Some implementation here...
    }
}

namespace Example\Tests;

use PHPUnit\Framework\TestCase;
use Qameta\Allure\Allure;
use Qameta\Allure\Attribute\Parameter;

class SomeTest extends TestCase
{
    #[
        Parameter("param1", "value1"),
        Parameter("param2", "value2"),
    ]
    public function testCaseMethod(): void
    {
        //Some implementation here...
        Allure::parameter("param3", $someVar);
    }
}

namespace Example\Tests;

use PHPUnit\Framework\TestCase;
use Qameta\Allure\Attribute\Feature;
use Qameta\Allure\Attribute\Story;

#[
    Story("story1"),
    Story("story2"),
    Feature("feature1"),
    Feature("feature2"),
    Feature("feature3"),
]
class SomeTest extends TestCase
{
    #[
        Story("story3"),
        Feature("feature4"),
    ]
    public function testCaseMethod(): void
    {
        //Some implementation here...
    }
}

namespace Example\Tests;

use PHPUnit\Framework\TestCase;
use Qameta\Allure\Allure;

class SomeTest extends TestCase
{

    public function testCaseMethod()
    {
        //Some implementation here...
        Allure::attachment("Attachment 1", "attachment content", 'text/plain');
        Allure::attachmentFile("Attachment 2", "/path/to/file.png", 'image/png');
        //Some implementation here...
    }
}

namespace Example\Tests;

use PHPUnit\Framework\TestCase;
use Qameta\Allure\Allure;
use Qameta\Allure\Attribute\Parameter;
use Qameta\Allure\Attribute\Title;
use Qameta\Allure\StepContextInterface;

class SomeTest extends TestCase
{

    public function testCaseMethod(): void
    {
        //Some implementation here...
        $x = Allure::runStep(
            #[Title('First step')]
            function (StepContextInterface $step): string {
                $step->parameter('param1', $someValue);
                
                return 'foo';
            },
        );
        Allure::runStep([$this, 'stepTwo']);
        //Some implementation here...
    }

    #[
        Title("Second step"),
        Parameter("param2", "value2"),
    ]
    private function stepTwo(): void
    {
        //Some implementation here...
    }
}
xml
<extensions>
    <bootstrap class="Qameta\Allure\PHPUnit\AllureExtension">
          <!-- Path to config file (default is config/allure.config.php) -->
          <parameter name="config" value="config/allure.config.php" />
    </bootstrap>
</extensions>