PHP单元测试PHPUnit
简介
php -m | grep '模块名称'PHPUnit的安装
PHP档案包方式
wget https://phar.phpunit.de/phpunit.phar # 下载档案包
chmod +x phpunit.phar # 赋可执行权限
sudo mv phpunit.phar /usr/local/bin/phpunit
phpunit --version # 查看PHPUnit版本php -m | grep '模块名称'wget https://phar.phpunit.de/phpunit.phar # 下载档案包
chmod +x phpunit.phar # 赋可执行权限
sudo mv phpunit.phar /usr/local/bin/phpunit
phpunit --version # 查看PHPUnit版本{
"require-dev": {
"phpunit/phpunit": "5.0.*"
}
}<?php
class HelloWolrdTest extends PHPUnit_Framework_TestCase
{
public function testString()
{
$string = 'Hello, World';
$this->assertEquals('Hello, World', $string);
$this->assertNotEquals('Hello,World', $string);
}
public function testAarry()
{
$arr = array();
$this->assertEmpty($arr);
//$this->assertArrayHasKey('hello', $arr);
$arr['hello'] = 'world';
//$this->assertEmpty($arr);
$this->assertArrayHasKey('hello', $arr);
}
}phpunit HelloWorldTestPHPUnit 4.8.24 by Sebastian Bergmann and contributors.
..
Time: 103 ms, Memory: 12.50Mb
OK (2 tests, 4 assertions)<?php class DependenceTest extends PHPUnit_Framework_TestCase
{
public function testEmpty()
{
$arr = array();
$this->assertEmpty($arr);
return $arr;
}
/**
* @depends testEmpty
*/
public function testPush(array $arr)
{
array_push($arr, 'foo');
$this->assertEquals('foo', $arr[count($arr) - 1]);
$this->assertNotEmpty($arr);
return $arr;
}
/**
* @depends testPush
*/
public function testPop(array $arr)
{
$this->assertEquals('foo', array_pop($arr));
$this->assertEmpty($arr);
}
public function testOne()
{
$this->assertTrue(false);
}
/*
* @depends testOne
*/
public function testTwo()
{
$this->assertTrue(1);
}
public function testProducerFirst()
{
$this->assertTrue(true);
return 'first';
}
public function testProducerSecond()
{
$this->assertTrue(true);
return 'second';
}
/**
* @depends testProducerFirst
* @depends testProducerSecond
*/
public function testOneConsumer()
{
$this->assertEquals(
array('first', 'second'),
func_get_args()
);
}
/**
* @depends testProducerSecond
* @depends testProducerFirst
*/
public function testTwoConsumer()
{
$this->assertEquals(
array('first', 'second'),
func_get_args()
);
}
}
<?php
class DataProviderTest extends PHPUnit_Framework_TestCase
{
public function getTestAddData()
{
return array(
array(0, 0, 0),
array(0, 1, 1),
array(1, 1, 2),
array(2, 2, 5),
);
}
/**
* @dataProvider getTestAddData
*/
public function testAdd($op1, $op2, $sum)
{
$this->assertEquals($sum, $op1 + $op2);
}
/**
* @dataProvider getTestMulData
*/
public function testMul($op1, $op2, $mul)
{
$this->assertEquals($mul, $op1 * $op2);
}
public function getTestMulData()
{
return array(
array(1, 1, 1),
array(2, 3, 6),
array(3, 3, 8),
);
}
}
<?php
class ExceptionTest extends PHPUnit_Framework_TestCase
{
/**
* @expectedException InvalidArgumentException
*/
public function testException()
{
throw new InvalidArgumentException('', 0);
//throw new Exception('', 0);
}
/**
* @expectedException Exception
* @expectedExceptionCode 404
*/
public function testExceptionCode()
{
throw new Exception('exception', 404);
//throw new Exception('exception', 30);
}
/**
* @expectedException Exception
* @expectedExceptionMessage test
*/
public function testExceptionMessage()
{
throw new Exception('test', 0);
//throw new Exception('abcdefg', 0);
}
/**
* @expectedException Exception
* @expectedExceptionMessageRegExp /[a-z]+/
*/
public function testExceptionMessageRegExp()
{
throw new Exception('test', 0);
//throw new Exception('TEST', 0);
}
public function testOne()
{
$this->assertEquals(1, 1);
}
}<?php
class OutputTest extends PHPUnit_Framework_TestCase
{
public function testOutputOne()
{
$this->expectOutputString('foo');
print 'test';
}
public function testOutputTwo()
{
$this->expectOutputString('foo');
print 'foo';
}
}<?php
class SummaryTest extends PHPUnit_Framework_TestCase
{
public function testSuccess()
{
$this->assertEquals(1, 1);
}
public function testFail()
{
$this->assertEquals(2, 1);
}
public function testException()
{
throw new Exception('', 0);
}
/*
* @depends testThree
*/
public function testSkip()
{
$this->assertEquals(2, 1);
}
}phpunit SummaryTestPHPUnit 4.8.24 by Sebastian Bergmann and contributors.
.FEF
Time: 96 ms, Memory: 12.00Mb
There was 1 error:
1) SummaryTest::testException
Exception:
/Users/kenneth/codes/php/phpunit/SummaryTest.php:18
--
There were 2 failures:
1) SummaryTest::testFail
Failed asserting that 1 matches expected 2.
/Users/kenneth/codes/php/phpunit/SummaryTest.php:13
2) SummaryTest::testSkip
Failed asserting that 1 matches expected 2.
/Users/kenneth/codes/php/phpunit/SummaryTest.php:26
FAILURES!
Tests: 4, Assertions: 3, Errors: 1, Failures: 2.