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

 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:)

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

Something like:


class InitialTests extends \\PHPUnit_Framework_TestCase
{
  public static function setUpBeforeClass()
  {
    // echo "Setup before class\
";
  }
  public function setUp()
  {
    // echo "Setup before test\
";
  }
  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);
  }
}

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.

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.

Sorry, figured it out. I had to create a test-runner to include all the stuff I needed. Thanks for the help.

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?

public function createarrays($begin, $end){

		$result = array();

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

		return $result;
	}

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.

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.

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


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.