转自

后期移至

以下为汪大哥写的

yunlian服务监控

如何写监控代码

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

 class XxxTestCase extends UnitTestCase {
function setUp() {
// some setup work
} function tearDown() {
// some teardown work
} function testFunction1() {
$this->assertTrue(1 + 1 == 2);
} function testFunction2() {
// test the second function
}
}
在index.php文件中加入该文件。
$testcase->addFile("$root/tests/xxx.php");

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

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

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

 assertTrue($x)                  Fail if $x is false
assertFalse($x) Fail if $x is true
assertNull($x) Fail if $x is set
assertNotNull($x) Fail if $x not set
assertIsA($x, $t) Fail if $x is not the class or type $t
assertNotA($x, $t) Fail if $x is of the class or type $t
assertEqual($x, $y) Fail if $x == $y is false
assertNotEqual($x, $y) Fail if $x == $y is true
assertWithinMargin($x, $y, $m) Fail if abs($x - $y) < $m is false
assertOutsideMargin($x, $y, $m) Fail if abs($x - $y) < $m is true
assertIdentical($x, $y) Fail if $x == $y is false or a type mismatch
assertNotIdentical($x, $y) Fail if $x == $y is true and types match
assertReference($x, $y) Fail unless $x and $y are the same variable
assertClone($x, $y) Fail unless $x and $y are identical copies
assertPattern($p, $x) Fail unless the regex $p matches $x
assertNoPattern($p, $x) Fail if the regex $p matches $x
expectError($x) Fail if matching error does not occour
expectException($x) Fail if matching exception is not thrown
ignoreException($x) Swallows any upcoming matching exception
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:

 <?php
class ProxyCase extends UnitTestCase{
private $website_ori;
private $ch_ori;
function setUp() {
$this->website_ori = "http://139.196.141.219/test.php";
$this->ch_ori = curl_init();
$this->assertTrue($this->ch_ori);
curl_setopt($this->ch_ori,CURLOPT_URL,$this->website_ori);
curl_setopt($this->ch_ori,CURLOPT_RETURNTRANSFER,1);
curl_setopt($this->ch_ori,CURLOPT_HEADER,0);
curl_setopt($this->ch_ori,CURLOPT_CONNECTTIMEOUT_MS,3000);
}
function tearDown() {
curl_close($this->ch_ori);
}
function testConnectOriUrlSuccess() {
$this->assertEqual(1 + 1, 2);
$output = curl_exec($this->ch_ori);
$this->assertEqual($output,'test page');
echo $output . '<br/>' ;
$this->assertTrue($this->ch_ori);
$info = curl_getinfo($this->ch_ori);
$this->assertEqual($info['http_code'],200);
echo 'Time: ' . $info['total_time'] . '<br / >';
echo 'Url: ' . $info['url'] . '<br/>';
echo 'Code: ' . $info['http_code'] . '<br/>';
}
function testOneAndOneMakesThree() {
$this->assertEqual(1 + 1, 2);
}
}
?>

转 php simple test的更多相关文章

  1. PHP设计模式(一)简单工厂模式 (Simple Factory For PHP)

    最近天气变化无常,身为程序猿的寡人!~终究难耐天气的挑战,病倒了,果然,程序猿还需多保养自己的身体,有句话这么说:一生只有两件事能报复你:不够努力的辜负和过度消耗身体的后患.话不多说,开始吧. 一.什 ...

  2. Design Patterns Simplified - Part 3 (Simple Factory)【设计模式简述--第三部分(简单工厂)】

    原文链接:http://www.c-sharpcorner.com/UploadFile/19b1bd/design-patterns-simplified-part3-factory/ Design ...

  3. WATERHAMMER: A COMPLEX PHENOMENON WITH A SIMPLE SOLUTION

    开启阅读模式 WATERHAMMER A COMPLEX PHENOMENON WITH A SIMPLE SOLUTION Waterhammer is an impact load that is ...

  4. BZOJ 3489: A simple rmq problem

    3489: A simple rmq problem Time Limit: 40 Sec  Memory Limit: 600 MBSubmit: 1594  Solved: 520[Submit] ...

  5. Le lié à la légèreté semblait être et donc plus simple

    Il est toutefois vraiment à partir www.runmasterfr.com/free-40-flyknit-2015-hommes-c-1_58_59.html de ...

  6. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

  7. 设计模式之简单工厂模式Simple Factory(四创建型)

    工厂模式简介. 工厂模式专门负责将大量有共同接口的类实例化 工厂模式可以动态决定将哪一个类实例化,不必事先知道每次要实例化哪一个类. 工厂模式有三种形态: 1.简单工厂模式Simple Factory ...

  8. HDU 5795 A Simple Nim 打表求SG函数的规律

    A Simple Nim Problem Description   Two players take turns picking candies from n heaps,the player wh ...

  9. 关于The C compiler "arm-none-eabi-gcc" is not able to compile a simple test program. 的错误自省...

    在 GCC ARM Embedded https://launchpad.net/gcc-arm-embedded/ 上面下载了个arm-none-eabi-gcc 用cmake 编译时 #指定C交叉 ...

  10. A Simple OpenGL Shader Example II

    A Simple OpenGL Shader Example II eryar@163.com Abstract. The OpenGL Shading Language syntax comes f ...

随机推荐

  1. iOS 解决ipv6问题

    解决ipv6的方法有很多种,由于现在国内的网络运营商还在使用ipv4的网络环境,所以appstore应用不可能大范围去修改自己的服务器, 而且国内的云服务器几乎没有ipv6地址. 这里附上苹果开发平台 ...

  2. dSYM文件

    来到新公司后,前段时间就一直在忙,前不久 项目 终于成功发布上线了,最近就在给项目做优化,并排除一些线上软件的 bug,因为项目中使用了友盟统计,所以在友盟给出的错误信息统计中能比较方便的找出客户端异 ...

  3. matplotlib subplot 子图

    总括 MATLAB和pyplot有当前的图形(figure)和当前的轴(axes)的概念,所有的作图命令都是对当前的对象作用.可以通过gca()获得当前的axes(轴),通过gcf()获得当前的图形( ...

  4. C++ 学习笔记(五)类的知识小结一(重载,友元函数,静态成员,new)

    ---恢复内容开始--- 学习C++类知识点还是挺多的,每个知识点学习的时候都觉得这个知识点咋那么多东西,其实真学完了再回头看好像也就那么点.这次用程序写一个黑猫揍白猫的故事总结一下这段时间学习的零碎 ...

  5. 【二分 最小割】cf808F. Card Game

    Digital collectible card games have become very popular recently. So Vova decided to try one of thes ...

  6. java web项目(spring项目)中集成webservice ,实现对外开放接口

    什么是WebService?webService小示例 点此了解 下面进入正题: Javaweb项目(spring项目)中集成webservice ,实现对外开放接口步骤: 准备: 采用与spring ...

  7. 安装liteIDE on mac

    download and install: http://sourceforge.net/projects/liteide/files/ 解决不能编译,没有自动完成的问题: http://stacko ...

  8. MongoDB学习-->Spring Data Mongodb-->MongodbTemplate

    配置文件application-dev.yml: server: port: 8888 mongo: host: localhost port: 27017 timeout: 60000 db: ma ...

  9. C# 数组 之间转换

    List<string> strLen = new List<string>();            if (!string.IsNullOrEmpty(group_id) ...

  10. SVM 与 LR的异同

    LR & SVM 的区别 相同点 LR和SVM都是分类算法. 如果不考虑核函数,LR和SVM都是线性分类算法,也就是说他们的分类决策面都是线性的. LR和SVM都是监督学习算法. LR和SVM ...