码迷,mamicode.com
首页 > Web开发 > 详细

PHP测试用例练习

时间:2016-03-14 01:28:39      阅读:668      评论:0      收藏:0      [点我收藏+]

标签:

本测试用例是一个判断三角形类型的练习测试用例,基于Netbeans 8.1IDE环境,和phpunit-5.2.10以及脚手架工具phpunit-skelgen-2.0.1。具体的环境搭建可参照:

http://www.cnblogs.com/x3d/p/phpunit-in-netbeans8.html

源代码类:

 1 <?php
 2 
 3 class Triangle
 4 {
 5 
 6     /**
 7      * 是否三角形 9      * @return boolean
10      */
11     public function is_triangle($a, $b, $c)
12     {
13         if (empty($a) || empty($b) || empty($c) || !is_numeric($a) || !is_numeric($b) || !is_numeric($c) || $a < 0 || $b < 0 || $c < 0) {
14             return FALSE;
15         }
16         if (($a + $b > $c) && ($b + $c > $a) && ($c + $a > $b)) {
17             return true;
18         }
19     }
20 
21     /**
22      * 是否直角三角形
23      * 
24      */
25     public function is_angled_triangle($a, $b, $c)
26     {
27         if ($this->is_triangle($a, $b, $c)) {
28             if (pow($a, 2) + pow($b, 2) == pow($c, 2)) {
29                 return TRUE;
30             }
31             if (pow($a, 2) + pow($c, 2) == pow($b, 2)) {
32                 return TRUE;
33             }
34             if (pow($b, 2) + pow($c, 2) == pow($a, 2)) {
35                 return TRUE;
36             }
37             return FALSE;
38         }
39     }
40 
41     /**
42      * 是否为等腰三角形
43      * 
44      */
45     public function is_isosceles_triangle($a, $b, $c)
46     {
47         if ($this->is_triangle($a, $b, $c) && (($a == $b) || ($b == $c) || ($c == $a))) {
48             return TRUE;
49         }
50     }
51 
52     /**
53      * 是否为等腰直角三角形
54      * @return boolean
55      */
56     public function is_isosceles_angled_triangle($a, $b, $c)
57     {
58         //TODO
59     }
60 
61     /**
62      * 是否为等边三角形
63      * 
64      */
65     public function is_equilateral_triangle($a, $b, $c)
66     {
67         if ($this->is_triangle($a, $b, $c) && $a == $b && $b == $c) {
68             return TRUE;
69         }
70     }
71 
72 }

生成的测试用例:

class TriangleTest extends PHPUnit_Framework_TestCase
{

    /**
     * @var Triangle
     */
    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 Triangle;
    }

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

    /**
     * @covers Triangle::is_triangle
     * @todo   Implement testIs_triangle().
     */
    public function testIs_triangle1()
    {
        $this->assertTrue($this->object->is_triangle(3, 4, 5));
    }

    /**
     * @covers Triangle::is_triangle
     * @todo   Implement testIs_triangle().
     */
    public function testIs_triangle2()
    {
        $this->assertFalse($this->object->is_triangle(0, 1, 2));
    }

    /**
     * @covers Triangle::is_triangle
     * @todo   Implement testIs_triangle().
     */
    public function testIs_triangle3()
    {
        $this->assertFalse($this->object->is_triangle(1, 0, 2));
    }

    /**
     * @covers Triangle::is_triangle
     * @todo   Implement testIs_triangle().
     */
    public function testIs_triangle4()
    {
        $this->assertFalse($this->object->is_triangle(1, 0, 2));
    }

    /**
     * @covers Triangle::is_triangle
     * @todo   Implement testIs_triangle().
     */
    public function testIs_triangle5()
    {
        $this->assertFalse($this->object->is_triangle(1, 2, 3));
    }

    /**
     * @covers Triangle::is_triangle
     * @todo   Implement testIs_triangle().
     */
    public function testIs_triangle6()
    {
        $this->assertFalse($this->object->is_triangle(1, 3, 2));
    }

    /**
     * @covers Triangle::is_triangle
     * @todo   Implement testIs_triangle().
     */
    public function testIs_triangle7()
    {
        $this->assertFalse($this->object->is_triangle(3, 1, 2));
    }

    /**
     * @covers Triangle::is_triangle
     * @todo   Implement testIs_triangle().
     */
    public function testIs_triangle8()
    {
        $this->assertFalse($this->object->is_triangle(‘,‘, ‘,‘, ‘,‘));
    }

    /**
     * @covers Triangle::is_triangle
     * @todo   Implement testIs_triangle().
     */
    public function testIs_triangle9()
    {
        $this->assertFalse($this->object->is_triangle(‘a‘, 3, ‘@‘));
    }

    /**
     * @covers Triangle::is_triangle
     * @todo   Implement testIs_triangle().
     */
    public function testIs_triangle10()
    {
        $this->assertFalse($this->object->is_triangle(-3, 4, 5));
    }

    /**
     * @covers Triangle::is_triangle
     * @todo   Implement testIs_triangle().
     */
    public function testIs_triangle11()
    {
        $this->assertFalse($this->object->is_triangle(3, 4));
    }

    /**
     * @covers Triangle::is_angled_triangle
     * @todo   Implement testIs_angled_triangle().
     */
    public function testIs_angled_triangle()
    {
        $this->assertTrue($this->object->is_angled_triangle(3, 4, 5));
    }

    /**
     * @covers Triangle::is_isosceles_triangle
     * @todo   Implement testIs_isosceles_triangle().
     */
    public function testIs_isosceles_triangle1()
    {
        $this->assertTrue($this->object->is_isosceles_triangle(3, 3, 4));
    }

    /**
     * @covers Triangle::is_isosceles_triangle
     * @todo   Implement testIs_isosceles_triangle().
     */
    public function testIs_isosceles_triangle2()
    {
        $this->assertTrue($this->object->is_isosceles_triangle(3, 4, 4));
    }

    /**
     * @covers Triangle::is_isosceles_triangle
     * @todo   Implement testIs_isosceles_triangle().
     */
    public function testIs_isosceles_triangle3()
    {
        $this->assertTrue($this->object->is_isosceles_triangle(3, 4, 3));
    }

    /**
     * @covers Triangle::is_isosceles_angled_triangle
     * @todo   Implement testIs_isosceles_angled_triangle().
     */
    public function testIs_isosceles_angled_triangle()
    {
        //
    }

    /**
     * @covers Triangle::is_equilateral_triangle
     * @todo   Implement testIs_equilateral_triangle().
     */
    public function testIs_equilateral_triangle()
    {
        $this->assertTrue($this->object->is_equilateral_triangle(3, 3, 3));
    }

}

