在昨天我们说到可以通过调用这个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跑一跑, 有一种快感.
未完待续…
英文: How to Unit Test URL Connectivity via PHPUnit?
强烈推荐
- 英国代购-畅购英伦
- TopCashBack 返现 (英国购物必备, 积少成多, 我2年来一共得了3000多英镑)
- Quidco 返现 (也是很不错的英国返现网站, 返现率高)
- 注册就送10美元, 免费使用2个月的 DigitalOcean 云主机(性价比超高, 每月只需5美元)
- 注册就送10美元, 免费使用4个月的 Vultr 云主机(性价比超高, 每月只需2.5美元)
- 注册就送10美元, 免费使用2个月的 阿里 云主机(性价比超高, 每月只需4.5美元)
- 注册就送20美元, 免费使用4个月的 Linode 云主机(性价比超高, 每月只需5美元) (折扣码: PodCastInit2022)
- PlusNet 英国光纤(超快, 超划算! 用户名 doctorlai)
- 刷了美国运通信用卡一年得到的积分 换了 485英镑
- 注册就送50英镑 – 英国最便宜最划算的电气提供商
- 能把比特币莱特币变现的银行卡! 不需要手续费就可以把虚拟货币法币兑换
微信公众号: 小赖子的英国生活和资讯 JustYYUK