SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHPUnit Question?

    Hello,

    I am really new to PHP but understand things a little and after studying OOP after sometime I wanted to learn how to test my code. I have been reading the manual and searching tutorials but I cannot seem to understand how to right a simple test for something like this

    PHP Code:
     class someclass {
             
             public function 
    somefunction($value){

             return 
    $value
    }

    This has to be easy but I don't think I am getting it, how would I write a test for this.

    I understand how to write the test class and extend PHPUnit frame work and do the setup and the teardown but how do I actually test this method? What I mean is how do I either pass a value to the $value to test it in my test case?

    I know this is a basic example but I really do not understand the lingo on the PHPUnit manual site, any help would be awesome
    Last edited by Mittineague; Aug 28, 2011 at 19:23. Reason: fixing bbcode tag

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2005
    Location
    Winnipeg
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You wouldn't actually write a test for an empty method because there is nothing to test.

    You typically test for exceptions being thrown, conditionals and return values.

    I suppose in this case/your example you could pass a value to the method and ensure the value you provided was the same as the one returned and if it isn't you know something in the code has changed and therefore a likely bug has been introduced.

    Cheers,
    Alex
    The only constant in software is change itself

  3. #3
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    692
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    Something like:
    PHP Code:
    class InitialTests extends \PHPUnit_Framework_TestCase
    {
      public static function 
    setUpBeforeClass()
      {
        
    // echo "Setup before class\n";
      
    }
      public function 
    setUp()
      {
        
    // echo "Setup before test\n";
      
    }
      function 
    test1()
      {
        
    $this->assertTrue(true);
      }
      function 
    test2()
      {
        
    $this->assertTrue(false);
      }
      function 
    testSomeFunction
      
    {
        
    $someClass = new someclass();
        
    $input 'XXX';
        
    $output $someClass->somefunction($input);
        
    $this->assertEquals($input,$output);
      }


  4. #4
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is what I was looking for, how to pass things to a method for testing.

    But what was really throwing me off is PHPUnit wasn't including the file with the class I am testing correctly. I still have the problem but I am using a portable version and don't understand yet what is going on.


    Do any of you know how portable version of PHPUnit work?

    I get class not found errors I tried it on a few separate test and none of them are working. Everyone gets class not found errors.

    Would having PHPUnit installed on server and having Portable version running from directories using includes be causing this?

    What might be some things to look into as far as fixing the class not found, is it some sorta config file?

    Thank you for all the help I know these are very newbie questions

    I searched the web and found many having problems with this but don't fully understand how there fixing the problem.

  5. #5
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    692
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    I'm not sure what you mean by portable version. PHPUnit doesn't do anything special about loading classes. Just include them directly or setup an autoload system of some sort. Makeing sure your classes get loaded is not PHPunit's responsibility.

  6. #6
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, figured it out. I had to create a test-runner to include all the stuff I needed. Thanks for the help.

  7. #7
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using the code example below what kind of things or how would I think about testing this. In plain English what would an experienced person test for?

    Should I also right a test if something would pass a string to this function that it would fail?

    Trying to understand the point behind unit testing and seeing some feedback on actual code would really help me

    I wrote a test to see if the sizeof(array) would be equal to the size expected. Is this what one should do?

    PHP Code:
    public function createarrays($begin$end){

            
    $result = array();

            for(
    $int=$begin$int<=$end$int++){
                
    $result[] = $int;
            }

            return 
    $result;
        } 

  8. #8
    SitePoint Member
    Join Date
    May 2011
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The first thing you want to understand about Unit Testing (besides its purpose) is the types of testing you do in your functions. I like to break them down like this:

    Functional Testing: You make assertions about what didn't happen. You got a value back from a function for example and it should never be null, assert that the value isn't null.

    Behavior Testing: You're using objects with that have encapsulated data, you test a function that changes the state of properties in your object. You'll need to make assertions about the changed state of that object, did it change?

    Integration Testing: This isn't really much unit testing, but it matters. You have code that interacts with other objects or even a database. Test what was sent and make sure it's all the same.

  9. #9
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    692
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    Write one test that actually tests the values of the returned array. That will also serve as documentation for just what the function does. And then write a few test for boundary or unusual conditions. For example, what happens if $end is less that $begin? What is the expected behavior if $end == $begin? What happens if strings are passed instead of integers? All kids of good stuff could be tested for.

  10. #10
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    re your first test function - to follow the ideals of TDD (Test Driven Design) you'd start of with a class with no contents, then expect to see a red line as you run the first test, below - because it is very easy to accidentally load previous classes. Getting a red line often tells you just as much as getting a green one.

    These are perhaps the tests you'd run
    PHP Code:
    testSomeFunctionExists(){}

    //testSomeFunctionReturnsExpectedValueString(){}

    //testSomeFunctionReturnsExpectedValueInt(){}

    //testSomeFunctionWithNoArgReturnsError(){} 
    These are stubbed out because I can only tell you the SimpleTest equivalents.

    It is not to say that these tests would remain - as you refactor and build your way out from this basic premise the names and content of your tests naturally have to evolve too.

    Read half way down this page, its only a few paragraphs - but switched me on to TDD and the red line/green line mantra.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •