AmMrWu

a fish.

导航

PHPUnit单元测试学习和试用

<?php
/*PHPUnit单元测试学习和试用
 * 环境:widnows zendstudio 7.2
 * 右击项目:properties PHPIncludePath Library Add PHPUnitCase
 * 新建PHPUnit Test Case 自动生成如下文件
 * Run As PHPUnit Case :alt+shit+x U
 */
require_once 'PHPUnit\Framework\TestCase.php';

/**
 * test case.
 */
class PHPTest extends PHPUnit_Framework_TestCase {
    /**
     * Prepares the environment before running a test.
     */
    protected function setUp() {
        parent::setUp ();
    // TODO Auto-generated PHPTest::setUp()
    }
    
    /**
     * Cleans up the environment after running a test.
     */
    protected function tearDown() {
        // TODO Auto-generated PHPTest::tearDown()
        parent::tearDown ();
    }
    
    /**
     * Constructs the test case.
     */
    public function __construct() {
        // TODO Auto-generated constructor
    }
    
    
    /*
     * 测试方法名 前面加 test+测试方法名
     * 注意 lessThan 是 后者lessThan前者
     */
    public function testFun()
    {
        $this->assertEquals(1,$this->Fun());//判断是否相等
        $this->assertNotEquals(2,$this->Fun());//不相等
        $this->assertLessThan(2,$this->Fun());
        $this->assertLessThanOrEqual(2,$this->Fun());
        $this->assertGreaterThan(0,$this->Fun());
        
        
    }
    //被测试的方法
    public function Fun()
    {
        return 1;
    }
    
    public function testFun1()
    {
        $this->assertArrayHasKey('one',$this->Fun1());
        $this->assertContains(1,$this->Fun1(),'Fun1() return not contains 1');
    }
    
    public function Fun1()
    {
     return array('one'=>1,'two'=>2);
    
    }
    
    
    

}

posted on 2012-09-28 14:37  RorySmart  阅读(411)  评论(0编辑  收藏  举报