Download presentation
Presentation is loading. Please wait.
Published byGeraldine Cunningham Modified over 9 years ago
1
1 Presentation Title Test-driven development (TDD) Overview David Wu
2
2 Presentation Title Outline What TDD? Why TDD? Development Cycle –Initial test case –ATDD –BDD Example for real case Conclusion Lesson Learn
3
3 Presentation Title What TDD? Test-driven development (TDD) –Write test first –Development process –Very short development cycle. –Eliminate duplication
4
4 Presentation Title What TDD? TDD cycle –Writes an automated test case –Produces the minimum code to pass test –Refactor the code to acceptable standards.
5
5 Presentation 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
6
6 Presentation 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.
7
7 Presentation Title Why TDD? Keep the unit small –Reduced debugging effort –Self documenting test Simple designs Refactor and then done Inspires confidence.
8
8 Presentation 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.
9
9 Presentation 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?
10
10 Presentation Title Development Cycle Development cycle 1.Add a test 2.Run all tests and see if the new one fails 3.Write some code 4.Run tests 5.Refactor code 6.Repeat
11
11 Presentation 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
12
12 Presentation 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
13
13 Presentation 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.
14
14 Presentation Title Test Driven Development Cycle - ATDD RequirementUser story Acceptance test case Working 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?
15
15 Presentation Title Development Cycle - ATDD User story (User case) –Abstract enough –Simple and readable –Describe who –Describe what the function –A communication basis
16
16 Presentation Title Development Cycle - ATDD User story format reference –As a, I want so that –As a, I want –In order to as a, I want Example –Requirement: Login system –User story: As a user input the username and password, I want to login the system.
17
17 Presentation Title Development Cycle - BDD RequirementUser story Acceptance test case Working 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?
18
18 Presentation 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.
19
19 Presentation Title Development Cycle - BDD User story Acceptance test case Feature Scenario ATDD and BDD mapping
20
20 Presentation 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)
21
21 Presentation Title Development Cycle (ATDD+BDD)
22
22 Presentation Title Development Cycle (ATDD+BDD) ATDD & BDD Unit test
23
23 Presentation Title Development Cycle (ATDD+BDD) 1.Write user story 2.Write acceptance test case from the user story 3.Write acceptance test program 4.Run the acceptance test case and the test fails 5.Write integration test case 6.Integration test fails 7.Write unit test 8.Unit test fails 9.Write production code 10.Pass the unit test 11.Refactor the unit test covered production code 12.Repeat step7 ~ step11 until the integration test passed 13.Repeat step5 ~ step12 until acceptance test passed 14.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
24
24 Presentation 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.
25
25 Presentation 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
26
26 Presentation 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
27
27 Presentation 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) { }
28
28 Presentation 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 }
29
29 Presentation 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); }
30
30 Presentation 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); }
31
31 Presentation Title Conclusion W-Model
32
32 Presentation Title Conclusion Method – protect production code Process – communication and change Development cycle – short cycle for test and refactor Objective – satisfy requirement 32
33
33 Presentation 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
34
34 Presentation Title Lesson Learn –Big up-front design –Big User Story –High turnover of staff
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.