PHP Unit Testing & Mock Objects with SimpleTest plugin for Eclipse

Categories PHP
0

Install and configure SimpleTest

1. Download the SimpleTest Eclipse plugin from https://sourceforge.net/project/showfiles.php?group_id=76550;

2. Install it using Eclipse Update Manager;

3. Configure it as follows:

Let’s run a simple test within Eclipse

4. Add a php file in a PHP project; Write a test like this (Note the function name must start with “test”; The setUp and tearDown function will be executed for every test method):

<?php
    class test1 extends UnitTestCase {
      function test_pass(){
        $x = 1;
        $y = 2;
        $total = $x + $y;
        $this->assertEqual(3,$total, "This should pass");
      }
    }
?>

5. Select “Run As” > “SimpleTest”; The test results will be shown in the Result View:

6. Enable Autocompletion (Content Assist) by Letting eclipse locate the SimpleTest library (As an alternative, you can also copy simpletest folder into your project):

Yet another way to run the test

8. Copy the simpletest folder to your project folder;

9. Include some libraries in your test file:

<?php
    //Copy SimpleTest folder to your project first and then include it.
    include_once("simpletest/unit_tester.php");
    include_once("simpletest/reporter.php");

    class test1 extends UnitTestCase {
      //Test function's name should start with "test"
      function test_pass(){
        $x = 1;
        $y = 2;
        $total = $x + $y;
        $this->assertEqual(3,$total, "This should pass");
      }
    }

    //Run and show the test result in the web browser.
    $test = new test1();
    $test->run(new HtmlReporter());
?>

10. “Run As” > PHP Web Page, then you should see something like this:

Run a group of tests

11. Use TestSuite and run it within Eclipse:

<?php
    class AllTests extends TestSuite {
        function AllTests(){
            $this->TestSuite("All Tests");
            $this->addFile("D:Warrensamplesphp_pdtHelloPHPBeginningArrayTest.php");
            $this->addFile("D:Warrensamplesphp_pdtHelloPHPBeginningAnotherTest.php");            
        }
    }
?>

12. View the results:

13. Run TestSuite in the web browser:

<?php
    include_once("../simpletest/test_case.php");
    include_once("../simpletest/unit_tester.php");
    include_once("../simpletest/reporter.php");

    $all = new TestSuite();
    $all->addFile("ArrayTest.php");
    $all->run(new HtmlReporter());
?>

14. View the results:

Debugging SimpleTest Tests

15. Open the file for all tests in step 13, then select “Debug As” > “PHP Web Page” or “PHP Script”. (Hint: Use “Main Menu > Run > Remove All Breakpoints” to clear breakpoints conveniently.)

Reference

http://www.simpletest.org/
http://simpletest.sourceforge.net/en/extension_eclipse.html

SimpleTest Documentation
http://www.simpletest.org/en/start-testing.html
SimpleTest mock objects documentation
http://www.simpletest.org/en/mock_objects_documentation.html
UnitTesting in PHP using SimpleTest
http://www.devpapers.com/article/303

0 thoughts on “PHP Unit Testing & Mock Objects with SimpleTest plugin for Eclipse

  1. Pingback: PHP | Warren Tang's Blog

  2. Pingback: Warren Tang Blog: PHP Unit Testing & Mock Objects mit SimpleTest Plugin für Eclipse | PHP Boutique

  3. Pingback: simpletest 를 eclipse plugin을 이용해서 사용하자 | 정우아빠 사이트

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>