Birds of a Feather Sessions @ OSCON

Sebastian Bergmann » 01 June 2008 » in Presentations, Presentations » 0 Comments

OSCON 2008

In addition to the tutorial on PHPUnit and phpUnderControl, I will present two Birds of a Feather sessions at this year's edition of OSCON:

Frameworks/Tools for (Unit) Testing

The Hamcrest Project illustrates that good things happen when the developers of (unit) testing frameworks and tools for different programming languages talk to each other. This Birds of a Feather session should serve as a meeting point for the developers of JUnit, TestNG, PHPUnit, PyTest, RSpec, <your favourite framework> for <your favourite language>.

As the developer of PHPUnit I am interested to meet with the developers of (unit) testing frameworks and tools for other languages to discuss concepts, ideas, features, etc.

Why, you might think?

Well, when I (as someone who is not really involved with the happenings in the Java community) first heard about TestNG, for instance, I saw that some of the ideas I had for PHPUnit had been developed independently for TestNG.

I think that this is just the tip of the proverbial iceberg and that we can all learn from each other. So if you are a developer of JUnit, TestNG, PyTest, RSpec, ... join me in this Birds of a Feather session for a discussion!

Hamcrest: A Cross-Language Library of Matchers

The Hamcrest Project provides a library of matcher objects (also known as constraints or predicates) allowing “match” rules to be defined declaratively in Java, PHP, Python, and Objective-C, to be used in other frameworks.

Typical usage scenarios on Hamcrest include testing frameworks, mocking libraries and UI validation rules.

This Birds of a Feather session, moderated by the creator of PHPUnit and the developer of hamcrest-php, has the goal of exposing a wider audience to Hamcrest.

See you in Portland, OR July 21-25 2008!


Defined tags for this entry: , , , ,

Hamcrest

Sebastian Bergmann » 28 May 2008 » in Presentations » 0 Comments

Defined tags for this entry: , ,

Speaking at DLW Europe

Sebastian Bergmann » 27 February 2008 » in PHP » 0 Comments

Dynamic Languages World Europe is the first European conference dealing with the shared concepts and frameworks of all important dynamic languages.

I will present two sessions at DLW Europe, one on Hamcrest, a project that originated in the Java community and is now being ported to more dynamic languages, and one on PHP's object model.

Hamcrest: A Cross-Language Library of Matchers

The Hamcrest Project provides a library of matcher objects (also known as constraints or predicates) allowing "match" rules to be defined declaratively in Java, PHP, and Python, to be used in other frameworks.

Typical usage scenarios on Hamcrest include testing frameworks, mocking libraries and UI validation rules.

This session, held by the creator of PHPUnit and the developer of hamcrest-php, will introduce the audience to Hamcrest and show how it integrates with mocking libraries such as JMock and testing frameworks such as JUnit and PHPUnit.

Understanding the PHP Object Model

Initially designed for "simple" Web programming, PHP has developed to a general-purpose object-oriented language making strong inroads into large-scale, business-critical Web systems. As of version 5 the PHP language features an object model that is similar to the ones of Java and C# and integrates ideas from other programming languages.

This talk will give an overview of PHP's object model, covering both basic OOP concepts such as interfaces, classes, and objects as well as PHP's “magic” interceptor methods.

See you in Karlsruhe, May 26-28 2008!

Defined tags for this entry: , ,

Getting Started with Hamcrest

Sebastian Bergmann » 08 January 2008 » in Articles, Articles » 6 Comments

I recently started porting Hamcrest to PHP. This tutorial shows you how to use Hamcrest for unit testing with PHP.

Introduction

Hamcrest is a framework for writing matcher objects, allowing match rules to be defined declaratively. There are a number of situations where matchers are invaluble, such as UI validation, or data filtering, but it is in the area of writing flexible tests that matchers are most commonly used.

When writing tests, it is sometimes difficult to get the balance right between overspecifying the test (and making it brittle to changes), and not specifying enough (making the test less valuable since it continues to pass even when the thing being tested is broken). Having a tool that allows you to pick out precisely the aspect under test and describe the values it should have, to a controlled level of precision, helps greatly in writing tests that are "just right". Such tests fail when the behaviour of the aspect under test deviates from the expected behaviour, yet continue to pass when minor, unrelated changes to the behaviour are made.

