Symfony2 FrameWork : การสร้าง Unit test และการ Run ไฟล์ แบบง่ายๆ

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: Symfony2 FrameWork : การสร้าง Unit test และการ Run ไฟล์ แบบง่ายๆ

Symfony2 FrameWork : การสร้าง Unit test และการ Run ไฟล์ แบบง่ายๆ

โดย thatsawan » 27/11/2014 1:28 pm

ในการ Run Unit test สิ่งเเรกที่จะต้องมี คือ ไฟล์ phpunit.bat
27-11-2557 12-35-09.jpg
27-11-2557 12-35-09.jpg (103.75 KiB) Viewed 1007 times
ในตัวอย่างการสร้างไฟล์เราได้ทำการศึกษาจาก http://symfony.com/doc/current/book/testing.html
27-11-2557 12-36-06.jpg
27-11-2557 12-36-06.jpg (57.32 KiB) Viewed 1007 times

โค้ด: เลือกทั้งหมด

// src/Acme/DemoBundle/Utility/Calculator.php
namespace Acme\DemoBundle\Utility;

class Calculator
{
    public function add($a, $b)
    {
        return $a + $b;
    }
} 

โค้ด: เลือกทั้งหมด

// src/Acme/DemoBundle/Tests/Utility/CalculatorTest.php
namespace Acme\DemoBundle\Tests\Utility;

use Acme\DemoBundle\Utility\Calculator;

class CalculatorTest extends \PHPUnit_Framework_TestCase
{
    public function testAdd()
    {
        $calc = new Calculator();
        $result = $calc->add(30, 12);

        // assert that your calculator added the numbers correctly!
        $this->assertEquals(42, $result);
    }
} 
วิธีการ Run เราจะ Run ผลการทำงานของ Unit test ผ่านทาง Command
27-11-2557 12-37-26.jpg
27-11-2557 12-37-26.jpg (63.41 KiB) Viewed 1007 times
27-11-2557 12-38-16.jpg
27-11-2557 12-38-16.jpg (33.6 KiB) Viewed 1007 times
27-11-2557 12-44-44.jpg
27-11-2557 12-44-44.jpg (86.39 KiB) Viewed 1007 times

โค้ด: เลือกทั้งหมด

$result = $calc->add(30, 12);
กำหนดค่าที่เราจะทำการ Test

โค้ด: เลือกทั้งหมด

$this->assertEquals(42, $result);
กำหนดค่าผลลัพท์ที่เราต้องการ
27-11-2557 12-38-31.jpg
27-11-2557 12-38-31.jpg (38.61 KiB) Viewed 1007 times
คำสั่งที่ใช้ในการ Run

โค้ด: เลือกทั้งหมด

phpunit -c app src/Acme/DemoBundle/Tests/Utility/
27-11-2557 12-40-28.jpg
27-11-2557 12-40-28.jpg (68.96 KiB) Viewed 1007 times
ผลการ Run คำสั่ง Unit test ผ่านไม่มี Erorr
27-11-2557 12-41-06.jpg
27-11-2557 12-41-06.jpg (88.05 KiB) Viewed 1007 times
ที่นี่เรามาเปลี่ยนค่า ให้กับการ test

โค้ด: เลือกทั้งหมด

$result = $calc->add(40, 12);
กำหนดค่าที่เราจะทำการ Test

โค้ด: เลือกทั้งหมด

$this->assertEquals(42, $result);
กำหนดค่าผลลัพท์ที่เราต้องการ
27-11-2557 12-41-58.jpg
27-11-2557 12-41-58.jpg (98.84 KiB) Viewed 1007 times
ผลการ Run คำสั่ง Unit test ไม่ผ่าน มี Erorr เพราะว่าค่าที่ได้ไม่ต้องกับค่าที่เรากำหนดผลลัพท์ไว้

ข้างบน