test in action – week 1

28
Test in Action – Week 1 Introduction Hubert Chan

Upload: yi-huan-chan

Post on 10-May-2015

867 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Test in action – week 1

Test in Action – Week 1Introduction

Hubert Chan

Page 2: Test in action – week 1

What is Test?

• Testing– Definition• Verification between actual and expected result

– Example• fibonacci(6) == 8• Fill <script> string in input box, there is no XSS hole

Page 3: Test in action – week 1

Definition of Test – Unit Test

• Unit Test– Write code to test your code– White box testing– Unit Under Test• Class• Function

Page 4: Test in action – week 1

Definition of Test – Unit Test

• Single Failure Point– Limited Dependency– Limited to • Module• Class

Page 5: Test in action – week 1

Unit Testing Example

• Fibonacci Numberfunction fib($n) { if ($n < 3 ) { return 1; } else { return fib($n-1) + fib($n-2); }}

Page 6: Test in action – week 1

Unit Testing Example

• Test Coderequire_once(__DIR__ . '/fib.php');

class FibTest extends PHPUnit_Framework_TestCase { public function testfib() { $res = fib(6); $this->assertEquals(8, $res); }}

Page 7: Test in action – week 1

Unit Testing Example

• Handler Test Codeclass LogHandlerTest extends PHPUnit_Framework_TestCase { public function test_generate_ajax_expect_json() { $handler = new LogHandler(); $res_json = $handler->generate_ajax(); $expect_json = "..."; $this->assertEquals($expect_json, $res_json); }}

Page 8: Test in action – week 1

Definition of Test – Integration Test

• Integration Test– Testing more dependent modules as a group– Black Box Testing– Usage Model Testing• Feature Based Testing• User Targeted Testing

Page 9: Test in action – week 1

Definition of Test – Integration Test

• Multiple Failure Point– Dependency– System Wise

Page 10: Test in action – week 1

Integration Test Example

• Selenium Example<?phprequire_once 'PHPUnit/Extensions/SeleniumTestCase.php'; class WebTest extends PHPUnit_Extensions_SeleniumTestCase { protected function setUp() { $this->setBrowser('*firefox'); $this->setBrowserUrl('http://www.example.com/'); } public function testTitle() { $this->open('http://www.example.com/'); $this->assertTitle('Example WWW Page'); }}

Page 11: Test in action – week 1

DifferenceUnit Testing Integration Test

Speed Fast Relatively Slow

Failure Point Less More

Test Coverage Better Neutral (*)

Failure Path Testing Easier Harder

Page 12: Test in action – week 1

Benefit of Test

• Facilitates Change– Ensure modules still work correctly after change– Foundation of change• Feature change• Refactoring

Page 13: Test in action – week 1

What’s good Unit Test

• Good Unit Test– Automated and repeatable– Reliable and consistent result– Focus on units– Anyone/CI should be able to run it– It should run at the push of a button– It should run quickly

Page 14: Test in action – week 1

Benefit of Unit Test

• Software Quality Enhancement– Verification• Catch bugs in code level• Faster than integration test• Quality Enforcing

– Faster bug identification• Identify the bugs location

Page 15: Test in action – week 1

Benefit of Unit Test

• Documentation– Living documentation of the module– Gain a basic understanding of the unit API

Page 16: Test in action – week 1

Benefit of Unit Test

• Code Quality Enhancement– Foundation of Refactoring– Test drives better OO design• Good OO design is essential for unit test

– Testability• Singleton => Anti-Pattern for testing

Page 17: Test in action – week 1

Hard to do Unit Test?

• Why?– Too complex• Violate Single Responsibility Principle

– Breaking encapsulation– Not program by interface– Messy hierarchy and responsibility

Page 18: Test in action – week 1

What’s TDD?

• Principle– Red– Green– Refactor

Page 19: Test in action – week 1

TDD – Red

• Red– Before actual implementation– Write a test that fails

Page 20: Test in action – week 1

TDD – Red (Example)

• Red– Before writing LogHandler implementation– Write the test code first class LogHandlerTest extends PHPUnit_Framework_TestCase { public function test_generate_ajax_expect_json() { $handler = new LogHandler(); $res_json = $handler->generate_ajax(); $expect_json = "..."; $this->assertEquals($expect_json, $res_json); }}

Page 21: Test in action – week 1

TDD – Green

• Green– Make the code work– Using mock/stub for dependency

Page 22: Test in action – week 1

TDD – Green (Example)

• Green (Example)class LogHander { private dbmodel; public function generate_ajax() { $data = dbmodel->query(); return $this->data_to_json($data); } protected function data_to_json($data) { // processing ... return json_encode($res); }}

Page 23: Test in action – week 1

TDD – Green (Example)

• In Green Example– Faking in TDD• We need a “fake” dbmodel• Can dbmodel->query() return dummy data?

– Think• How do we fake it?• Is it a good responsibility for module?

Page 24: Test in action – week 1

TDD – Refactor

• Refactor– Do Refactoring– Implement Real dbmodel– It breaks the test• Repeat another TDD cycle

Page 25: Test in action – week 1

Benefit of TDD

• Benefit– Make you think about your design• Design by Contract• Modularize/Flexible/Extensible

– Enforce you have tests

Page 26: Test in action – week 1

Continuous Integration

• Continuous Integration– Automation• Build • Testing

– More• Static Code Analysis• Test Coverage• Coding Style Checking

Page 27: Test in action – week 1

Continuous Integration

• Benefit of Continuous Integration– Fully automate– Catch and enforce to fix bugs– Report

Page 28: Test in action – week 1

Q & A

• How do you think about testing?• Think about– TDD / Testing is not elixir– BDD and ATDD– Real OO design– Legacy Code