Using PHPUnit: Difference between revisions
Jump to navigation
Jump to search
| (3 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
== | [[Category:Unit Testing]] [[Category:PHP]] [[Category:Web Development]] | ||
== Configuring unit tests in PHPStorm == | |||
=== | === Composer === | ||
Add PHPUnit to composer: <ref>[https://phpunit.de/getting-started/phpunit-8.html Getting Started With PHPUnit 8] - PHPUnit Documentation</ref> | |||
<syntaxhighlight lang="javascript"> | |||
"require-dev": { | |||
"phpunit/phpunit": "^8" | |||
} | |||
<syntaxhighlight lang=" | |||
/ | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Update installed composer dependencies: | |||
< | <pre> | ||
php composer.phar update | |||
</pre> | |||
</ | |||
=== Creating unit tests === | |||
Test class names start with "Test" and test function names start with "test". | |||
Test classes extend `PHPUnit\Framework\TestCase`. | |||
<syntaxhighlight lang=" | <syntaxhighlight lang="php"> | ||
<?php | |||
namespace Littled\Tests\Account; | |||
use Littled\Account\Address; | |||
use PHPUnit\Framework\TestCase; | |||
class AddressTest extends TestCase | |||
$ | { | ||
public function testInitialValues() | |||
{ | |||
$addr = new Address(); | |||
self::assertInstanceOf('Littled\Request\IntegerInput', $addr->id); | |||
self::assertEquals(100, $addr->address2->sizeLimit); | |||
/* etc... */ | |||
} | |||
} | |||
</syntaxhighlight> | </syntaxhighlight> | ||
== | === Running unit tests === | ||
=== | |||
< | Right click on a TestCase class or a directory containing TestCase classes and choose '''Run''' ( <kbd>⌃</kbd> <kbd>⇧</kbd> <kbd>R</kbd> ) | ||
</ | |||
=== Generating unit test classes === | |||
PHPStorm can be configured such that when a new class is created, a corresponding class can be added to unit tests. | |||
* Mark the class directory as the root for the app code. | |||
* Mark the directory containing the unit tests as a tests directory (right click > Mark Directory As > Test Sources Root) | |||
* Create a new PHP class file. | |||
* Right click on the file and create the unit test. | |||
== Notes == | == Notes == | ||
Latest revision as of 17:50, 6 September 2019
Configuring unit tests in PHPStorm[edit]
Composer[edit]
Add PHPUnit to composer: [1]
"require-dev": {
"phpunit/phpunit": "^8"
}
Update installed composer dependencies:
php composer.phar update
Creating unit tests[edit]
Test class names start with "Test" and test function names start with "test".
Test classes extend PHPUnit\Framework\TestCase.
<?php
namespace Littled\Tests\Account;
use Littled\Account\Address;
use PHPUnit\Framework\TestCase;
class AddressTest extends TestCase
{
public function testInitialValues()
{
$addr = new Address();
self::assertInstanceOf('Littled\Request\IntegerInput', $addr->id);
self::assertEquals(100, $addr->address2->sizeLimit);
/* etc... */
}
}
Running unit tests[edit]
Right click on a TestCase class or a directory containing TestCase classes and choose Run ( ⌃ ⇧ R )
Generating unit test classes[edit]
PHPStorm can be configured such that when a new class is created, a corresponding class can be added to unit tests.
- Mark the class directory as the root for the app code.
- Mark the directory containing the unit tests as a tests directory (right click > Mark Directory As > Test Sources Root)
- Create a new PHP class file.
- Right click on the file and create the unit test.
Notes[edit]
See also[edit]
- Getting Started With PHPUnit - PHPUnit Documentation
Footnotes[edit]
- ↑ Getting Started With PHPUnit 8 - PHPUnit Documentation