• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

用于测试php函数的代码

发布: 2007-7-01 15:06 | 作者: admin | 来源: | 查看: 18次 | 进入软件测试论坛讨论

领测软件测试网


今天忽然想到的,就写了一段测试php函数的代码。

<?php

/**
 * 参数数组$ParamList说明
 *
 * 数组的第一维索引是需要测试的函数的参数名,第二维的每个元素是该参数需要测试的可能值,元素值可以为数组。
 */

$ParamList = array("Param1" => array(3,4,3,2,1),
                   "Param2" => array(3,2,5),
                   "Param3" => array(0,0.5,1,1.5));

// 测试函数
sysTestFunction("Test", $ParamList);


// 待测试的函数
function Test($Param1, $Param2, $Param3)
{
    return $Param1 . "|" . $Param2 . "|" . $Param3;
}


/**
 * 自动测试
 *
 * @author         李玉鹏<[email protected]>
 * @param  string  $FunctionName  函数名称
 * @param  array   $ParamList     参数列表
 * @return array
 */
function sysTestFunction($FunctionName, $ParamList)
{
    if(empty($FunctionName))
    {
        echo "函数名不能为空";
        return false;
    }

    if(!is_array(current($ParamList)))
    {
        echo "参数不是2维数组";
        return false;
    }

    $TestParamList = sysCombineArray($ParamList);

    echo "开始测试函数" . $FunctionName . "<br />";
    foreach($TestParamList as $Key => $TestParamInfo)
    {
        echo "开始测试第" . $Key . "组参数:<br />";

        foreach($TestParamInfo as $ParamKey => $Param)
        {
            ${"Param" . $ParamKey} = $Param;
            $TempParamList[] = "$Param" . $ParamKey;
            if(is_array($Param))
            {
                echo "参数" . $ParamKey . ",类型为数组:";
                echo "<pre>";
                print_r($Param);
            }
            elseif(is_bool($Param))
            {
                echo "参数" . $ParamKey . ",类型为boll:";
                if($Param)
                {
                    echo "true";
                }
                else
                {
                    echo "false";
                }
            }
            else
            {
                echo "参数" . $ParamKey . ",类型为字符串或数字:";
                echo $Param;
            }
            echo "<br />";
        }

        $Params = join(", ", $TempParamList);
        unset($TempParamList);

        eval("$TestReturnResult = " . $FunctionName . "(" . $Params . ");");
        if(is_array($TestReturnResult))
        {
            echo "函数返回数组:<pre>";
            print_r($TestReturnResult);
        }
        elseif(is_bool($TestReturnResult))
        {
            if($TestReturnResult)
            {
                echo "函数返回true";
            }
            else
            {
                echo "函数返回false";
            }
        }
        else
        {
            echo "函数返回数字或字符串:" . $TestReturnResult;
        }
        echo "<br /><br />";
    }
}

/**
 * 计算组合的函数
 *
 * @author                   李玉鹏<[email protected]>
 * @param  array $CombinList 待排列组合的2维数组
 * @return array             组合后的数组
 */
function sysCombineArray($CombinList)
{
    if(!is_array(current($CombinList)))
    {
        echo "参数不是2维数组";
        return false;
    }

    /* 计算C(a,1) * C(b, 1) * ... * C(n, 1)的值 */
    $CombineCount = 1;
    foreach($CombinList as $Key => $Value)
    {
        $CombineCount *= count($Value);
    }

    $RepeatTime = $CombineCount;
    foreach($CombinList as $ClassNo => $ParamList)
    {
        // $ParamList中的元素在拆分成组合后纵向出现的最大重复次数
        $RepeatTime = $RepeatTime / count($ParamList);

        $StartPosition = 1;

        foreach($ParamList as $Param)
        {
            $TempStartPosition = $StartPosition;

            $SpaceCount = $CombineCount / count($ParamList) / $RepeatTime;

            for($J = 1; $J <= $SpaceCount; $J ++)
            {
                for($I = 0; $I < $RepeatTime; $I ++)
                {
                   $Result[$TempStartPosition + $I][$ClassNo] = $Param;
                }
                $TempStartPosition += $RepeatTime * count($ParamList);
            }
            $StartPosition += $RepeatTime;
        }
    }

    return $Result;
}
?>

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网