PHPUnit初试

先测试了一下加减,检查一下环境,又调用函数测试了服务器名。

源代码:

 1 class DemoController extends \Think\Controller
 2 {
 3
 4     /**
 5      * @assert (5, 8) == 13
 6      * @assert (16, 76) == 92
 7      * @assert (6, 16) == 32
 8      * @assert (6, 4) == 0
 9      * @assert (‘abc‘, 1) == 2
10      * @param int $a
11      * @param int $b
12      * @return int
13      */
14     public function plus($a, $b)
15     {
16         return $a + $b;
17     }
18
19      /**
20      * @assert (14, 8) == 6
21      * @assert (16, 6) == 10
22      * @assert (6, 4) == 0
23      * @assert (‘45‘, 1) == 44
24      * @param int $a
25      * @param int $b
26      * @return int
27      */
28     public function subtract($a, $b)
29     {
30         return $a - $b;
31     }
32
33     public function connectToServer($serverName = null)
34     {
35         if ($serverName == null) {
36             throw new Exception("这不是一个服务器名");
37         }
38         $fp = fsockopen($serverName, 8080);
39         return ($fp) ? true : false;
40     }
41
42
43 }

生成测试文件:

  1 class DemoControllerTest extends \PHPUnit_Framework_TestCase
  2 {
  3
  4     /**
  5      * @var DemoController
  6      */
  7     protected $object;
  8
  9     /**
 10      * Sets up the fixture, for example, opens a network connection.
 11      * This method is called before a test is executed.
 12      */
 13     protected function setUp()
 14     {
 15         $this->object = new DemoController;
 16     }
 17
 18     /**
 19      * Tears down the fixture, for example, closes a network connection.
 20      * This method is called after a test is executed.
 21      */
 22     protected function tearDown()
 23     {
 24
 25     }
 26
 27     /**
 28      * Generated from @assert (5, 8) == 13.
 29      *
 30      * @covers Home\Controller\DemoController::plus
 31      */
 32     public function testPlus()
 33     {
 34         $this->assertEquals(
 35                 13, $this->object->plus(5, 8)
 36         );
 37     }
 38
 39     /**
 40      * Generated from @assert (16, 76) == 92.
 41      *
 42      * @covers Home\Controller\DemoController::plus
 43      */
 44     public function testPlus2()
 45     {
 46         $this->assertEquals(
 47                 92, $this->object->plus(16, 76)
 48         );
 49     }
 50
 51     /**
 52      * Generated from @assert (6, 16) == 32.
 53      *
 54      * @covers Home\Controller\DemoController::plus
 55      */
 56     public function testPlus3()
 57     {
 58         $this->assertEquals(
 59                 32, $this->object->plus(6, 16)
 60         );
 61     }
 62
 63     /**
 64      * Generated from @assert (6, 4) == 0.
 65      *
 66      * @covers Home\Controller\DemoController::plus
 67      */
 68     public function testPlus4()
 69     {
 70         $this->assertEquals(
 71                 0, $this->object->plus(6, 4)
 72         );
 73     }
 74
 75     /**
 76      * Generated from @assert (‘abc‘, 1) == 0.
 77      *
 78      * @covers Home\Controller\DemoController::plus
 79      */
 80     public function testPlus5()
 81     {
 82         $this->assertEquals(
 83                 2, $this->object->plus(‘abc‘, 1)
 84         );
 85     }
 86
 87     /**
 88      * Generated from @assert (14, 8) == 6.
 89      *
 90      * @covers Home\Controller\DemoController::subtract
 91      */
 92     public function testSubtract()
 93     {
 94         $this->assertEquals(
 95                 6, $this->object->subtract(14, 8)
 96         );
 97     }
 98
 99     /**
100      * Generated from @assert (16, 6) == 10.
101      *
102      * @covers Home\Controller\DemoController::subtract
103      */
104     public function testSubtract2()
105     {
106         $this->assertEquals(
107                 10, $this->object->subtract(16, 6)
108         );
109     }
110
111     /**
112      * Generated from @assert (6, 4) == 0.
113      *
114      * @covers Home\Controller\DemoController::subtract
115      */
116     public function testSubtract3()
117     {
118         $this->assertEquals(
119                 0, $this->object->subtract(6, 4)
120         );
121     }
122
123     /**
124      * Generated from @assert (‘abc‘, 1) == 0.
125      *
126      * @covers Home\Controller\DemoController::subtract
127      */
128     public function testSubtract4()
129     {
130         $this->assertEquals(
131                 44, $this->object->subtract(‘45‘, 1)
132         );
133     }
134
135     /**
136      * @covers Home\Controller\DemoController::connectToServer
137      * @todo   Implement testConnectToServer().
138      */
139     public function testConnectToServer()
140     {
141 //        // Remove the following lines when you implement this test.
142 //        $this->markTestIncomplete(
143 //                ‘This test has not been implemented yet.‘
144 //        );
145         $serverName = ‘wwwcom‘;
146         $this->assertTrue($this->object->connectToServer($serverName) === false);
147     }
148     public function testConnectToServer2()
149     {
150         $serverName = ‘www.baidu.com‘;
151         $this->assertTrue($this->object->connectToServer($serverName) !== false);
152     }

这里的服务器测试用例是手动加上去的!

执行结果:

 1 ..FFF..F..F                                                       11 / 11 (100%)
 2
 3 Time: 44.42 seconds, Memory: 8.75Mb
 4
 5 There were 5 failures:
 6
 7 1) Home\Controller\DemoControllerTest::testPlus3
 8 Failed asserting that 22 matches expected 32.
 9
