Behaviour-driven development and acceptance tests on Java web applications

4,103 views

Published on

Internal BDD talk at Giran (http://www.giran.com.br).

Source code at: http://github.com/franciscosouza/java-bdd-talk

Published in: Technology

Behaviour-driven development and acceptance tests on Java web applications

  1. 1. Behaviour-driven development... and acceptance tests on Java web applications Francisco Souza @franciscosouza franciscosouza.net giran.com.br
  2. 2. Tests... Hey, developer!
  3. 3. how did we work?
  4. 4. http://www.flickr.com/photos/seandreilinger/2154168478/ Manual testing
  5. 5. Annoying http://www.flickr.com/photos/pusspaw/745074637/
  6. 6. http://www.flickr.com/photos/40045029@N07/4641262811/ Mecanic
  7. 7. Don't you like it?
  8. 8. Don't you like it? Do it better!
  9. 9. http://www.flickr.com/photos/statusfrustration/143040265/ Selenium
  10. 10. Automatization
  11. 11. Selenium IDE
  12. 12. Selenium RC
  13. 13. TDD
  14. 14. test
  15. 15. test first?
  16. 16. how did we work?
  17. 17. As a user I want to see the pa ge title“Google” on www .google.com.br. Specification
  18. 18. Tests?
  19. 19. a test is a runnable specification
  20. 20. You can leave your post-it and use case specification in a outdated box
  21. 21. You can leave your post-it and use case specification in a outdated box but you have to update your tests!
  22. 22. You can leave your post-it and use case specification in a outdated box Agreed! but you have to update your tests!
  23. 23. show me the code!
  24. 24. show me the code! package net.franciscosouza.selenium; import com.thoughtworks.selenium.*; public class TestGoogleHomePage extends SeleneseTestCase { public void setUp() throws Exception { setUp("http://www.google.com.br/", "*chrome"); } public void testGoogleTitle() throws Exception { selenium.open("http://www.google.com.br"); assertEquals("Google", selenium.getTitle()); } }
  25. 25. Ugly! http://www.flickr.com/photos/zanini/4924308668/
  26. 26. and the customer?
  27. 27. As a user I want to see the pa ge title“Google” on www .google.com.br.
  28. 28. Given
  29. 29. Given that I am on the browser start page
  30. 30. Given that I am on the browser start page When
  31. 31. Given that I am on the browser start page When I go to the website www.google.com.br
  32. 32. Given that I am on the browser start page When I go to the website www.google.com.br Then
  33. 33. Given that I am on the browser start page When I go to the website www.google.com.br Then the title of page should be “Google”
  34. 34. is it possible?
  35. 35. JBehave http://www.flickr.com/photos/nocas/12555343/
  36. 36. 01. Write the story
  37. 37. 01. Write the story Given that I am on the browser start page When I go to the website www.google.com.br Then the title of page should be "Google"
  38. 38. 02. Define the steps
  39. 39. 02. Define the steps public class GoogleSteps extends SeleneseTestCase { @Given("that I am on the browser start page") public void openTheBrowser() throws Exception { this.setUp("http://www.google.com.br", "*chrome"); } @When("I go to the website www.google.com.br") public void goToGoogleHomePage() { this.selenium.open("/"); } @Then("the window title should be Google") public void testTitle() { assertEquals("Google", this.selenium.getTitle()); } }
  40. 40. 03. Step runner
  41. 41. 03. Step runner public class GoogleTitle extends JUnitStory { @Override public Configuration configuration() { return new MostUsefulConfiguration() .useStoryLoader(new LoadFromClasspath(this.getClass().getClassLoader())) .useStoryReporterBuilder(new StoryReporterBuilder().withDefaultFormats().withFormats(Format.CONSOLE, Format.TXT)); } @Override public List<CandidateSteps> candidateSteps() { return new InstanceStepsFactory(this.configuration(), new GoogleSteps()).createCandidateSteps(); } }
  42. 42. 04. Run Forest run!
  43. 43. 04. Run Forest run! Di dn 't =/ o w r k
  44. 44. What is the problem with JBehave?
  45. 45. What is the problem with JBehave? Dependencies
  46. 46. What is the problem with JBehave? Dependencies i18n
  47. 47. What is the problem with JBehave? Dependencies i18n I couldn't get this on my machine =/
  48. 48. http://www.flickr.com/photos/mein_arkengel/3728122696/ Boring
  49. 49. http://www.flickr.com/photos/torimercedes/2837826814/ Have fun?
  50. 50. You can have fun with BDD :)
  51. 51. You can have fun with BDD :) And you should!
  52. 52. Gherkin Syntax
  53. 53. Cucumber
  54. 54. Lettuce
  55. 55. Ruby?
  56. 56. Ruby? Python?
  57. 57. Ruby? Python? I am a Java developer!
  58. 58. JRuby? Python? I am a Java developer!
  59. 59. JRuby? Jython? I am a Java developer!
  60. 60. is it possible?
  61. 61. Cucumber
  62. 62. 01. Install JRuby $ rvm install jruby $ rvm jruby
  63. 63. 02. Install Cucumber and Selenium $ gem install cucumber $ gem install selenium-client
  64. 64. 03. Write a feature Feature: Google homepage testing Scenario: Title of Google homepage Given that I am on the browser start page When I go to the website www.google.com.br Then the title of page should be "Google"
  65. 65. Understand: A feature has many scenarios. A scenario has many steps.
  66. 66. 04. Define the steps using Ruby and Selenium
  67. 67. 05. Run the tests $ cucumber
  68. 68. Couldn't I write step definitions in Java? http://www.flickr.com/photos/smull/356355686/
  69. 69. You can! ;)
  70. 70. Cuke4Duke http://www.flickr.com/photos/sharynmorrow/4860775922/
  71. 71. You can use Maven, Ant or command line
  72. 72. You can use Maven, Ant or command line I like command line :)
  73. 73. 01. Install cuke4duke gem $ gem install cuke4duke
  74. 74. 02. Add the JARs to your project cuke4duke.jar picocontainer.jar http://cukes.info/maven/cuke4duke/cuke4duke/
  75. 75. Advertise: Maven is the best way to add and manage cuke4duke in your project ;) http://cukes.info/maven/
  76. 76. 03. Write copy the feature Feature: Google homepage testing Scenario: Title of Google homepage Given that I am on the browser start page When I go to the website www.google.com.br Then the title of page should be "Google"
  77. 77. 04. Define the steps using Java and Selenium public class GoogleHomePageSteps extends SeleneseTestCase { @Before() public void setUpSelenium() throws Exception { this.setUp("http://www.google.com.br", "*chrome"); } @Given("that I am on the browser start page") public void openBrowser() { this.selenium.open("/"); } @When("I go to the website www.google.com.br") public void goToGoogle() { this.selenium.open("http://www.google.com.br"); } ...
  78. 78. 04. Define the steps using Java and Selenium @Then("the title of page should be "(.*)"") public void checkTitle(String title) { assertEquals(title, this.selenium.getTitle()); } @After public void closeSelenium() { this.selenium.close(); } }
  79. 79. 05. Run Forest run! $ cuke4duke --jars lib --require bin/ features
  80. 80. 05. Run Forest run! $ cuke4duke --jars lib --require bin/ features Folder containing your .class files Folder containing your Folder containing your .jar files .feature files
  81. 81. i18n http://www.flickr.com/photos/78617484@N00/2656184369/
  82. 82. Portuguese ;) # language: pt Funcionalidade: Teste da página inicial do Google Cenário: Título da página inicial do Google Dado que estou na página inicial do navegador Quando eu vou para a o site www.google.com.br Então o título da página deve ser "Google"
  83. 83. Cucumber actually (2010 Sep 11) spoken 45 languages
  84. 84. Stop the manual testing right now!
  85. 85. Stop the manual testing right now! Enjoy Cucumber :)
  86. 86. Francisco Souza @franciscosouza www.franciscosouza.net

×