承接 allure-framework/allure-phpunit 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

allure-framework/allure-phpunit

Composer 安装命令:

composer require --dev allure-framework/allure-phpunit

包简介

Allure PHPUnit integration

README 文档

README

Latest Stable Version Build Type Coverage Psalm Level Total Downloads License

This an official PHPUnit adapter for Allure Framework - a flexible, lightweight and multi-language framework for writing self-documenting tests.

Table of Contents

What is this for?

The main purpose of this adapter is to accumulate information about your tests and write it out to a set of JSON files: one for each test class. Then you can use a standalone command line tool or a plugin for popular continuous integration systems to generate an HTML page showing your tests in a good form.

Examples

Please take a look at these example tests.

How to generate report

This adapter only generates JSON files containing information about tests. See wiki section on how to generate report.

Installation && Usage

Note: this adapter supports Allure 2.x.x only.

Supported PHP versions: 8.1-8.5.

In order to use this adapter you need to add a new dependency to your composer.json file:

{
    "require": {
	    "php": "^8.1",
	    "allure-framework/allure-phpunit": "^3"
    }
}

Then add Allure test listener in phpunit.xml file:

<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>

Config is common PHP file that should return an array:

<?php

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,
    ],
];

After running PHPUnit tests a new folder will be created (build/allure-results in the example above). This folder will contain generated JSON files. See framework help for details about how to generate report from JSON files. By default generated report will only show a limited set of information but you can use cool Allure features by adding a minimum of test code changes. Read next section for details.

Main features

This adapter comes with a set of PHP annotations and traits allowing to use main Allure features.

Human-readable test class or test method title

In order to add such title to any test class or test case method you need to annotate it with #[DisplayName] annotation:

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...
    }
}

Extended test class or test method description

Similarly you can add detailed description for each test class and test method. To add such description simply use #[Description] annotation:

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...
    }
}

Description can be added in Markdown format (which is default one) or in HTML format. For HTML simply pass true value for optional isHtml argument.

Set test severity

#[Severity] annotation is used in order to prioritize test methods by severity:

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...
    }
}

Specify test parameters information

In order to add information about test method parameters you should use #[Parameter] annotation. You can also use static shortcut if your marameter has dynamic value:

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);
    }
}

Map test classes and test methods to features and stories

In some development approaches tests are classified by stories and features. If you're using this then you can annotate your test with #[Story] and #[Feature] annotations:

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...
    }
}

You will then be able to filter tests by specified features and stories in generated Allure report.

Attach files to report

If you wish to attach some files generated during PHPUnit run (screenshots, log files, dumps and so on) to report - then you need to use static shortcuts in your test class:

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...
    }
}

In order to create an attachment simply call Allure::attachment() method. This method accepts human-readable name, string content and MIME attachment type. To attach a file, use Allure::attachmentFile() method that accepts file name instead of string content.

Divide test methods into steps

Allure framework also supports very useful feature called steps. Consider a test method which has complex logic inside and several assertions. When an exception is thrown or one of assertions fails sometimes it's very difficult to determine which one caused the failure. Allure steps allow dividing test method logic into several isolated pieces having independent run statuses such as passed or failed. This allows to have much cleaner understanding of what really happens. In order to use steps simply use static shortcuts:

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...
    }
}

The entire test method execution status will depend on every step but information about steps status will be stored separately.

allure-framework/allure-phpunit 适用场景与选型建议

allure-framework/allure-phpunit 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13.33M 次下载、GitHub Stars 达 66, 最近一次更新时间为 2014 年 05 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「testing」 「phpunit」 「report」 「steps」 「attachments」 「cases」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 allure-framework/allure-phpunit 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 allure-framework/allure-phpunit 我们能提供哪些服务?
定制开发 / 二次开发

基于 allure-framework/allure-phpunit 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 13.33M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 69
  • 点击次数: 27
  • 依赖项目数: 33
  • 推荐数: 1

GitHub 信息

  • Stars: 66
  • Watchers: 12
  • Forks: 34
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2014-05-04