javascript
Brief description  about Online courses   join in Online courses
OR

PHP Tools for Testing and Debugging

Kuljit  Singh
Kuljit Singh
Software Testing Lead

1. Cucumber

I recently attended a local Ruby usergroup meeting, and was treated to an introduction to the Cucumber behavior-driven development tool. Cucumber allows you to write tests in "plain English" (or any of 30 other spoken languages) and then execute them in conjunction with a third-party tool such as Watir, Selenium, or Mechanize to execute the tests within a browser.

For instance, check out this Cucumber test (part of the Cucumber example suite), which is intended to confirm the results of a search executed through the Google search engine:

Feature: Search
  In order to learn more
  As an information seeker
  I want to find more information
 
  Scenario: Find what I'm looking for
    Given I am on the Google search page
    When I search for "cucumber github"
    Then I should see
      """
      GitHub
      """

Although originally written for testing Ruby on Rails applications, Cucumber can be used with many mainstream programming languages, including Java, Python and PHP. Learn more about how to configure Cucumber to test your PHP-driven websites

2. PHPUnit

Test-driven development (TDD) is all the rage these days, and for good reason. By elevating the role of testing -- insomuch that tests are developed alongside the application code rather than after much of the application code has already been written -- developers are constantly encouraged to think about the different ways in which errors or unexpected outcomes can occur. As a result, they accordingly write tests to ensure their application doesn't succumb to those errors.

The PHPUnit testing framework offers PHP developers a fantastic solution for doing TDD, offering a rigorous solution for writing, organizing and automating tests. Incorporating such a tool into your daily development routine can result in enormous productivity increases by saving you the hassle of having to test features such as the user login process manually. For instance, the Zend Framework offers support for PHPUnit, allowing me to write tests that can automate testing of the login process, as demonstrated here:

public function testValidLoginShouldAuthenticateSessionAndRedirectToHomePage()
{
 
  $this->request->setMethod('POST')
       ->setPost(array(
           'email' => 'wj@example.com',
           'pswd'  => 'secret',
           'public' => 0
         ));
 
  $this->dispatch('/account/login');
       
  $this->assertController('account');
  $this->assertAction('login');
 
  $this->assertRedirectTo('/');
 
  $this->assertTrue(Zend_Auth::getInstance()->hasIdentity());
 
}

This test will confirm that the account controller's login action can accept a valid email address and password, authenticate the account, redirect the user to the site's homepage, and finally confirm that a valid user session exists.

3. SimpleTest

SimpleTest is another great automated testing solution, which like PHPUnit allows developers to write unit tests using familiar PHP syntax. Additionally, SimpleTest offers several features not natively available to PHPUnit (although these features can be used with PHPUnit in conjunction with third-party tools) such as Web interface testing, including the ability to test authentication and Web forms.

For instance, the following SimpleTest test will navigate to the Google home page, insert the query "PHP" into the search field, and submit the form by clicking the Google Search button:

class SimpleFormTests extends WebTestCase {
  function testDefaultValue() {
    $this->get('http://www.google.com/');
    $this->setField('q', 'PHP');
    $this->click('Google Search');
  }
}

Although not as well known as PHPUnit, SimpleTest's user-friendly approach to testing continues to earn the framework accolades among many members of the PHP community. For this reason, if you're a relative novice to programming and feel particularly intimidated by the various testing frameworks, SimpleTest may be well worth a look.

4. XDebug

Much of the time you spend testing and debugging will involve grunt work that requires you to inspect the state of your application at various stages of execution. Typically accomplished using native PHP statements such as echo and var_dump, reviewing the contents of an object can quickly become a messy affair due to the lack of coherent formatting when the object contents are output to the browser.

The XDebug PHP extension can greatly enhance the readability of this data by changing how array and object contents are output to the browser. Consider the improvements when reviewing even a simple array such as this:

<?php
 
$sports = array();
$sports[] = "Football";
$sports[] = "Baseball";
$sports[] = "Basketball";
 
?>

For output using var_dump, the array contents will look like this:

array(3) { [0]=> string(8) "Football" [1]=> string(8) "Baseball" [2]=> string(10) "Basketball"}

However, with XDebug enabled, the array contents will appear like this:

