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

Yesterday, I published a handy online SteemIt tool to check the followers not voting your post:

 SteemIt API Tool - Check If Your Followers Have Voted Your Post 

And I provide a free API to use. To ensure the API are working correctly (4 API servers), I have written the following unit test by PHPUnit. As you can see, the unit tests are very important and luckily, it is not difficult to write with modern programming language and test frameworks. 

Have you used TDD (Test Driven Development) in your daily work? Writing tests first isn't a bad thing and I would recommend this good practice.

昨天我们说到可以通过调用这个API来检查你的哪些Steem粉丝没有点赞你的文章:

撸了一个工具 - 快速检查你的粉丝到底有没有给你点赞!(带 免费API)

 那我们怎么确保这个API的功能是正常能用的呢? 万一服务器挂掉了又或者之后更新代码不小心改错了. 这些都是可以通过单元测试来确保功能可以用的并且以前能用的功能和行为并没有发生改变. 

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

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

$ which phpunit
/usr/local/bin/phpunit

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

<?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 在同文件夹下执行以下命令:  

$ 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表示程序方面的错 而点号则是测试通过).  

$ 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跑一跑, 有一种快感. 

 未完待续… 

Originally Published in Steemit. Thank you for reading my post, feel free to FOLLOW and Upvote @justyy which motivates me to create more quality posts. 

原创首发 SteemIt, 非常感谢阅读, 欢迎FOLLOW和Upvote @justyy 能激励我创作更多更好的内容。  

H2
H3
H4
3 columns
2 columns
1 column
10 Comments