Using PHPUnit from a Git Checkout

Sebastian Bergmann » 15 April 2010 » in Articles » 3 Comments

Users of PHPUnit frequently ask me questions such as "How do I use PHPUnit from a Git checkout?" or "How do I run PHPUnit's own test suite?" This article provides the answers to these questions.

Getting PHPUnit from Git

sb@thinkpad ~ % git clone git://github.com/sebastianbergmann/phpunit.git

You now have a phpunit directory in your current working directory that contains the branch for PHPUnit 3.5 (at the time of writing) because the 3.5 branch is currently configured as the default branch for the PHPUnit repository on GitHub. If you want to switch to the branch for PHPUnit 3.4, for instance, you can use

sb@thinkpad phpunit % git checkout -b 3.4 origin/3.4

This tells Git to create a new local branch of name 3.4 (-b 3.4) that is set up to track the remote branch 3.4 from origin (origin/3.4). Git automatically switches to the newly created local branch. Using

sb@thinkpad phpunit % git checkout 3.5

you can switch back to the branch for PHPUnit 3.5.

Running PHPUnit from a Git checkout

Inside the phpunit directory you will find the phpunit.php script. Using this script you can invoke the PHPUnit TextUI test runner:

sb@thinkpad ~ % phpunit/phpunit.php
PHPUnit @package_version@ by Sebastian Bergmann.
.
.
.

The @package_version@ placeholder string for the version information makes it clear that a non-release version of PHPUnit is being used. Upon installation, the PEAR Installer replaces this placeholder string with the relevant information.

Running PHPUnit's Own Test Suite

Running PHPUnit's own test suite is as easy as invoking phpunit in the checkout directory:

sb@thinkpad phpunit % phpunit                  
PHPUnit @package_version@ by Sebastian Bergmann.

............................................................  60 / 681
............................................................ 120 / 681
............................................................ 180 / 681
............................................................ 240 / 681
............................................................ 300 / 681
............................................................ 360 / 681
............................................................ 420 / 681
............................................................ 480 / 681
................................SSSSSSSSSSSSSSSSSSSSSSSSSSSS 540 / 681
SSSS.......................................S................ 600 / 681
............................................................ 660 / 681
.....................

Time: 27 seconds, Memory: 56.25Mb

OK, but incomplete or skipped tests!
Tests: 681, Assertions: 1459, Skipped: 33.

Writing code coverage data to XML file, this may take a moment.

Generating code coverage report, this may take a moment.

The above works because there is an XML configuration for PHPUnit (phpunit.xml.dist) in the directory that contains information about which tests to run and what logfiles to produce.

Defined tags for this entry: ,

Trackback specific URI for this entry

3 Comments to "Using PHPUnit from a Git Checkout"

Display comments as (Linear | Threaded)
  1. Matthew Weier O'Phinney
    15/04/2010 at 14:52 Permalink
    Very exciting -- I've been wanting to play with 3.5 a bit, and decided to follow your instructions here.

    However, it doesn't work out-of-the-box as you describe in this post. As you note in the README.markdown of the project, there are two new dependencies, PHP_FileIterator and Text_Template. Not mentioned is an additional dependency on PHP_CodeCoverage.

    In order to get things working from git, I did the following:

    % git checkout -b local/submods 3.5
    % git submodule add git://github.com/sebastianbergmann/php-code-coverage php-code-coverage
    % git submodule add git://github.com/sebastianbergmann/php-file-iterator php-file-iterator
    % git submodule add git://github.com/sebastianbergmann/php-text-template php-text-template
    % git ci -m 'Added submodules for code coverage, filter iterator, and text template'

    I then edited phpunit.php to add the following lines before any other code:
    set_include_path(implode(PATH_SEPARATOR, array(
    __DIR__,
    __DIR__ . '/php-code-coverage',
    __DIR__ . '/php-file-iterator',
    __DIR__ . '/php-text-template',
    get_include_path(),
    )));

    Once all the dependencies were met, it worked like a charm. Since I'm doing this in a local branch, I'm keeping my changes isolated, which allows me to selectively merge from the 3.5 branch when desired.

    Reply

  2. strk
    13/07/2010 at 11:48 Permalink
    php-token-stream is also required, and PHP/Timer.php (couldn't find the latter)

    git submodule add git://github.com/sebastianbergmann/php-token-stream php-token-stream

    Reply

  3. Graham Anderson
    16/04/2010 at 22:43 Permalink
    I'll also chip in that you can do a fair amount of foomagic by tweaking your local git config.

    Use glob patterns to ignore files, either in .gitignore files for your code tree, or in .git/info/exclude for local ignores.

    As Matthew alluded to in his post[1] about integrating Git into the ZF development process, you can accomplish a lot by using Git hooks. One of the issues many people face after moving their code to Git is how to handle directories. Git tracks content, not file systems, so you may often find that you are missing an empty directory or a directory may have incorrect permissions.

    To track empty directories you can use a quick hack:

    #!/bin/bash
    find . \( -type d -empty \) -and \( -not -regex ./\.git.* \) -exec touch {}/.gitignore \;

    Use a pre/post checkout hook to change permissions on a directory or cleanup test fragments.

    .git/hooks/post-checkout

    There should already be some samples in your .git/hooks directory

    [1]http://weierophinney.net/matthew/archives/236-GPG-signing-Git-Commits.html

    Reply

0 Trackbacks to "Using PHPUnit from a Git Checkout"

  1. No Trackbacks

Add Comment


To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

Submitted comments will be subject to moderation before being displayed.