Zend Framework 2 + PHPUnit + 多个模块 + 持续集成

12
感谢任何评论的提前感谢。我刚开始从Zend Framework 1转换到ZF2,经过快速入门和其他几个教程之后,我注意到使用phpunit的“默认”方式存在一些短缺。要么是我迷失和困惑了。
默认项目的文件夹结构为:
Project
| - config
| | - autoload
| | | - global.php
| | | - local.php.dist
| | - application.config.php
| - data
| - module
| | - Application
| | | - config
| | | - src
| | | - test
| | | | - ApplicationTest
| | | | - Bootstrap.php
| | | | - phpunit.xml
| | | | - TestConfig.php.dist 
| | | - view
| | | - Module.php
| | - Album
| | | - config
| | | - src
| | | - test
| | | | - AlbumTest
| | | | - Bootstrap.php
| | | | - phpunit.xml
| | | | - TestConfig.php.dist 
| | | - view
| | | - Module.php
| - public
| - vendor

我的问题是如何使用Jenkins和ANT来测试所有的PHPUnit测试套件。我理解单独测试每个模块的原因,但如何正确自动化以获取一个report.xml呢?如果不需要在phpunit配置文件或build.xml中指定每个模块,那就更好了。

再次感谢您的任何评论。

2个回答

4

抱歉,当我自己解决问题时,我忘记回答自己的问题。我向社区道歉...但是为了每个人的利益,这里是我如何让它工作的。

build.xml

<target name="phpunit" description="Run unit tests with PHPUnit">
    <apply executable="../vendor/bin/phpunit" parallel="false">
        <fileset dir="${env.WORKSPACE}/module" >
            <include name="**/test/phpunit.xml"/>
        </fileset>
        <arg value="--configuration" />
        <srcfile/>
    </apply>
</target>

每个模块都有相应的phpunit.xml文件。

<phpunit bootstrap="Bootstrap.php">
    <testsuites>
        <testsuite name="Application">
            <directory>./</directory>
        </testsuite>
    </testsuites>

<!-- Filters only matter for code coverage reporting -->
    <filter>
        <blacklist>
            <directory>../../../vendor/</directory>
            <directory>./</directory>
            <file>../Module.php</file>
        </blacklist>
    </filter>
    <logging>
        <log type="coverage-html" target="../../../build/coverage" title="Application Module" charset="UTF-8" yui="true" highlight="true" lowUpperBound="35" highLowerBound="70"/>
        <log type="coverage-clover" target="../../../build/logs/clover-Application.xml"/>
        <log type="junit" target="../../../build/logs/junit-Application.xml" logIncompleteSkipped="false"/>
    </logging>
</phpunit>

2

我使用以下结构。我将所有测试放在tests文件夹中,并按照模块的结构方式组织测试:

Project
| - config
| | - autoload
| | | - global.php
| | | - local.php.dist
| | - application.config.php
| - data
| - module
| | - Application
| | | - config
| | | - src
| | | | - Application
| | | | | - Controller
| | | | | | - IndexController.php
| | | | | - Model
| | | | | | - Foo.php
| | | | | - Form
| | | - view
| | | - Module.php
| | - Album
| | | - config
| | | - src
| | | | - Album
| | | | | - Controller
| | | | | | - IndexController.php
| | | | | - Model
| | | | | | - Bar.php
| | | | | - Form
| | | - view
| | | - Module.php
| - public
| - vendor
| - tests
| | - unit
| | | - module
| | | | - Application
| | | | | - src
| | | | | | - Application
| | | | | | | - Controller
| | | | | | | | - IndexControllerTest.php
| | | | | | | - Model
| | | | | | | | - FooTest.php
| | | | - Album
| | | | | - src
| | | | | | - Album
| | | | | | | - Controller
| | | | | | | | - IndexControllerTest.php
| | | | | | | - Model
| | | | | | | | - BarTest.php
| | - functional
| | | - features
| - phpunit.xml
| - phpunit-ci.xml
| - behat.yml

PHPUnit的配置文件可以是以下简化示例(根据您的需求添加白名单、过滤器、覆盖率等):

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/unit/Bootstrap.php" colors="true" backupGlobals="false" backupStaticAttributes="false" syntaxCheck="false">
    <testsuites>
        <testsuite name="sites">
            <directory suffix="Test.php">tests/unit</directory>
        </testsuite>
    </testsuites>
</phpunit>

phpunit-ci.xml示例:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/unit/Bootstrap.php" colors="true" backupGlobals="false" backupStaticAttributes="false" syntaxCheck="false">
    <testsuites>
        <testsuite name="sites">
            <directory suffix="Test.php">tests/unit</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist>    
            <!-- Album module -->
            <directory suffix=".php">module/Album/src/Album/Model</directory>
            <directory suffix=".php">module/Album/src/Album/Controller</directory>

            <!-- Application module -->
            <directory suffix=".php">module/Application/src/Application/Model</directory>
            <directory suffix=".php">module/Application/src/Application/Controller</directory>
        </whitelist>
    </filter>
    <logging>
        <log type="coverage-html" target="build/coverage" charset="UTF-8"
             yui="true" highlight="true" lowUpperBound="40" highLowerBound="80" />
        <log type="coverage-clover" target="build/logs/clover.xml" />
        <log type="junit" target="build/logs/junit.xml" logIncompleteSkipped="false" />
    </logging>
</phpunit>

在 build.xml 中很容易实现:

<target name="phpunit-ci" description="Run unit tests with config file for CI">
    <sequential>

        <exec executable="${basedir}/vendor/bin/phpunit" failonerror="true">
            <arg value="--version" />
        </exec>

        <exec executable="${basedir}/vendor/bin/phpunit" failonerror="true">
            <arg value="-c" />
            <arg path="${basedir}/phpunit-ci.xml" />
        </exec>

    </sequential>
</target>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接