php 单元测试
单元测试是提高代码质量,查漏补缺的,和平时的代码调试不一样
框架
1、phpunit/
中文文档
https://phpunit.readthedocs.io/zh_CN/latest/
2、ci-phpunit-test/ (An easier way to use PHPUnit with CodeIgniter 3.x. http://kenjis.github.io/ci-phpunit-test/)
3、VisualPHPUnit 一个可视化的测试工具(没研究)
http://visualphpunit.github.io/VisualPHPUnit/
http://visualphpunit.github.io/VisualPHPUnit/api/index.xhtml
如何在自己的项目中使用PHPUnit
Getting Started with PHPUnit
https://phpunit.de/getting-started-with-phpunit.html
https://phpunit.de/getting-started/phpunit-7.html
1、composer init
把下面内容加入到 composer.json 中(被测试类的位置)
"autoload": {
"classmap": [
"src/"
]
},
2、composer require --dev phpunit/phpunit ^7
3、 ./vendor/bin/phpunit --version
4、写测试类、被测试类
被测试的类
src/Email.php
<?php
declare(strict_types=1);
final class Email
{
private $email;
private function __construct(string $email)
{
$this->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
)
);
}
}
}
测试的类
tests/EmailTest.php
========demo==========
<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class EmailTest extends TestCase
{
public function testCanBeCreatedFromValidEmailAddress(): void
{
$this->assertInstanceOf(
Email::class,
Email::fromString('user@example.com')
);
}
public function testCannotBeCreatedFromInvalidEmailAddress(): void
{
$this->expectException(InvalidArgumentException::class);
Email::fromString('invalid');
}
public function testCanBeUsedAsString(): void
{
$this->assertEquals(
'user@example.com',
Email::fromString('user@example.com')
);
}
}
========demo==========
========遇到的知识点==========
安装全局PHPUnit 命令行(https://phpunit.readthedocs.io/en/7.4/installation.html)
$ wget https://phar.phpunit.de/phpunit-|version|.phar
$ chmod +x phpunit-|version|.phar
$ sudo mv phpunit-|version|.phar /usr/local/bin/phpunit
$ phpunit --version
PHPUnit x.y.z by Sebastian Bergmann and contributors.
Archives 应该是安装包的意思,二进制包的意思
// autoload_classmap.php @generated by Composer
========遇到的知识点==========
5、执行 命令行执行某个测试单元
./vendor/bin/phpunit --bootstrap vendor/autoload.php tests/EmailTest.php
((
--bootstrap vendor/autoload.php
指定 autoload.php
))
6、执行 tests 目录下所有测试样例
./vendor/bin/phpunit --bootstrap vendor/autoload.php --testdox tests
另一个框架 ci-phpunit-test/(An easier way to use PHPUnit with CodeIgniter 3.x http://kenjis.github.io/ci-phpunit-test/)
1、进入 CodeIgniter 目录 (/Users/peng/lab/phpframe/CodeIgniter)
cd /path/to/codeigniter/
2、更新下依赖包 composer install 3、安装此包 composer require kenjis/ci-phpunit-test --dev
4、执行 =======》 生成测试类 php vendor/kenjis/ci-phpunit-test/install.php
测试类在 application/tests/_ci_phpunit_test/patcher/third_party/PHP-Parser-3.0.3/lib/PhpParser/PrettyPrinterAbstract.php
5、Run Tests
项目根目录下
./vendor/bin/phpunit --bootstrap vendor/autoload.php application/tests/TestCase.php
phpunit
phpunit 相关知识
断言
https://www.kancloud.cn/manual/phpunit-book/68616
assertArrayHasKey()
assertInstanceOf
assertClassHasAttribute()
assertClassHasStaticAttribute()
assertContains()
assertContainsOnly()
assertContainsOnlyInstancesOf()
assertCount()
assertEmpty()
assertEqualXMLStructure()
assertEquals()
assertFalse()
assertFileEquals()
assertFileExists()
assertGreaterThan()
assertGreaterThanOrEqual()
assertInfinite()
assertInstanceOf()
assertInternalType()
assertJsonFileEqualsJsonFile()
assertJsonStringEqualsJsonFile
assertJsonStringEqualsJsonString
assertLessThan()
assertLessThanOrEqual()
assertNan()
assertNull()
assertObjectHasAttribute
assertRegExp()
assertStringMatchesFormat()
assertStringMatchesFormatFile()
assertSame()
assertStringEndsWith()
assertStringEqualsFile()
assertStringStartsWith()
assertThat()
assertTrue()
assertXmlFileEqualsXmlFile()
assertXmlStringEqualsXmlFile()
assertXmlStringEqualsXmlString()