Home Backend Development PHP Tutorial 测试php函数的方法_php实例

测试php函数的方法_php实例

Jun 07, 2016 pm 05:23 PM
php function test

今天忽然想到的,就写了一段测试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;
}

/**
 * 自动测试
 *
 * @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 . "
";
    foreach($TestParamList as $Key => $TestParamInfo)
    {
        echo "开始测试第" . $Key . "组参数:
";
        foreach($TestParamInfo as $ParamKey => $Param)
        {
            ${"Param" . $ParamKey} = $Param;
            $TempParamList[] = "$Param" . $ParamKey;
            if(is_array($Param))
            {
                echo "参数" . $ParamKey . ",类型为数组:";
                echo "

";<br>                print_r($Param);<br>            }<br>            elseif(is_bool($Param))<br>            {<br>                echo "参数" . $ParamKey . ",类型为boll:";<br>                if($Param)<br>                {<br>                    echo "true";<br>                }<br>                else<br>                {<br>                    echo "false";<br>                }<br>            }<br>            else<br>            {<br>                echo "参数" . $ParamKey . ",类型为字符串或数字:";<br>                echo $Param;<br>            }<br>            echo "<br>";<br>        }<br>        $Params = join(", ", $TempParamList);<br>        unset($TempParamList);<br>        eval("$TestReturnResult = " . $FunctionName . "(" . $Params . ");");<br>        if(is_array($TestReturnResult))<br>        {<br>            echo "函数返回数组:<pre class="brush:php;toolbar:false">";<br>            print_r($TestReturnResult);<br>        }<br>        elseif(is_bool($TestReturnResult))<br>        {<br>            if($TestReturnResult)<br>            {<br>                echo "函数返回true";<br>            }<br>            else<br>            {<br>                echo "函数返回false";<br>            }<br>        }<br>        else<br>        {<br>            echo "函数返回数字或字符串:" . $TestReturnResult;<br>        }<br>        echo "<br><br>";<br>    }<br>}<br>/**<br> * 计算组合的函数<br> *<br> * @param  array $CombinList 待排列组合的2维数组<br> * @return array             组合后的数组<br> */<br>function sysCombineArray($CombinList)<br>{<br>    if(!is_array(current($CombinList)))<br>    {<br>        echo "参数不是2维数组";<br>        return false;<br>    }<br>    /* 计算C(a,1) * C(b, 1) * ... * C(n, 1)的值 */<br>    $CombineCount = 1;<br>    foreach($CombinList as $Key => $Value)<br>    {<br>        $CombineCount *= count($Value);<br>    }<br>    $RepeatTime = $CombineCount;<br>    foreach($CombinList as $ClassNo => $ParamList)<br>    {<br>        // $ParamList中的元素在拆分成组合后纵向出现的最大重复次数<br>        $RepeatTime = $RepeatTime / count($ParamList);<br>        $StartPosition = 1;<br>        foreach($ParamList as $Param)<br>        {<br>            $TempStartPosition = $StartPosition;<br>            $SpaceCount = $CombineCount / count($ParamList) / $RepeatTime;<br>            for($J = 1; $J             {<br>                for($I = 0; $I                 {<br>                   $Result[$TempStartPosition + $I][$ClassNo] = $Param;<br>                }<br>                $TempStartPosition += $RepeatTime * count($ParamList);<br>            }<br>            $StartPosition += $RepeatTime;<br>        }<br>    }<br>    return $Result;<br>}<br>?><br>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What do you think of furmark? - How is furmark considered qualified? What do you think of furmark? - How is furmark considered qualified? Mar 19, 2024 am 09:25 AM

What do you think of furmark? 1. Set the &quot;Run Mode&quot; and &quot;Display Mode&quot; in the main interface, and also adjust the &quot;Test Mode&quot; and click the &quot;Start&quot; button. 2. After waiting for a while, you will see the test results, including various parameters of the graphics card. How is furmark qualified? 1. Use a furmark baking machine and check the results for about half an hour. It basically hovers around 85 degrees, with a peak value of 87 degrees and room temperature of 19 degrees. Large chassis, 5 chassis fan ports, two on the front, two on the top, and one on the rear, but only one fan is installed. All accessories are not overclocked. 2. Under normal circumstances, the normal temperature of the graphics card should be between &quot;30-85℃&quot;. 3. Even in summer when the ambient temperature is too high, the normal temperature is &quot;50-85℃

Join a new Xianxia adventure! 'Zhu Xian 2' 'Wuwei Test' pre-download is now available Join a new Xianxia adventure! 'Zhu Xian 2' 'Wuwei Test' pre-download is now available Apr 22, 2024 pm 12:50 PM

The "Inaction Test" of the new fantasy fairy MMORPG "Zhu Xian 2" will be launched on April 23. What kind of new fairy adventure story will happen in Zhu Xian Continent thousands of years after the original work? The Six Realm Immortal World, a full-time immortal academy, a free immortal life, and all kinds of fun in the immortal world are waiting for the immortal friends to explore in person! The "Wuwei Test" pre-download is now open. Fairy friends can go to the official website to download. You cannot log in to the game server before the server is launched. The activation code can be used after the pre-download and installation is completed. "Zhu Xian 2" "Inaction Test" opening hours: April 23 10:00 - May 6 23:59 The new fairy adventure chapter of the orthodox sequel to Zhu Xian "Zhu Xian 2" is based on the "Zhu Xian" novel as a blueprint. Based on the world view of the original work, the game background is set

The new king of domestic FPS! 'Operation Delta' Battlefield Exceeds Expectations The new king of domestic FPS! 'Operation Delta' Battlefield Exceeds Expectations Mar 07, 2024 am 09:37 AM

"Operation Delta" will launch a large-scale PC test called "Codename: ZERO" today (March 7). Last weekend, this game held an offline flash mob experience event in Shanghai, and 17173 was also fortunate to be invited to participate. This test is only more than four months away from the last time, which makes us curious, what new highlights and surprises will "Operation Delta" bring in such a short period of time? More than four months ago, I experienced "Operation Delta" in an offline tasting session and the first beta version. At that time, the game only opened the "Dangerous Action" mode. However, Operation Delta was already impressive for its time. In the context of major manufacturers flocking to the mobile game market, such an FPS that is comparable to international standards

How to disable test cases in Maven? How to disable test cases in Maven? Feb 26, 2024 am 09:57 AM

Maven is an open source project management tool that is commonly used for tasks such as construction, dependency management, and document release of Java projects. When using Maven for project build, sometimes we want to ignore the testing phase when executing commands such as mvnpackage, which will improve the build speed in some cases, especially when a prototype or test environment needs to be built quickly. This article will detail how to ignore the testing phase in Maven, with specific code examples. Why you should ignore testing During project development, it is often

Summary of methods for implementing image editing and processing functions using PHP image processing functions Summary of methods for implementing image editing and processing functions using PHP image processing functions Nov 20, 2023 pm 12:31 PM

PHP image processing functions are a set of functions specifically used to process and edit images. They provide developers with rich image processing functions. Through these functions, developers can implement operations such as cropping, scaling, rotating, and adding watermarks to images to meet different image processing needs. First, I will introduce how to use PHP image processing functions to achieve image cropping function. PHP provides the imagecrop() function, which can be used to crop images. By passing the coordinates and size of the cropping area, we can crop the image

PHP Jenkins 101: The only way to get started with CI/CD PHP Jenkins 101: The only way to get started with CI/CD Mar 09, 2024 am 10:28 AM

Introduction Continuous integration (CI) and continuous deployment (CD) are key practices in modern software development that help teams deliver high-quality software faster and more reliably. Jenkins is a popular open source CI/CD tool that automates the build, test and deployment process. This article explains how to set up a CI/CD pipeline with Jenkins using PHP. Set up Jenkins Install Jenkins: Download and install Jenkins from the official Jenkins website. Create project: Create a new project from the Jenkins dashboard and name it to match your php project. Configure source control: Configure your PHP project's git repository as Jenkin

The role of golang function closure in testing The role of golang function closure in testing Apr 24, 2024 am 08:54 AM

Go language function closures play a vital role in unit testing: Capturing values: Closures can access variables in the outer scope, allowing test parameters to be captured and reused in nested functions. Simplify test code: By capturing values, closures simplify test code by eliminating the need to repeatedly set parameters for each loop. Improve readability: Use closures to organize test logic, making test code clearer and easier to read.

Comparing PHP functions to functions in other languages Comparing PHP functions to functions in other languages Apr 10, 2024 am 10:03 AM

PHP functions have similarities with functions in other languages, but also have some unique features. Syntactically, PHP functions are declared with function, JavaScript is declared with function, and Python is declared with def. In terms of parameters and return values, PHP functions accept parameters and return a value. JavaScript and Python also have similar functions, but the syntax is different. In terms of scope, functions in PHP, JavaScript and Python all have global or local scope. Global functions can be accessed from anywhere, and local functions can only be accessed within their declaration scope.

See all articles