- Saved searches
- Use saved searches to filter your results more quickly
- License
- sebastianbergmann/phpunit
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- Getting Started with PHPUnit 8
- Download
- PHP Archive (PHAR)
- Composer
- Code
- Test Code
- Test Execution
- PHP Archive (PHAR)
- Composer
- TestDox
- Getting Started with PHPUnit 10
- Download
- PHP Archive (PHAR)
- Composer
- Code
- Test Code
- Test Execution
- PHP Archive (PHAR)
- Composer
- TestDox
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
The PHP Unit Testing framework.
License
sebastianbergmann/phpunit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks.
We distribute a PHP Archive (PHAR) that has all required (as well as some optional) dependencies of PHPUnit bundled in a single file:
$ wget https://phar.phpunit.de/phpunit-X.Y.phar $ php phpunit-X.Y.phar --version
Please replace X.Y with the version of PHPUnit you are interested in.
Alternatively, you may use Composer to download and install PHPUnit as well as its dependencies. Please refer to the «Getting Started» guide for details on how to install PHPUnit.
Please refer to CONTRIBUTING.md for information on how to contribute to PHPUnit and its related projects.
Thanks to everyone who has contributed to PHPUnit! You can find a detailed list of contributors on every PHPUnit related package on GitHub. This list shows only the major components:
A very special thanks to everyone who has contributed to the documentation.
Getting Started with PHPUnit 8
This tutorial assumes that you use PHP 7.2 or PHP 7.3. You will learn how to write simple unit tests as well as how to download and run PHPUnit.
The documentation for PHPUnit 8 can be found here.
Download
PHP Archive (PHAR)
We distribute a PHP Archive (PHAR) that contains everything you need in order to use PHPUnit 8. Simply download it from here and make it executable:
Composer
You can add PHPUnit as a local, per-project, development-time dependency to your project using Composer:
➜ wget -O phpunit https://phar.phpunit.de/phpunit-8.phar ➜ chmod +x phpunit ➜ ./phpunit --version PHPUnit 8.0.0 by Sebastian Bergmann and contributors.
➜ composer require --dev phpunit/phpunit ^8 ➜ ./vendor/bin/phpunit --version PHPUnit 8.0.0 by Sebastian Bergmann and contributors.
Please refer to the documentation for details on how to verify PHAR releases of PHPUnit.
The example shown above assumes that composer is on your $PATH .
Your composer.json should look similar to this:
Code
ensureIsValidEmail($email); $this->email = $email; > public static function fromString(string $email): self < return new self($email); >public function __toString(): string < return $this->email; > private function ensureIsValidEmail(string $email): void < if (!filter_var($email, FILTER_VALIDATE_EMAIL)) < throw new InvalidArgumentException( sprintf( '"%s" is not a valid email address', $email ) ); >> >
Test Code
assertInstanceOf( Email::class, Email::fromString('[email protected]') ); > public function testCannotBeCreatedFromInvalidEmailAddress(): void < $this->expectException(InvalidArgumentException::class); Email::fromString('invalid'); > public function testCanBeUsedAsString(): void < $this->assertEquals( '[email protected]', Email::fromString('[email protected]') ); > >
Test Execution
PHP Archive (PHAR)
➜ ./phpunit --bootstrap src/autoload.php tests PHPUnit 8.0.0 by Sebastian Bergmann and contributors. . 3 / 3 (100%) Time: 70 ms, Memory: 10.00MB OK (3 tests, 3 assertions)
The above assumes that you have downloaded phpunit.phar and put it into your $PATH as phpunit and that src/autoload.php is a script that sets up autoloading for the classes that are to be tested. Such a script is commonly generated using a tool such as phpab.
Composer
➜ ./vendor/bin/phpunit tests PHPUnit 8.0.0 by Sebastian Bergmann and contributors. . 3 / 3 (100%) Time: 70 ms, Memory: 10.00MB OK (3 tests, 3 assertions)
The above assumes that vendor/autoload.php , the autoloader script managed by Composer, exists and is able to load the code for the Email class. Depending on how you set up autoloading, you may need to run composer dump-autoload now.
—bootstrap src/autoload.php instructs the PHPUnit command-line test runner to include src/autoload.php before the tests are run.
tests instructs the PHPUnit command-line test runner to execute all tests found declared in *Test.php sourcecode files in the tests directory.
tests instructs the PHPUnit command-line test runner to execute all tests found declared in *Test.php sourcecode files in the tests directory.
TestDox
Below you see an alternative output which is based on the idea that the name of a test can be used to document the behavior that is verified by the test:
➜ ./phpunit --bootstrap src/autoload.php --testdox tests PHPUnit 8.0.0 by Sebastian Bergmann and contributors. Email ✔ Can be created from valid email address ✔ Cannot be created from invalid email address ✔ Can be used as string
➜ ./vendor/bin/phpunit --testdox tests PHPUnit 8.0.0 by Sebastian Bergmann and contributors. Email ✔ Can be created from valid email address ✔ Cannot be created from invalid email address ✔ Can be used as string
Copyright © Sebastian Bergmann.
Getting Started with PHPUnit 10
This tutorial assumes that you use PHP 8.1 or PHP 8.2. You will learn how to write simple unit tests as well as how to download and run PHPUnit.
The documentation for PHPUnit 10 can be found here.
Download
PHP Archive (PHAR)
We distribute a PHP Archive (PHAR) that contains everything you need in order to use PHPUnit 10. Simply download it from here and make it executable:
Composer
You can add PHPUnit as a local, per-project, development-time dependency to your project using Composer:
➜ wget -O phpunit https://phar.phpunit.de/phpunit-10.phar ➜ chmod +x phpunit ➜ ./phpunit --version PHPUnit 10.0.0 by Sebastian Bergmann and contributors.
➜ composer require --dev phpunit/phpunit ^10 ➜ ./vendor/bin/phpunit --version PHPUnit 10.0.0 by Sebastian Bergmann and contributors.
Please refer to the documentation for details on how to verify PHAR releases of PHPUnit.
The example shown above assumes that composer is on your $PATH .
Your composer.json should look similar to this:
Code
ensureIsValidEmail($email); $this->email = $email; > public static function fromString(string $email): self < return new self($email); >public function asString(): string < return $this->email; > private function ensureIsValidEmail(string $email): void < if (!filter_var($email, FILTER_VALIDATE_EMAIL)) < throw new InvalidArgumentException( sprintf( '"%s" is not a valid email address', $email ) ); >> >
Test Code
[email protected]'; $email = Email::fromString($string); $this->assertSame($string, $email->asString()); > public function testCannotBeCreatedFromInvalidEmail(): void < $this->expectException(InvalidArgumentException::class); Email::fromString('invalid'); > >
Test Execution
PHP Archive (PHAR)
➜ ./phpunit --bootstrap src/autoload.php tests PHPUnit 10.0.0 by Sebastian Bergmann and contributors. .. 2 / 2 (100%) Time: 70 ms, Memory: 10.00MB OK (2 tests, 2 assertions)
The above assumes that you have downloaded phpunit.phar and put it into your $PATH as phpunit and that src/autoload.php is a script that sets up autoloading for the classes that are to be tested. Such a script is commonly generated using a tool such as phpab.
Composer
➜ ./vendor/bin/phpunit tests PHPUnit 10.0.0 by Sebastian Bergmann and contributors. .. 2 / 2 (100%) Time: 70 ms, Memory: 10.00MB OK (2 tests, 2 assertions)
The above assumes that vendor/autoload.php , the autoloader script managed by Composer, exists and is able to load the code for the Email class. Depending on how you set up autoloading, you may need to run composer dump-autoload now.
—bootstrap src/autoload.php instructs the PHPUnit command-line test runner to include src/autoload.php before the tests are run.
tests instructs the PHPUnit command-line test runner to execute all tests found declared in *Test.php sourcecode files in the tests directory.
tests instructs the PHPUnit command-line test runner to execute all tests found declared in *Test.php sourcecode files in the tests directory.
TestDox
Below you see an alternative output which is based on the idea that the name of a test can be used to document the behavior that is verified by the test:
➜ ./phpunit --bootstrap src/autoload.php --testdox tests PHPUnit 10.0.0 by Sebastian Bergmann and contributors. .. 2 / 2 (100%) Time: 70 ms, Memory: 10.00MB Email ✔ Can be created from valid email address ✔ Cannot be created from invalid email address OK (2 tests, 2 assertions)
➜ ./vendor/bin/phpunit --testdox tests PHPUnit 10.0.0 by Sebastian Bergmann and contributors. .. 2 / 2 (100%) Time: 70 ms, Memory: 10.00MB Email ✔ Can be created from valid email address ✔ Cannot be created from invalid email address OK (2 tests, 2 assertions)
Copyright © Sebastian Bergmann.