运行结果如下:

 

"F:\php-7\php.exe" "F:\php-7\phpunit-5.2.10.phar" "--colors" "--log-junit" "C:\Users\legend\AppData\Local\Temp\nb-phpunit-log.xml" "--bootstrap" "C:\Users\legend\Documents\NetBeansProjects\testexm\test\bootstrap.php" "D:\Program Files\NetBeans 8.1\php\phpunit\NetBeansSuite.php" "--" "--run=C:\Users\legend\Documents\NetBeansProjects\testexm\test\triangleTest.php"


....FFF...E......                                                 17 / 17 (100%)

Time: 136 ms, Memory: 8.00Mb

There was 1 error:

1) TriangleTest::testIs_triangle11
Missing argument 3 for Triangle::is_triangle(), called in C:\Users\legend\Documents\NetBeansProjects\testexm\test\triangleTest.php on line 129 and defined

C:\Users\legend\Documents\NetBeansProjects\testexm\test\triangle.php:10
C:\Users\legend\Documents\NetBeansProjects\testexm\test\triangleTest.php:129

--

There were 3 failures:

1) TriangleTest::testIs_triangle5
Failed asserting that null is false.

C:\Users\legend\Documents\NetBeansProjects\testexm\test\triangleTest.php:75

2) TriangleTest::testIs_triangle6
Failed asserting that null is false.

C:\Users\legend\Documents\NetBeansProjects\testexm\test\triangleTest.php:84

3) TriangleTest::testIs_triangle7
Failed asserting that null is false.

C:\Users\legend\Documents\NetBeansProjects\testexm\test\triangleTest.php:93

FAILURES!
Tests: 17, Assertions: 15, Errors: 1, Failures: 3.
PHP Warning:  PHP Startup: mysql: Unable to initialize module
Module compiled with module API=20121212
PHP    compiled with module API=20151012
These options need to match
 in Unknown on line 0
PHP Warning:  PHP Startup: igbinary: Unable to initialize module
Module compiled with module API=20121212
PHP    compiled with module API=20151012
These options need to match
 in Unknown on line 0
PHP Warning:  PHP Startup: redis: Unable to initialize module
Module compiled with module API=20121212
PHP    compiled with module API=20151012
These options need to match
 in Unknown on line 0
完成。

 运行后发现原来测试代码中测试例

testIs_triangle5、testIs_triangle6、testIs_triangle7

 未通过,原因为源代码中未对两边和等于第三边做判断

测试用例应该有更好的实现方式,继续学习中。。

PHP测试用例练习

标签:

原文地址:http://www.cnblogs.com/isleep/p/5273949.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!