mac phpstorm设置单元测试

2021-07-23  本文已影响0人  JeansLin

设置PHP CLI Interpreter(本地php执行路径)

路径:Preferences > Languages & Frameworks > PHP

image.png

如果CLI Interpreter没有显示可选版本,可以点击右边"..."选择本地的PHP的可执行路径


image.png

设置phpunit

路径:Preferences > Languages & Frameworks > PHP > TestFrameworks


image.png

该示例中我们使用composer自动加载的方式,composer安装phpunit可以使用

composer require --dev phpunit/phpunit

Path to script:填项目中vendor的autoload.php
Default configuration file:填写项目中phpunit.xml文件的路径

配置phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="./test/bootstrap.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Tests">
            <directory suffix="Test.php">./test</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
</phpunit>

此处我们配置一个testsuite,目录为项目根目录所在的test目录
bootstrap:在PHPUnit命令行测试在测试之前执行,作用相当于include ./test/bootstrap.php,常用配类的自动加载

运行单元测试

我们在根目录写一个单元测试,内容如下并点击IDE的执行按钮就可以执行单元测试了


image.png

至此phpstorm的单元测试就配置完成了^_^
phpunit的具体操作可参考:phpunit官方文档

上一篇 下一篇

猜你喜欢

热点阅读