neo's profile十年一场梦 ..BlogListsGuestbookMore Tools Help

Blog


    11/19/2009

    PERL TESTING 3

     
    以虚代实  介绍一些test欺骗技术
     
    1)重建内建函数
    {
     package xxx;
     use subs 'system';
     package main;
     
      *xxx::system=sub{
     ...
     }; 
    }    //俗话叫重载  将system重新定义
     

    2)也可不重载 使用外接文件
    调用虚函数时使用 $^X pass.pl 为参数
    system($^X,pass.pl)    
    vi pass.pl
     exit 0;
    实际执行的是  perl  pass.pl   返回0;
     

    3)冒名模块
    use Test::MockModule;
    my $lwp=Test::MockModule->new('LWP::Simple');
    $lwp->mock(get=>sub($){....});           //重写LWP::Simple模块中的 get函数
     

    4)冒名对象
    use Test::MockObject;
    $INC{'NET/SMTP.pm'}=1;     //阻止加载NET::SMTP
    my $smtp=Test::MockObject->new();
    $smtp->fake_module('Net::SMTP',new=>sub{$smtp});   //生成一个 冒名对象
    $smtp->set_true(qw(xx1 xx2 xx3 xx));     //创建一些新的冒名对象方法   每个方法均返回true
    $smtp->next_call();    //调用xx1方法    
                   next_call();    //调用xx2 只能顺序调用  唯一的缺点...
     

    5)部分冒名对象
    use Test::MockObject::Extends;
    my $lib=Library->new(..);
    my $mock_lib=Test::MockObject::Extends->new($lib);     //对Library中部分函数重写
    $mock_lib->set_true('xx1');      //重写xx1返回真
    $mock_lib->set_away(-xx2 =>x);   
    $mock_lib->set_away(xx3=>{xxx}); 
     
     
    6)重载运行时的代码   --如何在程序运行时临时改变某个全局变量值
    local $Logger::REPROT_TIME=0;
    ...
    $Logger::REPROT_TIME=1;
    ..
     
     
    7)重载perl的全局函数
    BEGIN {*CORE::GLOBAL::exit =sub {..}};

    Comments (1)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    No namewrote:
    絕不要羞於承認自己不知道的事。
    ----
    歡迎來我們的論壇看看哦!
    http://doublexadv.net
    Dec. 9

    Trackbacks

    The trackback URL for this entry is:
    http://sh-neo.spaces.live.com/blog/cns!1E3CA285E5F9E122!767.trak
    Weblogs that reference this entry
    • None