10 D:\wamp\www\wxportal\tests\Application\Home\Controller\DemoController.classTest.php:67
11
12 2) Home\Controller\DemoControllerTest::testPlus4
13 Failed asserting that 10 matches expected 0.
14
15 D:\wamp\www\wxportal\tests\Application\Home\Controller\DemoController.classTest.php:79
16
17 3) Home\Controller\DemoControllerTest::testPlus5
18 Failed asserting that 1 matches expected 2.
19
20 D:\wamp\www\wxportal\tests\Application\Home\Controller\DemoController.classTest.php:91
21
22 4) Home\Controller\DemoControllerTest::testSubtract3
23 Failed asserting that 2 matches expected 0.
24
25 D:\wamp\www\wxportal\tests\Application\Home\Controller\DemoController.classTest.php:127
26
27 5) Home\Controller\DemoControllerTest::testConnectToServer2
28 Failed asserting that false is true.
29
30 D:\wamp\www\wxportal\tests\Application\Home\Controller\DemoController.classTest.php:158
31
32 FAILURES!
33 Tests: 11, Assertions: 11, Failures: 5.
34 完成。
时间: 2024-11-03 01:30:32

PHPUnit初试的相关文章

Ubuntu Nginx uwsgi django 初试

/************************************************************************************** * Ubuntu Nginx uwsgi django 初试 * 说明: * 最近打算通过Python搭建一个数据收集的网站,先做一个搭建测试. * * 2016-8-5 深圳 南山平山村 曾剑锋 ***************************************************************

How Use PHPUnit Test in Laravel 5.1

Access all tutorials in sprocket icon. June 12, 2015 from author Bill Keck. Sometimes for beginners, the idea of PHPUnit testing code can be scary. You find yourself having to install the test framework, learn a whole new series of commands and metho

Windows Phone开发(2):竖立自信,初试锋茫

上一篇文章中,我们聊了一些"大炮"话题,从这篇文章开始,我们一起来学习WP开发吧. 一.我们有哪些装备. 安装完VS 学习版 for WP后,也连同SDK一并安装了,不必像安卓那样,安装JDK,下载IDE,还要装SDK和Eclipse插件.WP开发环境是可以一键安装的. 1.模拟器. 这个东东应该说比较重要,如果我们没有现成的WP手机怎么办?模拟器就帮我解决这个问题,我们不需要购买WP手机也可以进行WP应用开发,更何况,我们不可能什么时候都要在真实手机上运行,那调试也不方便. 启动模拟

初识 PHPunit stub 模拟返回数据

这是这段时间以来结合 PHPunit 文档和大牛们的讲解,想记录下自己学习到的知识,未来参考补充,完善学到的东西 我们一般使用单测对公司业务里的代码进行测试,他会帮忙找到你的一个个小小的思考不够全面的地方.(虽然大牛说过开发可以先写单测再来写实现的代码,然而现在的我感觉离那个还是有好多距离来着) stub(桩件): “将对象替换为(可选地)返回配置好的返回值的测试替身的实践方法称为上桩(stubbing)”——这是官方文档给出的上桩解释 现在倒回来看我才理解,怎么说呢?举个梨子. ====前提

uwsgi 配置 初试

/************************************************************************************** * uwsgi 配置 初试 * 说明: * 将uwsgi命令行参数改成配置文件中的参数. * * 2016-8-6 深圳 南山平山村 曾剑锋 *************************************************************************************/ 一.sh

搜索引擎--范例:django之初试牛刀

这学期学了一门课<信息检索>,也就是传说中的搜索引擎 大作业自然而然的让我们自己做一个小型的搜索引擎.于是乎,我们本次的主题就诞生了 我也是边学边用,下面和大家一起分享我在这个过程中学到的东西,说的不对的请大家指正 这是我的成果搜索引擎--范例,大家可以点进去看看,点此下载所有文件盒源代码 按照流程,下面我给大家分享的依次是: 1:SAE创建新应用,SVN管理代码 2:新浪微博API获取最近的微博 3:中文分词算法的实现 4:谈谈django--mysql数据库的一些常用命令 搜索引擎--范例

初试cnblog..

总体 页面灵活度的确不小,还有一些零碎的没有搞,到时候再看看页面总体模板怎么改 = = 感觉NB Not Bad 还有js 原来也要申请 虽然也不是啥坏事儿.. 不支持markdown 直接写有点郁闷 - -,还得单独用个markdown编辑器搭配写,真想来个 import markdown ... print("I'm comming,cnblog!") 也要申请 初试cnblog..,布布扣,bubuko.com

Emgu CV 初试

Emgu CV 是.NET平台下对OpenCV图像处理库的封装,也就是.NET版.可以运行在C#.VB.VC++等. 安装完成后需要设置环境变量,比如我安装在D:\Emgu\emgucv-windows-universal-cuda 2.9.0.1922,然后再系统环境变量添加D:\Emgu\emgucv-windows-universal-cuda 2.9.0.192\bin即可 Emgu CV下载地址 http://sourceforge.net/projects/emgucv/files/

phpunit在Windows下安装

目前在小公司,没有测试,所以就准备学习了phpunit框架,准备花一周空余时间,自己学习摸索,单元测试. 今日学习phpunit在Windows下的安装. 1,在官网http://www.phpunit.cn/下载需要的phpunit版本 2,新建文件夹F:/wamp/www/phpunit,把下载的phpunit6.0.phar放到该目录下,改名为phpunit.phar 3,配置环境变量,和php.exe的环境变量一起配置 4,打开cmd.exe进入该目录,运行:echo @php "%~d