PHP质量工具系列之phpunit

安装

composer global require  --dev phpunit/phpunit

编写用咧

单元测试

以下是一个thinkphp6/8的示例,可根据实际情况修改,一般是放在项目目录的tests文件夹中,tests文件夹和public同级

<?php
declare (strict_types = 1);

namespace tests;

use app\controller\user\v1\User;
use PHPUnit\Framework\TestCase;
use think\App;
use app\Request;
use think\Response;

require_once __DIR__ . '/../vendor/autoload.php';

class UserTest extends TestCase
{
    private Request $request;
    private User $user;

    protected function setUp(): void
    {
        $app = new App();
        $app->http->run();

        $this->request = new Request();
        $this->user = new User($app);
    }

    public function testAdd()
    {
        $this->assertIsBool(true);
    }

    public function tearDown(): void
    {
        set_exception_handler(null);
        restore_error_handler();
    }
}

编写phpunit.xml

以下是一个示例,可根据实际情况修改,一般是放在项目目录中,和public同级
需要注意的是,如果您已有的phpunit.xml模板过低,可以使用以下命令进行升级

php vendor/bin/phpunit --migrate-configuration
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.0/phpunit.xsd"
         backupGlobals="false"
         colors="true"
         bootstrap="vendor/autoload.php"
         cacheDirectory=".phpunit.cache">
    <php>
        <!-- 设置显示所有错误和警告 -->
        <ini name="display_errors" value="On"/>
        <!-- 设置错误报告等级,E_ALL 包括了 E_WARNING -->
        <ini name="error_reporting" value="E_ALL"/>
    </php>
  <!-- 如果有多个测试套件,可以在这里添加多个testsuite标签 -->
  <testsuites>
    <testsuite name="Application Test Suite">
      <!-- 假设所有的测试类都放在tests目录下 -->
      <directory>test</directory>
      <!-- 如果有特定的测试文件或目录,可以单独列出,例如 -->
      <!-- <file>tests/Feature/ExampleTest.php</file> -->
    </testsuite>
  </testsuites>
  <!-- 配置代码覆盖率过滤,确保只计算src目录下的代码 -->
  <!-- 日志配置,可以用来生成测试报告 -->
  <logging>
    <junit outputFile="reports/junit.xml"/>
    <!-- 可以添加其他类型的日志输出,比如html -->
  </logging>
  <!-- 如果有需要忽略的测试,可以通过以下方式配置 -->
  <!-- <exclude>tests/Integration/SlowTests/*</exclude> -->
  <!-- 其他可选配置,如设置超时时间、测试运行器等 -->
  <!-- <php>
        <ini name="memory_limit" value="1024M"/>
    </php> -->
  <source>
    <include>
      <directory suffix=".php">app</directory>
    </include>
  </source>
</phpunit>

执行单个测试用例或者批量执行

# 仅执行单元测试
php vendor/bin/phpunit.phar --config phpunit.xml

# 生成带覆盖率报告
vendor/bin/phpunit --config phpunit.xml --no-progress --log-junit reports/phpunit_result.xml --coverage-html reports/phpunit_coverage_html --coverage-clover reports/phpunit_coverage_clover.xml

参数解释
–config 指定phpunit配置文件地址
–no-progress 不显示执行过程
–log-junit 将结果保存到指定路径上,格式为junit
–coverage-html 生成html格式的覆盖率报告,并指定文件夹地址
–coverage-clover 生成clover格式的覆盖率报告,并指定文件地址

在Jenkins中使用

  1. 安装HTML Publisher
  2. 在项目中使用
Dashboard --> 您的项目 --> Configuration --> 构建后操作 --> 选择 Publish HTML reports

HTML directory 按照phpunit生成的报告路径填写,其他的根据实际情况填写即可
在这里插入图片描述

转载请保留出处,都看到这里了,点个赞再走吧

PHP质量工具系列

PHP/JS质量工具,安全工具 总结
TOP 6 PHP代码质量工具
PHP质量工具系列之php-depend
PHP质量工具系列之phpmd
PHP质量工具系列之phpcpd
PHP质量工具系列之phploc
PHP质量工具系列之paslm
PHP质量工具系列之phpstan
PHP质量工具系列之Owasp dependency-check
PHP质量工具系列之php_codesniffer
PHP质量工具系列之phpunit
PHP质量工具系列之xhprof
SBOM生成之CycloneDX

CI/CD之Jenkins插件使用系列

jenkins插件之Jdepend
jenkins插件之plot
jenkins插件之dependency-check
jenkins插件之Warnings
jenkins插件之xunit

  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要安装和使用PHPUnit,您可以按照以下步骤进行操作: 1. 下载PHPUnit:使用命令`wget https://phar.phpunit.de/phpunit.phar`下载PHPUnit。 2. 将PHPUnit设置为全局变量:使用命令`mv phpunit.phar /usr/local/bin/phpunit`将PHPUnit移动到/usr/local/bin目录下,这样您就可以在任何位置直接使用`phpunit`命令了。 3. 检查安装是否成功:在命令行中输入`phpunit --version`,如果正确显示PHPUnit的版本号,则表示安装成功。 4. 创建测试文件:在您的项目目录中创建一个名为`TestExample.php`的文件,并添加测试代码。 5. 运行测试:在命令行中进入到您的项目目录中,然后运行`phpunit TestExample.php`命令来执行测试。 6. 配置文件:如果您需要自定义PHPUnit的配置,可以在项目目录中创建一个名为`phpunit.xml`或`phpunit.xml.dist`的配置文件。如果您希望忽略当前目录下的配置文件,可以使用`--no-configuration`选项来跳过配置文件的加载。 通过按照上述步骤进行操作,您就可以成功安装和使用PHPUnit来进行PHP单元测试了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [PHPUnit简介及使用](https://blog.csdn.net/agonie201218/article/details/89675236)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [PHPUnit介绍及安装](https://blog.csdn.net/lg_lin/article/details/8073945)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值