PHPUnitWriting Tests for PHPUnit

The basic conventions and steps for writing tests with PHPUnit:

1. The tests for a class Class go into a class ClassTest.

2. ClassTest inherits (most of the time) from PHPUnit_Framework_TestCase.

3. The tests are public methods that are named test*.

  Alternatively, you can use the @test annotation in a method's docblock to mark it as a test method.

4. Inside the test methods, assertion methods such as assertEquals() are used to assert that an actual value matches an expected value.

<?php
class StackTest extends PHPUnit_Framework_TestCase
{
    public function testPushAndPop()
    {
        $stack = array();
        $this->assertEquals(0, count($stack));
 
        array_push($stack, 'foo');
        $this->assertEquals('foo', $stack[count($stack)-1]);
        $this->assertEquals(1, count($stack));
 
        $this->assertEquals('foo', array_pop($stack));
        $this->assertEquals(0, count($stack));
    }
}
?>

 Whenever you are tempted to type something into a print statement or a debugger expression, write it as a test instead.

原文地址:https://www.cnblogs.com/Hebe/p/3099675.html