Wednesday, November 4, 2009

Phexample, because examples expand on one another

Imagine you want to test the Stack class, and your first test just
creates a test and checks if it's empty.

Then in your second test, you create the same empty stack, but now
push an element 'apple' and check if the size is one.

Then in your third test, you do all the same, and then you pop the
element and check that it is an apple.

Wouldn't it be great if you could write your test cases such that they
could expand on one another? If you didn't need to copy paste the code
of the previous test and just say that you require the previous one!

Well, that's what PhExample lets you do. And as a bonus, tests are
ignored if the test they expand on does not pass. This gives you less
failed tests, because examples are executed only if the examples they
expand on pass too! That gives you the simplest failing examples in
your set to look at!

And best of all, you can run all examples with ye old TestRunner!

The example discussed above would read in code as follows:

EGExample subclass: #ForExampleStack

"Create the empty stack"
| stack |
stack := Stack new.
stack should be isEmpty.
stack size should = 0.
^ stack

"Push one element"
| stack |
stack := self given: #shouldBeEmpty.
stack push: 42.
stack should not be isEmpty.
stack size should = 1.
stack top should = 42.
^ stack

"And pop it again"
| stack |
stack := self given: #shouldPushElement.
stack pop should = 42.
stack should be isEmpty.
stack size should = 0.

Find the code at

You can install it as follows using Gofer:

Gofer new
squeaksource: 'phexample';
addPackage: 'Phexample';

The repository is write-global, so feel free to drop your ideas!

PhExample is based on JExample by Lea Haensenberger, Adrian Kuhn, and
Markus Gaelli. See JExample

1 comment:

  1. Thanks, can't wait to try it! btw addPackage: is depreciated in Gofer (#package: now) :)


Note: Only a member of this blog may post a comment.