php单元测试/涉及代码覆盖率——netbeans工具

1.入门php

https://netbeans.org/kb/trails/php_zh_CN.htmlhtml

NetBeans是开源软件开发集成环境,是一个开放框架,可扩展的开发平台,能够用于Java、C/C++,PHP等语言的开发,自己是一个开发平台,能够经过扩展插件来扩展功能。web

 

2.搭环境apache

软件,插件等参照 文件中的NetBeans.rarwindows

前提:wamp server环境OK浏览器

准备谷歌自由版(netbeans插件ok)框架

a.首先必须先安装jdk-8u40-nb-8_0_2-windows-x64.exe工具

b.安装netbeans-8.0.2-windows.exe测试

c.将如下3个插件放在wamp\bin\php\php5.5.12 目录中ui

更新wamp\bin\php\php5.5.12\zend_ext\php_xdebug的dll文件版本

d.设置电脑环境变量

e.安装pear(规范你的代码,并便于往后快速生成文档)
在管理员权限下运行,运行后,会生成对应文件及文件夹
D:\wamp\bin\php\php5.5.12\php.ini
中自动插入path路径
 
f.追加pear环境变量
 
g.修改2个文件夹中的php.ini文件
1)wamp\bin\php\php5.5.12\php.ini
能够变更下pear文件的存放位置
 
修改php_xdebug文件名字
 
 
添加如下内容
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000
xdebug.auto_trace=1
xdebug.collect_params=1
xdebug.collect_return=1
xdebug.trace_output_dir="d:/wamp/xdebug/trace"
xdebug.profiler_enable=1
xdebug.profiler_output_name = cachegrind.out.%t.%p
xdebug.profiler_output_dir="d:/wamp/xdebug/profiler"
 
2)wamp\bin\apache\apache2.4.9\bin\php.ini
一样
 
3.启动wampserver的服务
打开netbeans软件
文件-新建项目
下一步
下一步
完成
 
工具-选项-常规
web浏览器-编辑
添加新的浏览器,使用文件夹中的谷歌自由版,里面有netbeans相关插件
 
选项-PHP-常规
添加解析器
框架和工具
选择phpunit,添加phpunit脚本和框架生成器脚本
 
4.建立完项目后,修改属性-测试,phpunit打勾
 
5.选中某个源文件中的须要测试的php文件,单击右键“工具”-“建立测试”,输入测试文件存放目录
生成xxxtest.php文件,修改代码,执行测试任务
 
 6.举例
源文件test.php
----------------------------------------
<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Sample
 *
 * @author sicnco
 */
class Sample {
    //put your code here
    public function Get($a)
    {
        if($a==1)
        {
            return 'ok:'.$a;
        }
        else
        {
            return 'ng:'.$a;
        }
    }
}
 --------------------------------------
修改后的测试文件testtest.php
<?php

require_once '../test.php';  <------这一行须要手动添加

/**
 * Generated by PHPUnit_SkeletonGenerator on 2015-03-27 at 03:50:41.
 */
class SampleTest extends PHPUnit_Framework_TestCase {

    /**
     * @var Sample
     */
    protected $object;

    /**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp() {
        $this->object = new Sample;
    }

    /**
     * Tears down the fixture, for example, closes a network connection.
     * This method is called after a test is executed.
     */
    protected function tearDown() {
        
    }

    /**
     * @covers Sample::Get
     * @todo   Implement testGet().
     */
    public function testGet() {
        $rlt = $this->object->Get(1);               <----须要写代码,验证
        $this->assertEquals($rlt, 'ok:1');          <----验证代码运行结果
    }

}
 
 
点击测试文件testtest.php右键,选中“运行”,查看测试结果
相关文章
相关标签/搜索