小赖子的英国生活和资讯

通过例子学写 PHP单元测试来确保API功能正常

阅读 桌面完整版

昨天我们说到可以通过调用这个API来检查你的哪些Steem粉丝没有点赞你的文章, 那我们怎么确保这个API的功能是正常能用的呢? 万一服务器挂掉了又或者之后更新代码不小心改错了. 这些都是可以通过单元测试来确保功能可以用的并且以前能用的功能和行为并没有发生改变.

特别是我提供了四台API服务器: 美国东部, 美国西部, 日本东京和英国伦敦, 那我需要每天定时跑些测试来确保API一切正常. 可以通过 Crontab 每天定时跑, 一旦有错误就发邮件提醒或者记录到事件中.

PHP是世界上最好的语言, 通过phpunit 测试API的调用, 首先, 你需要安装 phpunit (官网安装说明), 安装完后可以运行以下命令来确认:

1
2
$ which phpunit
/usr/local/bin/phpunit
$ which phpunit
/usr/local/bin/phpunit

然后我们可以开始写一个简单的 PHP单元测试, 代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
use PHPUnit\Framework\TestCase;
 
class SteemTestsWhoHasNotVoted extends TestCase {
 
  public function dataProvider() {
    // list of API servers    
    $servers = [
        ["happyukgo.com"],
        ["uploadbeta.com"],
        ["helloacm.com"],
        ["steakovercooked.com"]
    ];  
    return $servers;
  }  
   
  /**
   * @dataProvider dataProvider
   */    
  public function simpleTest($domain) {
    $data = file_get_contents("https://$domain/api/steemit/who-has-not-voted/?url=https://steemit.com/steemit/@justyy/steemit-api-tool-check-if-your-followers-have-voted-your-post-api");
    $result = json_decode($data, true);
    $this->assertEquals("justyy", $result['id']);
    $this->assertTrue(is_array($result['who-has-not-voted-yet']));
  }
}
<?php
use PHPUnit\Framework\TestCase;

class SteemTestsWhoHasNotVoted extends TestCase {

  public function dataProvider() {
    // list of API servers    
    $servers = [
        ["happyukgo.com"],
        ["uploadbeta.com"],
        ["helloacm.com"],
        ["steakovercooked.com"]
    ];  
    return $servers;
  }  
   
  /**
   * @dataProvider dataProvider
   */    
  public function simpleTest($domain) {
    $data = file_get_contents("https://$domain/api/steemit/who-has-not-voted/?url=https://steemit.com/steemit/@justyy/steemit-api-tool-check-if-your-followers-have-voted-your-post-api");
    $result = json_decode($data, true);
    $this->assertEquals("justyy", $result['id']);
    $this->assertTrue(is_array($result['who-has-not-voted-yet']));
  }
}

然后我们保存为 steemit-who-has-not-voted-yet-api-test.php 在同文件夹下执行以下命令:

1
2
3
4
5
6
7
8
$ phpunit steemit-who-has-not-voted-yet-api-test.php
PHPUnit 6.0.6 by Sebastian Bergmann and contributors.
 
....                                                                4 / 4 (100%)
 
Time: 13.09 seconds, Memory: 8.00MB
 
OK (4 tests, 8 assertions)
$ phpunit steemit-who-has-not-voted-yet-api-test.php
PHPUnit 6.0.6 by Sebastian Bergmann and contributors.

....                                                                4 / 4 (100%)

Time: 13.09 seconds, Memory: 8.00MB

OK (4 tests, 8 assertions)

PHPUnit 就会加载该PHP文件然后在执行代码中 TestCase的子类(测试类中的公开方法), 其中 dataProvider 定义了API服务器数组这样就不用为每个服务器各写一份代码了.

假设我们把测试方法 simpleTest中的 assertEquals 第一个参数改成 justyy1, 再执行一次, 则会报错 (F 表示 Failure 断言出错了, E表示程序方面的错 而点号则是测试通过).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
$ phpunit steemit-who-has-not-voted-yet-api-test.php
PHPUnit 6.0.6 by Sebastian Bergmann and contributors.
 
FFFF                                                                4 / 4 (100%)
 
Time: 7.83 seconds, Memory: 8.00MB
 
There were 4 failures:
 
1) SteemTestsWhoHasNotVoted::simpleTest with data set #0 ('happyukgo.com')
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'justyy1'
+'justyy'
 
/var/www/phptests/steemit-who-has-not-voted-yet-api-test.php:19
 
2) SteemTestsWhoHasNotVoted::simpleTest with data set #1 ('uploadbeta.com')
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'justyy1'
+'justyy'
 
/var/www/phptests/steemit-who-has-not-voted-yet-api-test.php:19
 
3) SteemTestsWhoHasNotVoted::simpleTest with data set #2 ('helloacm.com')
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'justyy1'
+'justyy'
 
/var/www/phptests/steemit-who-has-not-voted-yet-api-test.php:19
 
4) SteemTestsWhoHasNotVoted::simpleTest with data set #3 ('steakovercooked.com')
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'justyy1'
+'justyy'
 
/var/www/phptests/steemit-who-has-not-voted-yet-api-test.php:19
 
FAILURES!
Tests: 4, Assertions: 4, Failures: 4.
$ phpunit steemit-who-has-not-voted-yet-api-test.php
PHPUnit 6.0.6 by Sebastian Bergmann and contributors.

FFFF                                                                4 / 4 (100%)

Time: 7.83 seconds, Memory: 8.00MB

There were 4 failures:

1) SteemTestsWhoHasNotVoted::simpleTest with data set #0 ('happyukgo.com')
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'justyy1'
+'justyy'

/var/www/phptests/steemit-who-has-not-voted-yet-api-test.php:19

2) SteemTestsWhoHasNotVoted::simpleTest with data set #1 ('uploadbeta.com')
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'justyy1'
+'justyy'

/var/www/phptests/steemit-who-has-not-voted-yet-api-test.php:19

3) SteemTestsWhoHasNotVoted::simpleTest with data set #2 ('helloacm.com')
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'justyy1'
+'justyy'

/var/www/phptests/steemit-who-has-not-voted-yet-api-test.php:19

4) SteemTestsWhoHasNotVoted::simpleTest with data set #3 ('steakovercooked.com')
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'justyy1'
+'justyy'

/var/www/phptests/steemit-who-has-not-voted-yet-api-test.php:19

FAILURES!
Tests: 4, Assertions: 4, Failures: 4.

是不是很简单? 平时写程序都得要有单元测试, 没有单元测试的项目都不算大项目, 甚至你可以先写测试用例, 定义好接口, 然后写完测试再写实现, 这也就是传说中的 TDD (Test Driven Development). 以下是我的一个个人写的项目所写的单元测试, 每天没事打开SSH跑一跑, 有一种快感.

PHPUNIT 单元测试

未完待续…

英文: How to Unit Test URL Connectivity via PHPUnit?

强烈推荐

微信公众号: 小赖子的英国生活和资讯 JustYYUK

阅读 桌面完整版
Exit mobile version