PHPUnit + guzzle Real unit test on-line interface

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.