Приглашаем посетить
Сумароков (sumarokov.lit-info.ru)

Section 13-1.  Formatting Feedback

Previous
Table of Contents
Next

13-1. Formatting Feedback

Besides the required <batchtest> element, the <phpunit2> element allows for another nested element: <formatter> is used to write test results in different formats. Output will always be sent to a file, unless you set the usefile attribute to false.

The name of the file is predetermined by the formatter and can be changed by the outfile attribute. There are three predefined formatters:


brief

Prints detailed information in plain text only for test cases that failed.


plain

Prints one-line statistics in plain text for all test cases.


xml

Writes the test results in XML format.

Table 4 shows the parameters that can be used to configure the <formatter> task.

Table 4. Attributes for the <formatter> element

Name

Type

Description

Default

type

String

Name of a predefined formatter (xml, plain, or brief).

classname

String

Name of a custom formatter class.

usefile

Boolean

Flag marking whether output should be sent to a file.

true

todir

String

Directory the file is written to.

outfile

String

Name of the file that is written to.

Depends on the formatter used.


To generate a test report in HTML format, you can use the <phpunit2report> task, which applies an XSLT stylesheet to the XML logfile created by the <formatter> task. Phing ships with two XSLT stylesheetsphpunit2-frames.xsl and phpunit2-noframes.xslthat generate HTML reports with or without frames, respectively.

Example 16 shows a build.xml file for Phing that runs the tests from the BankAccountTest class and generates a test report in HTML format using the phpunit2-frames.xsl XSLT stylesheet. The HTML files generated for the report will be written to the report/ directory that is created by the "prepare" <target> and deleted by the "clean" <target>.

Example 16. Applying an XSLT stylesheet to get a test report
<?xml version="1.0"?>

<project name="BankAccount" basedir="." default="report"> 
	 <target name="prepare"> 
     <mkdir dir="report"/> 
   </target>

   <target name="clean">
     <delete dir="report"/>
   </target>

	 <target name="report" depends="prepare">
     <phpunit2>
        <batchtest>
          <fileset dir=".">
             <include name="*Test.php"/>
          </fileset>
        </batchtest>

			  <formatter type="xml" todir="report"
               outfile="logfile.xml"/>
     </phpunit2>

     <phpunit2report infile="report/logfile.xml" 
				 format="frames" styledir="." 
               todir="report"/>
   </target>
</project>

The following example shows the output of the phing command as it runs:

	phing
	Buildfile: /home/sb/build.xml

	BankAccount > prepare:
		[mkdir] Created dir: /home/sb/report

	BankAccount > report:

	BUILD FINISHED

	Total time: 0.1112 seconds

Figure 3 shows the title page of the generated test report.

Section 13-1.  Formatting Feedback

Figure 1-3. The generated test report


Table 5 shows the parameters that can be used to configure the <phpunit2report> task.

Table 5. Attributes for the <phpunit2report> element

Name

Type

Description

Default

infile

String

The filename of the XML results file to use.

testsuites.xml

format

String

The format of the generated report. Must be frames or noframes

noframes.

styledir

String

The directory in which the stylesheets are located. The stylesheets must conform to the following conventions: the stylesheet for the frames format must be named phpunit2-frames.xsl, the stylesheet for the noframes format must be named phpunit2-noframes.xsl.

todir

String

The directory to which the files resulting from the transformation should be written.


In addition to the test report that we just generated, Phing can generate a code-coverage report. For this, we need the <coverage-setup> and <coverage-report> tasks. The former prepares a database in which code-coverage information is stored while the tests are run; the latter formats such a database into a report in HTML format using XSLT stylesheets.

Example 17 shows a build.xml file for Phing that runs the tests from the BankAccountTest class and generates a codecoverage report in HTML format.

Example 17. Generating a code-coverage report
<?xml version="1.0"?>

<project name="BankAccount" basedir="."
      default="coverage-report">
<target name="prepare">
   <mkdir dir="coverage-report"/>
</target>

<target name="clean">
   <delete dir="coverage-report"/>
</target>

<target name="coverage-report" depends="prepare">
   <coverage-setup database="./coverage-report/database">
     <fileset dir=".">
        <include name="*.php"/>
        <exclude name="*Test.php"/>
     </fileset>
   </coverage-setup>

   <phpunit2>
   <batchtest>
     <fileset dir=".">
        <include name="*Test.php"/>
     </fileset>
     </batchtest>
     </phpunit2>

     <coverage-report outfile="coverage-report/coverage.xml">
        <report todir="coverage-report" styledir="."/>
     </coverage-report>
    </target>
  </project>

Figure 4 shows the title page of the generated code-coverage report.

Section 13-1.  Formatting Feedback

Figure 1-4. The generated code-coverage report



Previous
Table of Contents
Next