My first Hamcrest test

We'll start by writing a very simple PHPUnit test, but instead of using PHPUnit's assertEquals() or assertThat() methods, we use Hamcrest's assertThat() function and its standard set of matchers:

<?php
require_once 'PHPUnit/Framework.php';
require_once 'Hamcrest.php';
 
class BiscuitTest extends PHPUnit_Framework_TestCase
{
    public function testEquals()
    {
        $theBiscuit = new Biscuit('Ginger');
        $myBiscuit = new Biscuit('Ginger');
        assertThat($theBiscuit, equalTo($myBiscuit));
    }
}
?>

The assertThat() method is a stylized sentence for making a test assertion. In this example, the subject of the assertion is the object biscuit that is the first method parameter. The second method parameter is a matcher for Biscuit objects, here a matcher that checks one object is equal to another.

If you have more than one assertion in your test you can include an identifier for the tested value in the assertion:

  • assertThat('chocolate chips', $theBiscuit->getChocolateChipCount(), equalTo(10));
  • assertThat('hazelnuts', $theBiscuit->getHazelnutCount(), equalTo(3));

Sugar

Hamcrest strives to make your tests as readable as possible. For example, the is() matcher is a wrapper that doesn't add any extra behavior to the underlying matcher. The following assertions are all equivalent:

  • assertThat($theBiscuit, equalTo($myBiscuit));
  • assertThat($theBiscuit, is(equalTo($myBiscuit)));
  • assertThat($theBiscuit, is($myBiscuit));

A tour of common matchers

Hamcrest comes with a library of useful matchers. Here are the ones that are already ported to PHP:

  • Core
    • anything() - always matches, useful if you don't care what the object under test is
    • describedAs() - decorator to adding custom failure description
    • is() - decorator to improve readability - see "Sugar", above
  • Logical
    • allOf() - matches if all matchers match, short circuits (like PHP &&)
    • anyOf() - matches if any matchers match, short circuits (like PHP ||)
    • not() - matches if the wrapped matcher doesn't match and vice versa
  • Object
    • equalTo() - test object equality using ==
    • anInstanceOf() - test type
    • notNullValue(), nullValue() - test for null
    • sameInstance() - test object identity using ===
  • Number
    • closeTo() - test floating point values are close to a given value
    • greaterThan(), greaterThanOrEqualTo(), lessThan(), lessThanOrEqualTo() - test ordering
  • Text
    • equalToIgnoringCase() - test string equality ignoring case
    • equalToIgnoringWhiteSpace() - test string equality ignoring differences in runs of whitespace
    • containsString(), endsWith(), startsWith() - test string matching

Other test frameworks

Hamcrest has been designed from the outset to integrate with different unit testing frameworks. For example, the Java port of Hamcrest can be used with JUnit 3 and 4 as well as TestNG. It is easy enough to migrate to using Hamcrest-style assertions in an existing test suite, since other assertion styles can co-exist with Hamcrest's.

Hamcrest can also be used with mock objects frameworks by using adaptors to bridge from the mock objects framework's concept of a matcher to a Hamcrest matcher. For example, JMock 1's constraints are Hamcrest's matchers. Hamcrest provides a JMock 1 adaptor to allow you to use Hamcrest matchers in your JMock 1 tests. JMock 2 doesn't need such an adaptor layer since it is designed to use Hamcrest as its matching library. Hamcrest also provides adaptors for EasyMock 2.

Defined tags for this entry: , , ,

Hamcrest

Sebastian Bergmann » 29 December 2007 » in Hamcrest » 1 Comment

I have begun to port Hamcrest, which provides a library of matcher objects (also known as constraints or predicates) allowing "match" rules to be defined declaratively, to be used in other frameworks. Typical scenarios include testing frameworks, mocking libraries and UI validation rules, to PHP.

hamcrest-php complements the existing hamcrest-java and hamcrest-dotnet implementations.

The idea here is to use hamcrest-php in PHPUnit 4. I hope that it will also be used by (or be usable with) PHPMock, the new PHP library that abstracts the concept of Mock Objects found in testing libraries such as PHPUnit into a library of its own.
Defined tags for this entry: , ,