PhpStorm 配置 PHPUnit(基于docker环境与mac)

一、目录结构

  • phpunit.xml:单元测试配置文件
  • tests:存放测试代码目录
  • build:生成测试报告目录
  • tests/bootstrap.php 脚手架入口

phpunit

<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="tests/bootstrap.php"

         backupGlobals="false"

         backupStaticAttributes="false"

         beStrictAboutCoversAnnotation="true"

         beStrictAboutOutputDuringTests="true"

         beStrictAboutTestsThatDoNotTestAnything="true"

         beStrictAboutTodoAnnotatedTests="true"

扫描二维码关注公众号,回复: 3724868 查看本文章

         forceCoversAnnotation="true"

         strict="true"

         verbose="true">

    <testsuites>

        <testsuite name="ProjectName">

            <directory suffix="Test.php">tests</directory>

        </testsuite>

    </testsuites>

    <logging>

        <log type="coverage-html" target="build/coverage"/>

        <log type="coverage-clover" target="build/coverage/clover.xml"/>

        <log type="coverage-xml" target="build/logs/coverage"/>

        <log type="junit" target="build/logs/junit.xml"/>

    </logging>

    <filter>

        <whitelist processUncoveredFilesFromWhitelist="true">

            <directory suffix=".php">./application</directory>

            <exclude>

                <directory suffix=".php">./application/common/conf</directory>

            </exclude>

        </whitelist>

    </filter>

</phpunit>

二、编辑器配置

2.1、标识目录资源类型

  1. 右击 build 目录,Mark Directory as 【Exclude】避免生成报告后 ide 自动执行此目录无用的代码分析
  2. 右击 tests 目录,Mark Directory as 【Test Resource Root】

2.2、Cli Interpreter 设置

Step 1:Preference => Language & Framework => PHP

Step 2:点击 Cli Interpreter 右侧按钮

Step 3:新增:Select Cli Interpreter From Docker 

如果server没有可以选择的,则可以New一个,要保证Connection successful

如果connection显示失败,可以参考下图设置,右键docker小图标=》setting=》general=>expose daemon tcp://localhost:2375勾上

Step 4:设置 Docker-compose.yml

Step 5:设置 PHP 执行路径

配置完成后,效果如下

Step 6:设置 Project Path Mapping

/Users/jason_yip/opt/htdocs/papi-eren.mama.cn => /opt/htdocs/papi-eren.mama.cn

2.3、设置 Test Framework

Step 1:Preference => Language & Framework => PHP => Test Frameworks

Step 2:设置 PHPUnit By Remote Interpreter

如下图所示,设置好相关配置

执行配置文件也可以配成如下图,采用phpunit-dev.xml配置文件不会生成报告,这样执行单元测试会比较快些。

三、composer安装phpunit

安装phpunit包后,在IDE编写单元测试时有代码提示功能,方便写单元测试,效果如图:

3.1、创建phpunit目录

Step 1:在opt目录下创建code文件夹

Step 2:在code文件夹下创建phpunit文件夹

最后路径为:C:\Users\Administrator\opt\code\phpunit

3.2、安装phpunit

在phpunit文件下执行:composer require phpunit/phpunit,得到如图文件

3.3、phpstorm添加phpunit扩展类

Step 1:右键打开External Libraries,选择Configure PHP Include Paths...

Step 2:点击右边 + 号

Step 3:选择phpunit路径后点击OK

Step 4:显示多了一条记录,最后点击 Apply

猜你喜欢

转载自blog.csdn.net/Baron0071/article/details/82712678