array
  0 => string 'Football' (length=8)
  1 => string 'Baseball' (length=8)
  2 => string 'Basketball' (length=10)

Installed in mere minutes, XDebug is one of those tools that quickly will make you wonder how you ever got along without it. For more information about XDebug,.

5. Firebug

These days, creating great websites isn't simply a function of understanding a server-side language, with even the simplest of websites are driven by a symphony of HTML, CSS, JavaScript and a language such as PHP. Therefore, your ability to test and debug all facets of the website effectively is going to weigh heavily on your productivity. And for such purposes, there are few tools more capable than Firebug, a Firefox extension that you can use to inspect and manipulate every conceivable characteristic of a Web page.

Whether you want to see what your website looks like with all images disabled, experiment with CSS-driven font sizes, or easily measure the dimensions of a DIV using a built-in ruler, Firebug contains a seemingly endless array of useful features capable of answering any question you may have regarding your website's operation.

6. FirePHP

FirePHP extends the aforementioned Firebug's capabilities to the server side, allowing you to easily log messages and other data hailing from a PHP script. This can be tremendously useful when debugging Ajax-driven features, or when you simply want to inspect the contents of an object or array without having to repeatedly insert and delete echo or var_dump statements.

Like Firebug, FirePHP is also a Firefox extension. However you'll also need to install a simple PHP library on the server running your PHP-driven website. This script serves as the bridge for communications between the server and Firebug/FirePHP..

7. Watir

As its name implies, Watir ("Web Application Testing in Ruby") has its roots in the Ruby community, but it can be used in conjunction with a wide variety of programming languages, PHP included. The toolkit allows you to write scripts that automate browser-based tasks in order to determine whether both your Web interface and server-side application are performing as expected. These tests are written in Ruby's always easily understandable code and can perform tasks such as testing a website's login interface, as demonstrated here:

require 'rubygems' 
require 'watir' 
browser = Watir::Browser.new 
browser.goto "http://www.example.com/account/login" 
browser.text_field(:name, "email").set("test@example.com") 
browser.text_field(:name, "password").set("secret") 
browser.button(:value, "Login").click 
if browser.contains_text("Welcome back, Jason") 
  puts "Test passed. Test user login successful." 
else 
  puts "Test failed. Test user did not successfully login." 
end

Although originally natively capable of testing only Internet Explorer, numerous extensions allow you to automate testing within all of the major browsers, including Firefox, Safari, and Google Chrome. Further, you can automate the execution of your Watir tests just as you can with PHPUnit using Ruby's Test::Unit framework.

8. Selenium

Like Watir, Selenium is a testing solution that allows you to verify the proper operation of your website from the user's perspective. In addition to providing developers with the ability to write scripted tests, an impressive Firefox extension known as Selenium IDE allows test developers to record tests directly from within Firefox simply by interacting with the website. These actions will then be converted into the scripts that Selenium will use to execute the tests. Scripting capabilities are supported for all of the other major browsers.

9. Human-powered Testing with UserTesting.com

Most small businesses or open source projects lack the funding necessary to hire a full- or even part-time quality assurance team. Yet those of you in search of feedback from your fellow humans aren't out of luck, because quite a few online usability testing services can offer extremely detailed and frank feedback at a surprisingly low cost.

One such service is UserTesting.com, which for just $39 will provide you with both video and written summaries of their testing panel's experience interacting with your website. UserTesting.com's services are so popular that globally recognized companies such as Amazon.com, Staples and Cisco have relied upon the service for unbiased third-party feedback.

10. Bug Tracking with Bugzilla

With all of these great testing and debugging utilities at your disposal, you're going to need some effective way to keep track of the problems you uncover, not to mention assign them to various team members. One of the most popular issue tracking solutions is BugZilla, an open source project used to manage issues within not only high-profile project initiatives such as the Mozilla Foundation and the Apache Software Foundation but also organizations such as NASA, Facebook and The New York Times.

Bugzilla supports all of the features you might expect in a high-quality issue tracking solution, including the ability to track the status of reported bugs, assign and change the status of bugs, and create useful reports analyzing metrics such as bug reporting frequency. Although not written in PHP, it's a Perl-based project meaning you'll be able to run it on any server capable of supporting PHP.

Write your comment now