Zend Framework 2 Unit Tests on Jenkins not working

2019-08-03 18:23发布

问题:

I have a zend framework 2 project and i am trying to set up my jenkins so that unit tests can be executed. Jenkins is running on ubuntu and i am developing under Windows 7 with PHPStorm.

build.xml

<target name="phpunit" description="Run unit tests with PHPUnit">
    <exec executable="phpunit" failonerror="true">
        <arg value="${basedir}/module/Addressbook/test"/>
    </exec>
</target>

Folder structure:

  • project
    • module
      • Addressbook
      • test
        • AddressbookTest
          • Controller
            • AddressbookControllerTest.php
          • Boostrap.php
          • phpunit.xml.dist
          • TestConfig.php
    • build.xml

Jenkins Output:

phpunit:
     [exec] PHPUnit 3.7.13 by Sebastian Bergmann.
     [exec] 
     [exec] PHP Fatal error:  Class 'AddressbookTest\Bootstrap' not found in /var/lib/jenkins/workspace/Test/src/module/Addressbook/test/AddressbookTest/Controller/AddressbookControllerTest.php on line 28

PHPStorm on my local machine does this when running phpunit.xml.dist

D:\Zend\ZendServer\bin\php.exe -d auto_prepend_file=D:/Zend/Apache2/htdocs/demoshop/vendor/autoload.php C:\Users\ChristianB\AppData\Local\Temp\ide-phpunit.php --configuration D:/Zend/Apache2/htdocs/demoshop/module/Addressbook/test/phpunit.xml.dist

How can i use that for jenkins?

回答1:

It looks like your include path isn't setup correctly, I wouldn't use exec directly with PHPUNIT when there's better options.

You should look into using PHING tasks with Jenkins, they work excellent together.

You then setup Jenking to trigger your PHING target to run the unit tests for you via the PHPUNIT task, an example phing target for PHPUNIT:

<target name="phpunit">
    <phpunit bootstrap="${srcdir}/tests/bootstrap.php">
        <formatter todir="${builddir}/reports" type="xml"/>
        <batchtest>
            <fileset dir="${srcdir}/tests">
                <include name="**/*Test*.php"/>
                <exclude name="**/Abstract*.php"/>
                <exclude name="${srcdir}/vendor/**"/>
            </fileset>
        </batchtest>
    </phpunit>
    <!-- 
        Generate a report from the XML data created.. 
        note: error when using format="frames"
    --> 
    <phpunitreport infile="${builddir}/reports/testsuites.xml" 
        format="noframes" 
        todir="${builddir}/reports/tests"
    />

</target>