转自

后期移至

以下为汪大哥写的

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. mysql 安装简介

    Linux: 安装 [root @ localhost ~]# yum install mysql-server 设定为开机自动启动 [root @ localhost ~]# chkconfig m ...

  2. c++ 拷贝资源方法

    #include "stdio.h" #include "stdlib.h" #include <sys/types.h> #include < ...

  3. numpy的linspace使用详解

    文档地址: https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html Parameters(参数): start ...

  4. Python爬虫系列-Selenium+Chrome/PhantomJS爬取淘宝美食

    1.搜索关键字 利用Selenium驱动浏览器搜索关键字,得到查询后的商品列表 2.分析页码并翻页 得到商品页码数,模拟翻页,得到后续页面的商品列表 3.分析提取商品内容 利用PyQuery分析源码, ...

  5. 怎样处理jmeter中文乱码

    jmeter返回 中文乱码: 1.在jmeter的bin目录下,找到jmeter的配置文件,jmeter.properties,然后把 sampleresult.default.encoding=UT ...

  6. 【android】android对位图文件的支持

    Android 支持以下三种格式的位图文件:.png(首选)..jpg(可接受)..gif(不建议).

  7. 【linux】【安全】服务器安全建议

    引用自 <鸟哥的linux私房菜-服务器篇>  http://cn.linux.vbird.org/linux_server/0210network-secure_1.php 建立完善的登 ...

  8. 【laravel】【转发】laravel 导入导出excel文档

    1.简介 Laravel Excel 在 Laravel 5 中集成 PHPOffice 套件中的 PHPExcel ,从而方便我们以优雅的.富有表现力的代码实现Excel/CSV文件的导入和 导出  ...

  9. python标准输入输出

    input() 读取键盘输入 input() 函数从标准输入读入一行文本,默认的标准输入是键盘. input 可以接收一个Python表达式作为输入,并将运算结果返回. print()和format( ...

  10. makedown语法

    文章转载至:https://blog.csdn.net/u014061630/article/details/81359144#1-%E5%BF%AB%E6%8D%B7%E9%94%AE 前言 写过博 ...