mac phpstorm设置单元测试

设置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官方文档

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容