Introduction
Previously wanted to use the JS Unit Test framework Test interface, but for half a day is simulated Ajax request method test. (jest frame). So thought of using PHP to achieve.
Business
PHPUnit Address: Https://phpunit.de/manual/current/zh_cn/installation.html#installation.optional-packages
Guzzle Address: Https://github.com/guzzle/guzzle
Problems with installation:
1.phpunit needs php5.6 environment.
2.guzzle decompression requires zlib. Install with Brew.
Test the code when:
a.php
require 'vendor/autoload.php';class LoginTest extends PHPUnit_Framework_TestCase { //只是试试phpunit功能 public function testNormal() { $expected = 1; $actual = 1; $this->assertEquals($expected,$actual); } //测试api public function testSend(){ $client = new GuzzleHttp\Client(); $res = $client->request('GET', 'https://developer.github.com/v3/', [ ]); echo $res->getStatusCode(); // 200 echo $res->getHeaderLine('content-type'); // 'application/json; charset=utf8' echo $res->getBody(); $this->assertEquals(200, $res->getStatusCode()); } } ?>
Then run in the current directory
$ phpunit a.php
说明: guzzle安装必须和测试代码在同目录中
Test results
The above describes the PHPUnit + guzzle real unit test interface, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.