How to run phpt tests with PHPUnit

In this post I will explain how to run phpt tests with PHPUnit.

For my GSOC project I will use (and contribute to) the PEAR package Text_Wiki (more about my contributions on this package on another post). This package use phpt tests for unit testing. I’m not familiar with phpt tests as I’m with PHPUnit and phpt tests didn’t give me a good impression (its difficult to understand feedback and its syntax mixing PHP code and plain text statements lead to improper syntax highlighting in Emacs :-D).

So I proposed the main developer of Text_Wiki to switch to PHPUnit. He had no objections if I were able to provide a solution that made possible to run the old phpt tests with the new PHPUnit tests at the same time.

I found that there is a PHPUnit extension that does exactly what I was looking for. As I wasn’t able to find a tutorial explaining how to use it, I decided to write this post to share the way I used that extension.

I’m using version 3.3.17 of PHPUnit and it came with two phpt related extensions located in the directory PHPUnit/Extensions (if you use Ubuntu as I do the full path will probably be /usr/share/php/PHPUnit/Extensions): PhptTestCase.php and PhptTestSuite.php

The first one implements the class PHPUnit_Extensions_PhptTestCase. The constructor of this class receive as a parameter the path to a single phpt file. I did not use this one.

The other file implements the class PHPUnit_Extensions_PhptTestSuite. You can instantiate this class in your test script passing as the first parameter the root directory where your phpt files are located. The constructor of the class will recursively look for all files with the extension .phpt.

On the link below you can view the code I used to integrate the old Text_Wiki phpt tests with the new PHPUnit classes I’m writing:

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *