【转载】VS Code 进行 PHP 单元测试

发布时间 2023-11-29 16:32:15作者: 夏秋初

参考

注意

PHPUnit Test Explorer 可能会抽风导致没有测试按钮之类的情况,抽风的时候也可以使用注释 @test 来标注需要测试的方法,就可以点击测试了

use PHPUnit\Framework\TestCase;

class SampleTest extends TestCase
{
    public function testSomething()
    {
        $this->assertTrue(true, 'This should already work.');
    }

    /**
     * @test
     */
    public function something()
    {
        $this->assertTrue(true, 'This should already work.');
    }
}

环境

软件/系统 版本 说明
windows 10
php 8.1.9-Win32-vs16-x64
composer 2.4.4 下载
phpunit ^10.4 php依赖
vscode 1.84.2
PHPUnit Test Explorer v3.2.2 vscode插件

步骤

  1. 初始化项目。(此处 composerhttps://getcomposer.org/download/ 处下载的 composer.phar 去掉后缀)
php composer init
  1. 安装 phpunit 库。
php composer require --dev phpunit/phpunit