34
1 Presentation Title Test-driven development (TDD) Overview David Wu

1 Presentation Title Test-driven development (TDD) Overview David Wu

Embed Size (px)

Citation preview

Page 1: 1 Presentation Title Test-driven development (TDD) Overview David Wu

1Presentation Title

Test-driven development (TDD)

Overview

David Wu

Page 2: 1 Presentation Title Test-driven development (TDD) Overview David Wu

2Presentation Title

Outline

• What TDD?• Why TDD?• Development Cycle

– Initial test case– ATDD– BDD

• Example for real case• Conclusion• Lesson Learn

Page 3: 1 Presentation Title Test-driven development (TDD) Overview David Wu

3Presentation Title

What TDD?

• Test-driven development (TDD)  – Write test first– Development process– Very short development cycle.– Eliminate duplication

Page 4: 1 Presentation Title Test-driven development (TDD) Overview David Wu

4Presentation Title

What TDD?

• TDD cycle– Writes an automated test case – Produces the minimum code to pass

test– Refactor the code to acceptable

standards.

Page 5: 1 Presentation Title Test-driven development (TDD) Overview David Wu

5Presentation Title

Why TDD?

• Writing test code benefit– Validate the production code – Protect the production code for each

change– Resolve any failure rapidly– Automatic and short development cycle

Page 6: 1 Presentation Title Test-driven development (TDD) Overview David Wu

6Presentation Title

Why TDD?

• Writing the test first– Written for testability.– Every test for feature written.– Consider how to test rather than adding

it later.– Requirement deeper and easily

understanding.

Page 7: 1 Presentation Title Test-driven development (TDD) Overview David Wu

7Presentation Title

Why TDD?

• Keep the unit small– Reduced debugging effort– Self documenting test

• Simple designs• Refactor and then done• Inspires confidence.

Page 8: 1 Presentation Title Test-driven development (TDD) Overview David Wu

8Presentation Title

Why TDD?

• Programmer says:– I have tested all function by myself, why do I

test that again– We do not have the extra time to write test

case.– The testing is not my work, QT should do that.

Page 9: 1 Presentation Title Test-driven development (TDD) Overview David Wu

9Presentation Title

Why TDD?

• The question:– What the special aspect for TDD, is it not only

to write test code first?– Why do we need to do the double effort to

write test code?– Why do we need to validate the result I

expected?– How do we generate the initial test case?

Page 10: 1 Presentation Title Test-driven development (TDD) Overview David Wu

10Presentation Title

Development Cycle

• Development cycle1. Add a test2. Run all tests and see if the new one

fails3. Write some code4. Run tests5. Refactor code6. Repeat

Page 11: 1 Presentation Title Test-driven development (TDD) Overview David Wu

11Presentation Title

Development Cycle

(Re)Write a test

Write a production

code

Refactor code

Check if the test fails

Run all tests

Test fails

Test succeeds

All tests succeed

Repeat

Test(s) fail

Page 12: 1 Presentation Title Test-driven development (TDD) Overview David Wu

12Presentation Title

Development Cycle – ATDD

• But…, How to initial the test case?– Methodology

• ATDD (Acceptance Test Driven Development)

• BDD (Behavior Driven Development).– Breakdown

• Requirement• User story • Test case

Page 13: 1 Presentation Title Test-driven development (TDD) Overview David Wu

13Presentation Title

Development Cycle – ATDD

• Acceptance Testing– Focus

• System behavior• Functionality

– Checking• Up for Requirement• Down for Execution result.

– Difference of Unit test• Allow the object dependency.

Page 14: 1 Presentation Title Test-driven development (TDD) Overview David Wu

14Presentation Title

Test Driven Development Cycle - ATDD

Requirement User storyAcceptance

test caseWorking skeleton

Completed application

Refactor and pass the test

Break down Pass the test Break down

How to generate the user story test case from requirement

list?

Page 15: 1 Presentation Title Test-driven development (TDD) Overview David Wu

15Presentation Title

Development Cycle - ATDD

• User story (User case)– Abstract enough– Simple and readable– Describe who – Describe what the function– A communication basis

Page 16: 1 Presentation Title Test-driven development (TDD) Overview David Wu

16Presentation Title

Development Cycle - ATDD

• User story format reference– As a <role>, I want <goal/desire> so that

<benefit>– As a <role>, I want <goal/desire>– In order to <receive benefit> as a <role>, I

want <goal/desire>

• Example – Requirement: Login system– User story: As a user input the username and

password, I want to login the system.

Page 17: 1 Presentation Title Test-driven development (TDD) Overview David Wu

17Presentation Title

Development Cycle - BDD

Requirement User storyAcceptance

test caseWorking skeleton

Completed application

Refactor and pass the test

Break down Pass the test Break down

How to generate the acceptance test case from user

story?

Page 18: 1 Presentation Title Test-driven development (TDD) Overview David Wu

18Presentation Title

Development Cycle - BDD

• Behavior Driven Development (BDD)– Feature to user scenario– Describe as DSL (domain-specific

language).– Help to define the code template that

follow 3A (Arrange, Act, Assert).– Write the real test case.

Page 19: 1 Presentation Title Test-driven development (TDD) Overview David Wu

19Presentation Title

Development Cycle - BDD

User story

Acceptance test case

Feature

Scenario

