Introduction
I wanted to use the js unit testing framework to test the interface before, but after searching for a long time, I could only simulate the ajax request method (jest framework). So I thought of using php to implement it.
The main topic
phpunit address: https: //phpunit.de/manual/current/zh_cn/installation.html#installation.optional-packages
guzzle address: https://github.com/guzzle/guzzle
Installation problems:
1.phpunit requires php5.6 environment is required.
2.guzzle requires zlib when decompressing. Use brew to install.
Test the code as follows:
a.php
<code> 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()); } } ?> </code>
Then run it in the current directory
<code>$ phpunit a.php</code>
Note: guzzle installation must be with The test code is in the same directory
Test results
<code>Time: 7.14 seconds, Memory: 11.75Mb OK (2 tests, 2 assertions) antztekiMacBook-Pro:php_test antz$ </code>
The above introduces the phpunit + guzzle real unit testing online interface, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.