Skip to content

Categories:

SimpleTest: Start testing your code

In today’s tutorial, we will focus on testing your code using SimpleTest. Before we start, let me explain how this tutorial ‘works’. I will explain the functions / methods etc. of SimpleTest by testing a sample class, which will be developed throughout the tutorial. Every time the class is updated, there will be a link to a text file containing the current version of the class. Let’s start!

The Requirements for the class

In the list below, I will tell you what the class will be doing. I will write a test, which obviously will fail since there is no code within the tested class method. Then we’ll write code until the test completes successfully. This type of coding is commonly referred to as TDD: “Test Driven Development”. For more on TDD, please see this link.

  • The class will be used to handle a sale in a shop (where all products costs the same).
  • The code should allow the use of a getter and setter for the items.
  • In-case a method called ‘addItem‘ is used, it should add an item, instead of set the array to a new value (like setItems does)
  • When using count($order_object), the code should return the price for this order.

First let’s write a test!


To write a test is actually very simple. First, I’ll show you my directory structure

  • /
    • simpletest/
      • All the SimpleTest files
    • Shop_Order.php – contains the Shop_Order class
    • Show_Order_Test.php – contains the Test Case

To begin writing this class, we will create a Test Case, and the structure of the class.

Shop_Order_Test.php

<?php
require_once('simpletest/autorun.php');
require_once('Shop_Order.php');

class TestOfShopOrder extends UnitTestCase
{

	public $order;

	public function testCreateObject()
	{
		$this->order = new Shop_Order();
		$this->assertNotNull($this->order);
	}

	public function testAddItem()
	{
		$this->order->addItem('Item 1');
		$this->assertEqual(1, count($this->order->getItems()));
	}

}

Shop_Order.php

<?php
class Shop_Order
{

	protected $_items;
	protected $_price;

	public function getItems() { }
	public function setItems(array $items) { }
	public function addItem() { }

	public function getPrice() { }
	public function setPrice() { }
}

To run this test, simply navigate using a web browser to the test case file – if the file is in your Document Root. Otherwise, use php Shop_Order_Test.php to run it. You will see that 1 test passed, and 1 test failed. Since you can just initiate the class, the first test (assertNull) will pass. However, you can not add any items yet, as the functionality for it misses.

Shop_Order_Test.php
1) Equal expectation fails because [Integer: 1] differs from [Integer: 0] by 1 at [Shop_Order_Test.php line 19]
	in testAddItem
	in TestOfShopOrder
FAILURES!!!
Test cases run: 1/1, Passes: 1, Failures: 1, Exceptions: 0

As you can see, we were looking if 1 was equal to count($this->order->getItems()). But since the last statement returns null, and count(null) is equal to 0, this assertion fails.

The basics of SimpleTest

You create a class that whose name starts with “Test”, and in the class you create methods that start with “test”. They will automatically be run. There are a lot of assertions available.

List of assertions

List of assertions

For more information, see the SimpleTest documentation (available in the simpletest/doc folder).

Modifying Shop_Order.php

Now we are going to write code until the complete Test Case passes. Shop_Order.php (version 2). If you copy and paste that code into Shop_Order.php, running the Test Case again will result in 2 passes and 0 failures.

Shop_Order_Test.php
OK
Test cases run: 1/1, Passes: 2, Failures: 0, Exceptions: 0

More updates

To keep the length of this post a little bit sane, I will list the files to complete this post below.

That’s the end!

Please, if you need help understanding anything about this post, use the comment form below, and I will either reply on your comment, or mail you back if you enter a valid e-mail address.

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Technorati
  • Twitter
  • StumbleUpon

Posted in General.

Tagged with , .


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.