Home > Backend Development > PHP Tutorial > phpunit + guzzle real unit testing online interface

phpunit + guzzle real unit testing online interface

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-29 08:58:18
Original
1410 people have browsed it

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>
Copy after login

Then run it in the current directory

<code>$ phpunit a.php</code>
Copy after login

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>
Copy after login

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.

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template