转自

后期移至

以下为汪大哥写的

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. 01_5_Struts_ActionMethod_DMI_动态方法调用

    01_5_Struts_ActionMethod_DMI_动态方法调用 1. ActionMethod_DMI_动态方法调用 Action执行的时候并不一定要执行execute()方法 可以在配置文件 ...

  2. UITableView上添加按钮,按钮点击效果延迟的解决办法

    在自定义的TableView的初始化方法做如下操作 - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame: ...

  3. Convert HTML Entities-freecodecamp算法题目

    Convert HTML Entities 1.要求 将字符串中的字符 &.<.>." (双引号), 以及 ' (单引号)转换为它们对应的 HTML 实体. 2.思路 利 ...

  4. python入门:in 的用法(它在不在这个字符串里面)

    #!/usr/bin/env python # -*- coding:utf-8 -*- #in 的用法(它在不在这个字符串里面) #ret(返回,译音:ruai特) #给s赋值为字符串“Alex S ...

  5. linux的一些权限的操作 chmod

    u ---属主 g ---用户组 o ---其他组 + ----赋予权限 - ----取消权限 = ----赋予权限并取消以前的权限 r = 4   //读 w =2   //写 x =1   //执 ...

  6. Linux学习-核心的编译与安装

    编译核心与核心模块 核心与核心模块需要先编译起来,而编译的过程其实非常简单,你可以先使用『 make help 』去查 阅一下所有可用编译参数, 就会知道有底下这些基本功能: [root@study ...

  7. 像玩魔兽一样编程——谈VS2010键盘流

    早年在学校里的时候,经常玩War3,那时候很痴迷,也经常看sky.moon的一些第一视角,有的时候也会模仿模仿...好吧,往事不堪回首,现在工作了,谈一谈.Net程序猿使用VS的键盘流,如果你不知道s ...

  8. alidoing --使用JS实现多语言框架、喜欢的请进、、瓦特平台!

    大家好! 多语言实现的案例:http://alidoing.com/或者http://www.alidoing.com/ 图:切换语言界面 JS代码实现: 1.首先新建一个对象langobj,当然对象 ...

  9. 排序算法总结 c描述

    概述 排序有内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存. 我们这里说说八大排序就是内部排序. 当n较大, ...

  10. equal(),hashcode(),toString()方法的作用

    equal(),hashcode(),toString()方法的作用 这三个方法都是java.lang.Object的方法. equal();判断两对象是否相等hashcode();为对象在容器中添加 ...