Symfony2 FrameWork : การสร้าง Unit test และการ Run ไฟล์ แบบง่ายๆ
Posted: 27/11/2014 1:28 pm
ในการ Run Unit test สิ่งเเรกที่จะต้องมี คือ ไฟล์ phpunit.bat
http://symfony.com/doc/current/book/testing.html
วิธีการ Run เราจะ Run ผลการทำงานของ Unit test ผ่านทาง Command
กำหนดค่าที่เราจะทำการ Test
กำหนดค่าผลลัพท์ที่เราต้องการ
คำสั่งที่ใช้ในการ Run
ผลการ Run คำสั่ง Unit test ผ่านไม่มี Erorr
ที่นี่เรามาเปลี่ยนค่า ให้กับการ test
กำหนดค่าที่เราจะทำการ Test
กำหนดค่าผลลัพท์ที่เราต้องการ
ผลการ Run คำสั่ง Unit test ไม่ผ่าน มี Erorr เพราะว่าค่าที่ได้ไม่ต้องกับค่าที่เรากำหนดผลลัพท์ไว้
ในตัวอย่างการสร้างไฟล์เราได้ทำการศึกษาจาก Code: Select all
// src/Acme/DemoBundle/Utility/Calculator.php
namespace Acme\DemoBundle\Utility;
class Calculator
{
public function add($a, $b)
{
return $a + $b;
}
}
Code: Select all
// 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);
}
}
Code: Select all
$result = $calc->add(30, 12);
Code: Select all
$this->assertEquals(42, $result);
Code: Select all
phpunit -c app src/Acme/DemoBundle/Tests/Utility/
Code: Select all
$result = $calc->add(40, 12);
Code: Select all
$this->assertEquals(42, $result);