PHPUnit Manual
Edition for PHPUnit 11.5. Updated on Oct 20, 2025.
Sebastian Bergmann
This work is licensed under the Creative Commons Attribution 3.0 Unported License.
Contents:
- 1. Installation
- 2. Writing Tests for PHPUnit
- 3. The Command-Line Test Runner
- 4. Organizing Tests
- 5. Fixtures
- 6. Test Doubles
- Test Stubs
- Mock Objects
- MockBuilder API
setMockClassName()setConstructorArgs()disableOriginalConstructor()disableOriginalClone()enableArgumentCloning()disableAutoReturnValueGeneration()disallowMockingUnknownTypes()disableAutoload()enableProxyingToOriginalMethods()onlyMethods()addMethods()getMock()getMockForAbstractClass()getMockForTrait()- Set-Hooked Properties
- 7. Code Coverage
- 8. Risky Tests
- 9. Error Handling
- 10. Extending PHPUnit
Appendix
- 1. Assertions
- Static vs. Non-Static Usage of Assertion Methods
- Boolean
- Identity
- Equality
- Iterable
assertArrayHasKey()assertContains()assertContainsOnly()assertContainsOnlyArray()assertContainsOnlyBool()assertContainsOnlyCallable()assertContainsOnlyFloat()assertContainsOnlyInt()assertContainsOnlyIterable()assertContainsOnlyNull()assertContainsOnlyNumeric()assertContainsOnlyObject()assertContainsOnlyResource()assertContainsOnlyClosedResource()assertContainsOnlyScalar()assertContainsOnlyString()assertContainsOnlyInstancesOf()
- Objects
- Cardinality
- Types
- Strings
assertStringStartsWith()assertStringEndsWith()assertStringContainsString()assertStringContainsStringIgnoringCase()assertStringEqualsStringIgnoringLineEndings()assertMatchesRegularExpression()assertStringMatchesFormat()assertStringMatchesFormatFile()assertFileMatchesFormat()assertFileMatchesFormatFile()assertStringEqualsFile()
- JSON
- XML
- Filesystem
- Math
- Constraints
- 2. Attributes
- 3. Annotations
- @author
- @after
- @afterClass
- @backupGlobals
- @backupStaticAttributes
- @before
- @beforeClass
- @codeCoverageIgnore*
- @covers
- @coversDefaultClass
- @coversNothing
- @dataProvider
- @depends
- @doesNotPerformAssertions
- @group
- @large
- @medium
- @preserveGlobalState
- @requires
- @runTestsInSeparateProcesses
- @runInSeparateProcess
- @small
- @test
- @testdox
- @testWith
- @ticket
- @uses
- 4. Events
- 5. The XML Configuration File
- The
<phpunit>Element- The
backupGlobalsAttribute - The
backupStaticPropertiesAttribute - The
bootstrapAttribute - The
cacheDirectoryAttribute - The
cacheResultAttribute - The
colorsAttribute - The
columnsAttribute - The
controlGarbageCollectorAttribute - The
numberOfTestsBeforeGarbageCollectionAttribute - The
requireCoverageMetadataAttribute - The
processIsolationAttribute - The
stopOnDefectAttribute - The
stopOnErrorAttribute - The
stopOnFailureAttribute - The
stopOnWarningAttribute - The
stopOnRiskyAttribute - The
stopOnDeprecationAttribute - The
stopOnNoticeAttribute - The
stopOnSkippedAttribute - The
stopOnIncompleteAttribute - The
failOnAllIssuesAttribute - The
failOnEmptyTestSuiteAttribute - The
failOnWarningAttribute - The
failOnRiskyAttribute - The
failOnDeprecationAttribute - The
failOnPhpunitDeprecationAttribute - The
failOnNoticeAttribute - The
failOnSkippedAttribute - The
failOnIncompleteAttribute - The
beStrictAboutChangesToGlobalStateAttribute - The
beStrictAboutOutputDuringTestsAttribute - The
beStrictAboutTestsThatDoNotTestAnythingAttribute - The
beStrictAboutCoverageMetadataAttribute - The
enforceTimeLimitAttribute - The
defaultTimeLimitAttribute - The
timeoutForSmallTestsAttribute - The
timeoutForMediumTestsAttribute - The
timeoutForLargeTestsAttribute - The
defaultTestSuiteAttribute - The
stderrAttribute - The
reverseDefectListAttribute - The
registerMockObjectsFromTestArgumentsRecursivelyAttribute - The
extensionsDirectoryAttribute - The
executionOrderAttribute - The
resolveDependenciesAttribute - The
testdoxAttribute - The
testdoxSummaryAttribute - The
displayDetailsOnAllIssuesAttribute - The
displayDetailsOnIncompleteTestsAttribute - The
displayDetailsOnSkippedTestsAttribute - The
displayDetailsOnTestsThatTriggerDeprecationsAttribute - The
displayDetailsOnPhpunitDeprecationsAttribute - The
displayDetailsOnTestsThatTriggerErrorsAttribute - The
displayDetailsOnTestsThatTriggerNoticesAttribute - The
displayDetailsOnTestsThatTriggerWarningsAttribute - The
shortenArraysForExportThresholdAttribute
- The
- The
<testsuites>Element - The
<source>Element- The
<include>Element - The
<exclude>Element - The
<directory>Element - The
<file>Element - The
<deprecationTrigger>Element - The
<ignoreSelfDeprecations>Attribute - The
<ignoreDirectDeprecations>Attribute - The
<ignoreIndirectDeprecations>Attribute - The
<restrictDeprecations>Attribute - The
<restrictNotices>Attribute - The
<restrictWarnings>Attribute - The
<baseline>Attribute - The
<ignoreSuppressionOfDeprecations>Attribute - The
<ignoreSuppressionOfPhpDeprecations>Attribute - The
<ignoreSuppressionOfErrors>Attribute - The
<ignoreSuppressionOfNotices>Attribute - The
<ignoreSuppressionOfPhpNotices>Attribute - The
<ignoreSuppressionOfWarnings>Attribute - The
<ignoreSuppressionOfPhpWarnings>Attribute
- The
- The
<coverage>Element - The
<logging>Element - The
<groups>Element - The
<extensions>Element - The
<php>Element
- The
- 6. Copyright