徐文志

PHPUnit is a programmer-oriented testing framework for PHP
就是能够测试PHP代码的框架
QA的测试用例

代码的测试用例

为什么要写测试用例?
- 保证代码的质量,降低风险
- 降低忧虑感
- 快速迭代
- 有益于技术升级、重构
- 提升个人的技术形象
- ...
几种测试类型
- 单元测试
- API测试
- 浏览器测试
- 客户端测试
- ...
几种测试框架
PHPUnit - 单元测试

Selenium - 浏览器测试

Laravel-Dusk - 浏览器测试

PHPUnit使用
composer require phpunit/phpunit
-
phpunit.xml : PHPUnit的配置文件
-
tests : 测试用例目录
1|<?xml version="1.0" encoding="UTF-8"?>
2|<phpunit backupGlobals="false"
3| backupStaticAttributes="false"
4| bootstrap="./config/loader.php"
5| colors="true"
6| convertErrorsToExceptions="true"
7| convertNoticesToExceptions="true"
8| convertWarningsToExceptions="true"
9| processIsolation="false"
10| stopOnFailure="false">
11| <testsuites>
12| <testsuite name="Application Test Suite">
13| <directory suffix="Test.php">./tests</directory>
14| </testsuite>
15| </testsuites>
16| <logging>
17| <log type="coverage-html" target="./phpunit_report" charset="UTF-8"
18| yui="true" highlight="true"
19| lowUpperBound="50" highLowerBound="80" />
20| </logging>
21| <filter>
22| <whitelist processUncoveredFilesFromWhitelist="true">
23| <directory suffix=".php">./app</directory>
24| <exclude>
25|
26| </exclude>
27| </whitelist>
28| </filter>
29| <php>
30| <env name="APP_MODE" value="testing"/>
31| </php>
32|</phpunit><?php
// tests/UtilCase.php
use App\Providers\Util;
class UtilTest extends TestCase
{
public function testCamelToUnderScore()
{
$data = 'abcDeFGhiJk';
$this->assertTrue(Util::camelCaseToUnderScore($data) === 'abc_de_fghi_jk');
}
}
./vender/bin/phpunit./tests/*Test.phppublic function test*()
{
//...test code
}😀
public function test*()
{
//...test code
}public function test*()
{
//...test code
}Load phpunit.xmlDO IT.
如何使用PHPUnit做接口级测试

+

也不一定非得是👆


一些问题
如何维护测试用例?
如何构造测试数据?隔离?
如何对业务代码完全无侵入?
什么时候写测试用例?
Q & A
Thanks!
PHPUnit
By xuwenzhi
PHPUnit
- 1,695