Upload
geraldine-cunningham
View
220
Download
2
Embed Size (px)
Citation preview
1Presentation 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
3Presentation Title
What TDD?
• Test-driven development (TDD) – Write test first– Development process– Very short development cycle.– Eliminate duplication
4Presentation Title
What TDD?
• TDD cycle– Writes an automated test case – Produces the minimum code to pass
test– Refactor the code to acceptable
standards.
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
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.
7Presentation Title
Why TDD?
• Keep the unit small– Reduced debugging effort– Self documenting test
• Simple designs• Refactor and then done• Inspires confidence.
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.
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?
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
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
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
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.
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?
15Presentation Title
Development Cycle - ATDD
• User story (User case)– Abstract enough– Simple and readable– Describe who – Describe what the function– A communication basis
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.
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?
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.
19Presentation Title
Development Cycle - BDD
User story
Acceptance test case
Feature
Scenario
ATDD and BDD mapping
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)
21Presentation Title
Development Cycle (ATDD+BDD)
22Presentation Title
Development Cycle (ATDD+BDD)
ATDD & BDD
Unit test
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
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.
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
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
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){}
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}
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);}
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);}
31Presentation Title
Conclusion
W-Model
32Presentation Title
Conclusion
• Method – protect production code• Process – communication and change• Development cycle – short cycle for
test and refactor• Objective – satisfy requirement
32
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
34Presentation Title
Lesson Learn
– Big up-front design– Big User Story– High turnover of staff