转自

后期移至

以下为汪大哥写的

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. 【转】MFC消息映射详解(整理转载)

    消息:主要指由用户操作而向应用程序发出的信息,也包括操作系统内部产生的消息.例如,单击鼠标左按钮,windows将产WM_LBUTTONDOWN消息,而释放鼠标左按钮将产生WM_LBUTTONUP消息 ...

  2. NOIP2013 表达式求值

    题目描述 Description 给定一个只包含加法和乘法的算术表达式,请你编程计算表达式的值. 输入描述 Input Description 输入仅有一行,为需要你计算的表达式,表达式中只包含数字. ...

  3. VNC远程登录端使用经验之一

    1.vnc/xmanager都是经常用的远程登录软件.vnc有个缺点就是他的进程不会自动退出比如如果开了PID1再去开PID2...PIDn.那么前面的PIDn-1个进程就会一直运行如果不手动kill ...

  4. 【php】【特殊案例】数组调用方法

    As of PHP 5.4.0, you can call any callable stored in a variable. <?php class Foo { static functio ...

  5. 图解Disruptor框架(一):初识Ringbuffer

    图解Disruptor框架(一):初识Ringbuffer 概述 1. 什么是Disruptor?为什么是Disruptor? Disruptor是一个性能十分强悍的无锁高并发框架.在JUC并发包中, ...

  6. GoF23种设计模式之行为型模式之访问者模式

    概述 表示一个作用于某对象结构中的各元素的操作. 它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作. 适用性 1.一个对象结构包含很多类对象,它们有不同的接口,而你想对这些对象实施一些依 ...

  7. python寻找模块的路径顺序

    >>> import sys >>> sys.path ['', '/Library/Frameworks/Python.framework/Versions/3. ...

  8. 纯虚函数(pure virtual function )和抽象类(abstract base class)

    函数体=0的虚函数称为“纯虚函数”.包含纯虚函数的类称为“抽象类” #include <string> class Animal // This Animal is an abstract ...

  9. Hard problem CodeForces - 706C

    Time limit1000 ms Memory limit262144 kB 题目: Vasiliy is fond of solving different tasks. Today he fou ...

  10. April Fools Contest 2018

    这个比赛不正经,但是我可以一本正经的写代码啊 A. Quirky Quantifiers time limit per test 2 seconds memory limit per test 64 ...