提供一个PHPUnit 中文文档:
https://www.kancloud.cn/manual/phpunit-book/68609
何为单元测试:
指对软件中的基本单元进行测试,如函数、方法等,以检查其返回值或行为是否符合预期;实际中软件是很复杂的,由许多组件构成,执行流程连贯在一起,要进行单元片段的测试,就需要为其提供执行上下文(或者说参数)和环境(比如打桩模拟一些对象)来运行,并监控其行为和返回值,为此我们就需要写出做这件事情的程序代码,这样的代码叫做测试用例,许多测试用例有机结合在一起形成一个整体的测试,又叫做测试套件,被测试的程序代码叫做生产代码。phpunit这个软件就是用来帮助我们写测试用例并进行测试的。
phpunit官网地址:https://phpunit.de/
那么我如何选择下载那个版本呢?
点击: currently supported versions 查看一下对应的版本
这里我们选择比较新的版本进行测试:
➜ wget -O phpunit https://phar.phpunit.de/phpunit-7.phar ➜ chmod +x phpunit ➜ ./phpunit --versionPHPUnit 7.0.0 by Sebastian Bergmann and contributors.
选择phpunit7 来使用。
我们看到已经下载完成。
下面来看一个使用案例:
yunke.php
代码如下:
<?php use PHPUnit\Framework\TestCase; class yunkeTest extends TestCase { public function testPushAndPop() { $stack = []; $this->assertEquals(0, count($stack)); array_push($stack, 'foo'); $this->assertEquals('foo', $stack[count($stack) - 1]); $this->assertEquals(1, count($stack)); $this->assertEquals('foo', array_pop($stack)); $this->assertEquals(0, count($stack)); } } ?>
使用命令去执行:
./phpunit yunke
或者
./phpunit yunke.php
这样就可以了。
当看到这条命令的时候,说明您的第一个程序已经运行成功了。
[root@localhost phpunit]# ./phpunit yunke.php PHPUnit 7.5.1 by Sebastian Bergmann and contributors. . 1 / 1 (100%) Time: 84 ms, Memory: 8.00MB OK (1 test, 5 assertions)
那就是一个测试用例,简单的测试了一个数组操作,进行单元测试一般通过以下四步:
1. 针对生产代码类 Class 的测试写在类 ClassTest中。
2. ClassTest(通常)继承自 PHPUnit\Framework\TestCase。
3. 测试都是命名为 test* 的公用方法。也可以在方法的文档注释块(docblock)中使用 @test 标注将其标记为测试方法。
4. 在测试方法内,类似于 assertEquals()这样的断言方法用来对实际值与预期值的匹配做出断言判断。
其他测试:
PHPUnit从零开始(2):编写 PHPUnit 测试
https://blog.csdn.net/hel12he/article/details/50716771
凡是写在控制器类中 所有的test 方法 都会依次执行
public function testIncrementsDate() { $d = new DateTime('2011-03-15 11:15:00'); $f = new DayOfWeekField(); $f->increment($d); $this->assertEquals('2011-03-16 00:00:00', $d->format('Y-m-d H:i:s')); $d = new DateTime('2011-03-15 11:15:00'); $f->increment($d, true); $this->assertEquals('2011-03-14 23:59:00', $d->format('Y-m-d H:i:s')); } //判断是不是boolean 真 public function testValidatesHashValueNth() { $f = new DayOfWeekField(); $this->assertTrue($f->isSatisfiedBy(new DateTime(), '3#6')); } //判断假 $this->assertFalse($f->validate('-mon')); $this->assertInstanceOf('Symfony\Component\Debug\ErrorHandler', $handler); $this->assertSame($handler, ErrorHandler::register()); $this->assertEquals(__FILE__, $exception->getFile()); $this->assertRegExp('/^Notice: Undefined variable: (foo|bar)/', $exception->getMessage()); $this->assertArrayHasKey('foobar', $exception->getContext());
举例说明
class MinutesFieldTest extends \PHPUnit_Framework_TestCase { /** * @covers Cron\MinutesField::validate */ public function testValdatesField() { $f = new MinutesField(); $this->assertTrue($f->validate('1')); $this->assertTrue($f->validate('*')); $this->assertTrue($f->validate('*/3,1,1-12')); } /** * @covers Cron\MinutesField::increment */ public function testIncrementsDate() { $d = new DateTime('2011-03-15 11:15:00'); $f = new MinutesField(); $f->increment($d); $this->assertEquals('2011-03-15 11:16:00', $d->format('Y-m-d H:i:s')); $f->increment($d, true); $this->assertEquals('2011-03-15 11:15:00', $d->format('Y-m-d H:i:s')); } }
phpunit 执行单元测试文件中的一个方法
./vendor/bin/phpunit tests/Feature/PrescriptionTest.php --filter testCreatePrescriptionNotes
黑色加粗字体是 单元测试文件
蓝色字体 关键字
红色字体 要执行的方法