转 php simple test

转自

后期移至

以下为汪大哥写的

yunlian服务监控

如何写监控代码

首先在tests目录下新建一个文件xxx.php。其中xxx为你的服务名。

 1 class XxxTestCase extends UnitTestCase {
 2     function setUp() {
 3         // some setup work
 4     }
 5 
 6     function tearDown() {
 7         // some teardown work
 8     }
 9 
10     function testFunction1() {
11         $this->assertTrue(1 + 1 == 2);
12     }
13 
14     function testFunction2() {
15         // test the second function
16     }
17 }
在index.php文件中加入该文件。
$testcase->addFile("$root/tests/xxx.php");

setUp 和 tearDown 分别会在本测试case所有的test method运行之前和之后运行,你可以将一些setup和cleanup的工作放在这两个方法里。

XxxTestCase中所有以 test 开头的方法都会成为测试用例。建议每个方法里中只对服务的某一个功能进行测试,而不是把所有的测试用例全写在一个方法里。

使用 assert...() 方法来添加测试。一些常用的assert方法如下表:

 1 assertTrue($x)                  Fail if $x is false 
 2 assertFalse($x)                 Fail if $x is true 
 3 assertNull($x)                  Fail if $x is set 
 4 assertNotNull($x)               Fail if $x not set 
 5 assertIsA($x, $t)               Fail if $x is not the class or type $t 
 6 assertNotA($x, $t)              Fail if $x is of the class or type $t 
 7 assertEqual($x, $y)             Fail if $x == $y is false 
 8 assertNotEqual($x, $y)          Fail if $x == $y is true 
 9 assertWithinMargin($x, $y, $m)  Fail if abs($x - $y) < $m is false 
10 assertOutsideMargin($x, $y, $m) Fail if abs($x - $y) < $m is true 
11 assertIdentical($x, $y)         Fail if $x == $y is false or a type mismatch 
12 assertNotIdentical($x, $y)      Fail if $x == $y is true and types match 
13 assertReference($x, $y)         Fail unless $x and $y are the same variable 
14 assertClone($x, $y)             Fail unless $x and $y are identical copies 
15 assertPattern($p, $x)           Fail unless the regex $p matches $x 
16 assertNoPattern($p, $x)         Fail if the regex $p matches $x 
17 expectError($x)                 Fail if matching error does not occour 
18 expectException($x)             Fail if matching exception is not thrown 
19 ignoreException($x)             Swallows any upcoming matching exception 
20 assert($e)                      Fail on failed expectation object $e 

 

所有的 assert...() 方法都可以传一个可选的 description 作为最后一个参数,如果不传这个参数,只有默认的信息会被显示(一般足够了),如果你想添加一些额外的信息,传这个参数给 assert...() 方法就行了。

更多使用方法请参见 simpletest/docs/en/unit_test_documentation.html

如何使用

http://monitor.yunlian.io/[?format={html|txt|xml}][&service=xxx]
实际是:
http://139.196.141.219/monitor/index.php

直接打开时显示格式为html,此时只显示fail的信息,必须出现 green bar 才表示所有的测试通过,如果出现 red bar 或者什么bar也没有出现说明有测试失败或者出现了fatal error。

 

参考:

http://php.net/curl

 

第一个case:

 1 <?php
 2 class ProxyCase extends UnitTestCase{
 3     private $website_ori;
 4     private $ch_ori;
 5     function setUp() {
 6         $this->website_ori = "http://139.196.141.219/test.php";
 7         $this->ch_ori = curl_init();
 8         $this->assertTrue($this->ch_ori);
 9         curl_setopt($this->ch_ori,CURLOPT_URL,$this->website_ori);
10         curl_setopt($this->ch_ori,CURLOPT_RETURNTRANSFER,1);
11         curl_setopt($this->ch_ori,CURLOPT_HEADER,0);
12         curl_setopt($this->ch_ori,CURLOPT_CONNECTTIMEOUT_MS,3000);
13     }
14     function tearDown() {
15         curl_close($this->ch_ori);
16     }
17     function testConnectOriUrlSuccess() {
18         $this->assertEqual(1 + 1, 2);
19         $output = curl_exec($this->ch_ori);
20         $this->assertEqual($output,'test page');
21         echo $output . '<br/>' ;
22         $this->assertTrue($this->ch_ori);
23         $info = curl_getinfo($this->ch_ori);
24         $this->assertEqual($info['http_code'],200);
25         echo 'Time: ' . $info['total_time'] . '<br / >';
26         echo 'Url: ' . $info['url'] . '<br/>';
27         echo 'Code: ' . $info['http_code'] . '<br/>';
28     }
29     function testOneAndOneMakesThree() {
30         $this->assertEqual(1 + 1, 2);
31     }
32 }
33 ?>

 

posted on 2016-07-07 19:37  njczy2010  阅读(218)  评论(0编辑  收藏  举报