没有合适的资源?快使用搜索试试~ 我知道了~
PHP单元测试
需积分: 10 2 下载量 128 浏览量
2014-08-19
20:38:34
上传
评论
收藏 1.06MB PDF 举报
温馨提示
试读
217页
PHP单元测试入门,可以快速了解和掌握PHP单元测试方法,单元测试的基本知识等等
资源推荐
资源详情
资源评论
PHPUnit Manual
Sebastian Bergmann
PHPUnit Manual
Sebastian Bergmann
Publication date Edition for PHPUnit 3.7. Updated on 2013-09-27.
Copyright © 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Sebastian Bergmann
This work is licensed under the Creative Commons Attribution 3.0 Unported License.
iii
Table of Contents
1. Automating Tests ....................................................................................................... 1
2. PHPUnit's Goals ........................................................................................................ 3
3. Installing PHPUnit ..................................................................................................... 5
PHP Archive (PHAR) ............................................................................................ 5
Composer ............................................................................................................. 5
PEAR .................................................................................................................. 5
Optional packages .................................................................................................. 6
Upgrading ............................................................................................................ 8
4. Writing Tests for PHPUnit ......................................................................................... 10
Test Dependencies ............................................................................................... 10
Data Providers ..................................................................................................... 13
Testing Exceptions ............................................................................................... 17
Testing PHP Errors .............................................................................................. 21
Testing Output .................................................................................................... 23
Assertions ........................................................................................................... 24
assertArrayHasKey() ..................................................................................... 24
assertClassHasAttribute() ............................................................................... 25
assertClassHasStaticAttribute() ....................................................................... 26
assertContains() ........................................................................................... 27
assertContainsOnly() ..................................................................................... 28
assertContainsOnlyInstancesOf() ..................................................................... 29
assertCount() ............................................................................................... 30
assertEmpty() .............................................................................................. 31
assertEqualXMLStructure() ............................................................................ 32
assertEquals() .............................................................................................. 34
assertFalse() ................................................................................................ 41
assertFileEquals() ......................................................................................... 41
assertFileExists() .......................................................................................... 42
assertGreaterThan() ...................................................................................... 43
assertGreaterThanOrEqual() ........................................................................... 44
assertInstanceOf() ........................................................................................ 45
assertInternalType() ...................................................................................... 46
assertJsonFileEqualsJsonFile() ........................................................................ 47
assertJsonStringEqualsJsonFile() ..................................................................... 48
assertJsonStringEqualsJsonString() .................................................................. 49
assertLessThan() .......................................................................................... 50
assertLessThanOrEqual() ............................................................................... 51
assertNull() ................................................................................................. 52
assertObjectHasAttribute() ............................................................................. 52
assertRegExp() ............................................................................................ 53
assertStringMatchesFormat() .......................................................................... 54
assertStringMatchesFormatFile() ..................................................................... 55
assertSame() ................................................................................................ 56
assertSelectCount() ....................................................................................... 58
assertSelectEquals() ...................................................................................... 60
assertSelectRegExp() .................................................................................... 61
assertStringEndsWith() .................................................................................. 63
assertStringEqualsFile() ................................................................................. 64
assertStringStartsWith() ................................................................................. 65
assertTag() .................................................................................................. 66
assertThat() ................................................................................................. 68
assertTrue() ................................................................................................. 70
assertXmlFileEqualsXmlFile() ........................................................................ 71
assertXmlStringEqualsXmlFile() ..................................................................... 72
assertXmlStringEqualsXmlString() .................................................................. 73
PHPUnit Manual
iv
Error output ........................................................................................................ 75
Edge cases .................................................................................................. 77
5. The Command-Line Test Runner ................................................................................ 79
Command-Line switches ....................................................................................... 79
6. Fixtures .................................................................................................................. 86
More setUp() than tearDown() ............................................................................... 88
Variations ........................................................................................................... 89
Sharing Fixture .................................................................................................... 89
Global State ........................................................................................................ 89
7. Organizing Tests ...................................................................................................... 91
Composing a Test Suite Using the Filesystem ........................................................... 91
Composing a Test Suite Using XML Configuration ................................................... 92
8. Database Testing ...................................................................................................... 94
Supported Vendors for Database Testing .................................................................. 94
Difficulties in Database Testing .............................................................................. 94
The four stages of a database test ........................................................................... 95
1. Clean-Up Database ................................................................................... 95
2. Set up fixture .......................................................................................... 95
3–5. Run Test, Verify outcome and Teardown .................................................. 95
Configuration of a PHPUnit Database TestCase ........................................................ 95
Implementing getConnection() ....................................................................... 96
Implementing getDataSet() ............................................................................ 96
What about the Database Schema (DDL)? ........................................................ 97
Tip: Use your own Abstract Database TestCase ................................................. 97
Understanding DataSets and DataTables .................................................................. 98
Available Implementations ............................................................................ 99
Beware of Foreign Keys .............................................................................. 107
Implementing your own DataSets/DataTables .................................................. 107
The Connection API ........................................................................................... 108
Database Assertions API ..................................................................................... 109
Asserting the Row-Count of a Table .............................................................. 109
Asserting the State of a Table ...................................................................... 109
Asserting the Result of a Query .................................................................... 110
Asserting the State of Multiple Tables ........................................................... 110
Frequently Asked Questions ................................................................................. 111
Will PHPUnit (re-)create the database schema for each test? .............................. 111
Am I required to use PDO in my application for the Database Extension to
work? ....................................................................................................... 111
What can I do, when I get a “Too much Connections” Error? ............................. 111
How to handle NULL with Flat XML / CSV Datasets? ..................................... 112
9. Incomplete and Skipped Tests ................................................................................... 113
Incomplete Tests ................................................................................................ 113
Skipping Tests ................................................................................................... 114
Skipping Tests using @requires ............................................................................ 115
10. Test Doubles ........................................................................................................ 117
Stubs ................................................................................................................ 117
Mock Objects .................................................................................................... 122
Stubbing and Mocking Web Services ..................................................................... 127
Mocking the Filesystem ...................................................................................... 129
11. Testing Practices ................................................................................................... 132
During Development ........................................................................................... 132
During Debugging .............................................................................................. 132
12. Test-Driven Development ....................................................................................... 134
BankAccount Example ........................................................................................ 134
13. Behaviour-Driven Development ............................................................................... 139
BowlingGame Example ....................................................................................... 140
14. Code Coverage Analysis ........................................................................................ 145
Specifying Covered Methods ................................................................................ 147
PHPUnit Manual
v
Ignoring Code Blocks ......................................................................................... 149
Including and Excluding Files .............................................................................. 149
Edge cases ........................................................................................................ 150
15. Other Uses for Tests .............................................................................................. 151
Agile Documentation .......................................................................................... 151
Cross-Team Tests ............................................................................................... 151
16. Skeleton Generator ................................................................................................ 153
Generating a Test Case Class Skeleton ................................................................... 153
Generating a Class Skeleton from a Test Case Class ................................................. 155
17. PHPUnit and Selenium .......................................................................................... 158
Selenium Server ................................................................................................. 158
Installation ........................................................................................................ 158
PHPUnit_Extensions_Selenium2TestCase ............................................................... 158
PHPUnit_Extensions_SeleniumTestCase ................................................................ 159
18. Logging ............................................................................................................... 166
Test Results (XML) ............................................................................................ 166
Test Results (TAP) ............................................................................................. 167
Test Results (JSON) ........................................................................................... 167
Code Coverage (XML) ........................................................................................ 168
Code Coverage (TEXT) ...................................................................................... 168
19. Extending PHPUnit ............................................................................................... 170
Subclass PHPUnit_Framework_TestCase ................................................................ 170
Write custom assertions ....................................................................................... 170
Implement PHPUnit_Framework_TestListener ........................................................ 171
Subclass PHPUnit_Extensions_TestDecorator ......................................................... 172
Implement PHPUnit_Framework_Test ................................................................... 173
A. Assertions ............................................................................................................. 175
B. Annotations ........................................................................................................... 179
@author ............................................................................................................ 179
@backupGlobals ................................................................................................ 179
@backupStaticAttributes ...................................................................................... 179
@codeCoverageIgnore* ....................................................................................... 180
@covers ........................................................................................................... 180
@coversDefaultClass .......................................................................................... 181
@coversNothing ................................................................................................. 181
@dataProvider ................................................................................................... 182
@depends ......................................................................................................... 182
@expectedException ........................................................................................... 182
@expectedExceptionCode .................................................................................... 182
@expectedExceptionMessage ............................................................................... 183
@group ............................................................................................................ 183
@large .............................................................................................................. 184
@medium ......................................................................................................... 184
@preserveGlobalState ......................................................................................... 184
@requires ......................................................................................................... 184
@runTestsInSeparateProcesses ............................................................................. 185
@runInSeparateProcess ....................................................................................... 185
@small ............................................................................................................. 185
@test ................................................................................................................ 185
@testdox ........................................................................................................... 186
@ticket ............................................................................................................. 186
C. The XML Configuration File ................................................................................... 187
PHPUnit ........................................................................................................... 187
Test Suites ........................................................................................................ 188
Groups ............................................................................................................. 188
Including and Excluding Files for Code Coverage .................................................... 189
Logging ............................................................................................................ 189
Test Listeners .................................................................................................... 190
剩余216页未读,继续阅读
资源评论
qq_19774795
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功