ATDD and BDD mapping

Page 20: 1 Presentation Title Test-driven development (TDD) Overview David Wu

20Presentation Title

Development Cycle - BDD

• Feature– In order to: Objective– As a: Who– I want to: Result

• Scenario– Given: Pre-condition (Arrange)– When: Action (Act)– Then: Expectation (Assert)

Page 21: 1 Presentation Title Test-driven development (TDD) Overview David Wu

21Presentation Title

Development Cycle (ATDD+BDD)

Page 22: 1 Presentation Title Test-driven development (TDD) Overview David Wu

22Presentation Title

Development Cycle (ATDD+BDD)

ATDD & BDD

Unit test

Page 23: 1 Presentation Title Test-driven development (TDD) Overview David Wu

23Presentation Title

Development Cycle (ATDD+BDD)

1. Write user story2. Write acceptance test case from the user story3. Write acceptance test program4. Run the acceptance test case and the test fails5. Write integration test case6. Integration test fails7. Write unit test8. Unit test fails9. Write production code10. Pass the unit test11. Refactor the unit test covered production code12. Repeat step7 ~ step11 until the integration test passed13. Repeat step5 ~ step12 until acceptance test passed14. Repeat step3 ~ step13 until the all acceptance test cases for user

story passed 15. The user story is completed and develop the next user story

Page 24: 1 Presentation Title Test-driven development (TDD) Overview David Wu

24Presentation Title

Example for real case

• Login Feature– Login the system

• In order to – Verify the user account to avoid the illegal user to use the system

• As a – online user• I want to – Verify user account.

Page 25: 1 Presentation Title Test-driven development (TDD) Overview David Wu

25Presentation Title

Example for real case

• Login Scenario 1– When user id is “test” and password is

“passwd”, pass the verification and redirect the page to the index page.• Given: Login web page• And: input user as “test”• And: input password as “passwd”• When: click login button• Then: Redirect the web page to the index

page and show login successfully

Page 26: 1 Presentation Title Test-driven development (TDD) Overview David Wu

26Presentation Title

Example for real case

• Login Scenario 2– When user id is “non-exist” and password is

“passwd”, the verification failed and show the user id dose not exist.• Given: Login web page• And: input user as “non-exist”• And: input password as “passwd”• When: click login button• Then: show password is wrong

Page 27: 1 Presentation Title Test-driven development (TDD) Overview David Wu

27Presentation Title

Example for real case

• The acceptance code template from BDD

public void GivenLoginWebPage(){}public void GivenInputUser(string id){}public void GivenInputPassword(string password){}public void WhenClickLoginButton(){}public void ThenIsUrl(string url){}public void ThenIsMsgSHow(string message){}

Page 28: 1 Presentation Title Test-driven development (TDD) Overview David Wu

28Presentation Title

Example for real case

• We use selenium to capture the login scenario

[Test]public void TheLoginSuccessTest(){ driver.Navigate().GoToUrl(baseURL + "/login.php "); driver.FindElement(By.Id("txtCardId")).Clear(); driver.FindElement(By.Id("txtCardId")).SendKeys(“test"); driver.FindElement(By.Id("txtPassword")).Clear(); driver.FindElement(By.Id("txtPassword")).SendKeys(“passwd"); driver.FindElement(By.Id("btnLogin")).Click(); // verify the current page if it is the index.php or not}

Page 29: 1 Presentation Title Test-driven development (TDD) Overview David Wu

29Presentation Title

Example for real case

• We write the action code into the BDD generated code template

public void GivenLoginWebPage(){ driver.Navigate().GoToUrl(baseURL + "/WebBankSite/Login.aspx");}

public void GivenInputUser(string id){ driver.FindElement(By.Id("txtCardId")).Clear(); driver.FindElement(By.Id("txtCardId")).SendKeys(id);}

public void GivenInputPassword(string password){ driver.FindElement(By.Id("txtPassword")).Clear(); driver.FindElement(By.Id("txtPassword")).SendKeys(password);}

Page 30: 1 Presentation Title Test-driven development (TDD) Overview David Wu

30Presentation Title

Example for real case

30

public void WhenClickLoginButton(){ driver.FindElement(By.Id("btnLogin")).Click();}

public void ThenIsUrl(string url){ var expected = string.Format("{0}/{1}", baseURL, url); Assert.AreEqual(expected, driver.Url)}

public void ThenIsMsgSHow(string message){ Assert.AreEqual(message, driver.FindElement(By.Id("Message")).Text);}

Page 31: 1 Presentation Title Test-driven development (TDD) Overview David Wu

31Presentation Title

Conclusion

W-Model

Page 32: 1 Presentation Title Test-driven development (TDD) Overview David Wu

32Presentation Title

Conclusion

• Method – protect production code• Process – communication and change• Development cycle – short cycle for

test and refactor• Objective – satisfy requirement

32

Page 33: 1 Presentation Title Test-driven development (TDD) Overview David Wu

33Presentation Title

Lesson Learn

• Why agile fails– No CI (Continuous integration)

system– No unit test– No experienced scrum master– No cross-function team– No clear information – No definition of done– No code review

Page 34: 1 Presentation Title Test-driven development (TDD) Overview David Wu

34Presentation Title

Lesson Learn

– Big up-front design– Big User Story– High turnover of staff