php 框架测试,PHPUnit入门篇

#### PHPUnit是什么?

它是一款轻量级的php测试框架

#### 为什么要用PHPUnit?

1. facebook在用

2. 可以通过命令操控测试脚本

3. 可以测试性能

4. 可以测试代码覆盖率

5. 可以自动化的更新测试用例的参数数据

6. 各种格式的日志

6. 最最重要的是,功能强大,使用特别简单

#### PHPUnit的安装

~~~

pear channel-discover pear.phpunit.de

pear install phpunit/PHPUnit

~~~

#### 快速入门

~~~

require_once 'PHPUnit/Framework.php';

class ArrayTest extends PHPUnit_Framework_TestCase

{

public function testNewArrayIsEmpty()

{

// 创建数组fixture。

$fixture = array();

// 断言数组fixture的尺寸是0。

$this->assertEquals(0, sizeof($fixture));

}

}

?>

~~~

1. ArrayTest为测试类

2. ArrayTest 继承于PHPUnit_Framework_TestCase

3. 测试方法`testNewArrayIsEmpty()`,测试方法必须为`public`权限,一般以`test`开头,或者你也可以选择给其加注释`@test`来表明该函数为测试函数

~~~

/**

* @test

*/

public function testNewArrayIsEmpty()

{

$fixture = array();

$this->assertEquals(0, sizeof($fixture));

}

~~~

#### 命令行启动测试

phpunit 测试文件名,测试`ArrayTest.php`文件

~~~

phpunit ArrayTest

PHPUnit 3.2.10 by Sebastian Bergmann.

..

Time: 0 seconds

OK (2 tests)

~~~

#### 命令行参数

~~~

phpunit --help

PHPUnit 3.2.10 by Sebastian Bergmann.

Usage: phpunit [switches] UnitTest [UnitTest.php]

--log-graphviz Log test execution in GraphViz markup.

--log-json Log test execution in JSON format.

--log-tap Log test execution in TAP format to file.

--log-xml Log test execution in XML format to file.

--log-metrics Write metrics report in XML format.

--log-pmd Write violations report in PMD XML format.

--coverage-html

--coverage-xml Write code coverage information in XML format.

--test-db-dsn DSN for the test database.

--test-db-log-rev Revision information for database logging.

--test-db-prefix ... Prefix that should be stripped from filenames.

--test-db-log-info ... Additional information for database logging.

--testdox-html Write agile documentation in HTML format to file.

--testdox-text Write agile documentation in Text format to file.

--filter Filter which tests to run.

--group ... Only runs tests from the specified group(s).

--exclude-group ... Exclude tests from the specified group(s).

--loader TestSuiteLoader implementation to use.

--repeat Runs the test(s) repeatedly.

--tap Report test execution progress in TAP format.

--testdox Report test execution progress in TestDox format.

--no-syntax-check Disable syntax check of test source files.

--stop-on-failure Stop execution upon first error or failure.

--verbose Output more verbose information.

--wait Waits for a keystroke after each test.

--skeleton Generate skeleton UnitTest class for Unit in Unit.php.

--help Prints this usage information.

--version Prints the version and exits.

--configuration Read configuration from XML file.

-d key[=value] Sets a php.ini value.

~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值