Good Practices On Test Automation

1,051 views

Published on

presentation on 54th SPIN-Campinas meeting.
(http://www.cpqd.com.br/spin-cps/index.php?option=com_content&task=archivecategory&year=2013&month=03&module=1)

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,051
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Good Practices On Test Automation

  1. 1. Good practices on test automation Gustavo Labbate Godoy
  2. 2. Where are test automation on agile environments? Several agile methodologies: Scrum, Extreme Programming (XP), Lean Development, Feature- Driven Development (FDD), Kanban, RUP and OpenUP. Software Development by interactive and incremental way. TDD, Data-Driven Testing, Regression Testing. Automation is one of the three pilars of agile methodology. Delivery on time, with all tested and approved. “Working software is the primary measure of progress.” (http://agilemanifesto.org)
  3. 3. Junit is unit test ?
  4. 4. What is unit test and what is Junit ? Unit test Test the smaller of the components in a isolated way. Test methods must be independent. Functional test Test the behavior of the sistem, from data inputs, processing and data outputs. assertEquals( ExpectedResult, returnProcessedData(entryData) );assertEquals( ExpectedResult, returnProcessedData(entryData) ); returnedObject = insertData(entryData); assertNotNull(returnedObject); //Validating if insert was sucessfull consultedObject = consultData(returnedObject); assertEquals( consultedObject, expectedObject ); //Validating if insert was sucessfull through system query returnedObject = insertData(entryData); assertNotNull(returnedObject); //Validating if insert was sucessfull consultedObject = consultData(returnedObject); assertEquals( consultedObject, expectedObject ); //Validating if insert was sucessfull through system query
  5. 5. Correctly specify your tests
  6. 6. Correctly specify your tests Don't say test, be more specific ... TestCalculate - testCalc1 = shouldSumTwoInt - testCalc2 = shouldSubtractFromSum Test all functionality inside your method (avoid alphabetical ordering). expectedObjects = testParameters; expectedObjects = includeData(entryData); assertNotNull(returnedObject); //Validating if insert was sucessfull consultedObject = consultData(returnedObject); assertEquals( consultedObject, expectedObject ); //Validating if insert was sucessfull through system query expectedObjects = testParameters; expectedObjects = includeData(entryData); assertNotNull(returnedObject); //Validating if insert was sucessfull consultedObject = consultData(returnedObject); assertEquals( consultedObject, expectedObject ); //Validating if insert was sucessfull through system query
  7. 7. Correctly specify your tests But ... @Test public void shouldSucessfulyInclude() { … } @Test public void shouldSucessfullyConsultAfterInclude() { … } @Test public void shouldSucessfulyInclude() { … } @Test public void shouldSucessfullyConsultAfterInclude() { … } @FixMethodOrder(MethodSorters.NAME_ASCENDING) @FixMethodOrder(MethodSorters.NAME_ASCENDING) @Test public void aShouldSucessfulyInclude() { … } @Test public void bShouldSucessfullyConsultAfterInclude() { … } @Test public void aShouldSucessfulyInclude() { … } @Test public void bShouldSucessfullyConsultAfterInclude() { … } Alphabetically ordering methods: Every test method must be independent. Execution order is random.
  8. 8. Parameterize test data Read from a spreadsheet (or another external file … ) @RunWith(Parameterized.class) public Class UserInsert @Parameters public static Collection<Object[]> data() { return Arrays.asList(new Object[][] {{"ParameterA"}, {"ParameterB"}} ); } public UserInsert(String[ ] parameters) { /* Add parameters to variables ... */ } @RunWith(Parameterized.class) public Class UserInsert @Parameters public static Collection<Object[]> data() { return Arrays.asList(new Object[][] {{"ParameterA"}, {"ParameterB"}} ); } public UserInsert(String[ ] parameters) { /* Add parameters to variables ... */ } https://bitbucket.org/wakaleo/jxlunit/src/6eb465c848c8/src/main/java/com/wakaleo/training/jxlunit/SpreadsheetData.java @Parameters public static Collection spreadSheetData() throws IOException { InputStream spreadSheet = new FileInputStream(“mySheet.xls”); return new SpreadSheetData(spreadSheet).getData(); } @Parameters public static Collection spreadSheetData() throws IOException { InputStream spreadSheet = new FileInputStream(“mySheet.xls”); return new SpreadSheetData(spreadSheet).getData(); }
  9. 9. Selenium
  10. 10. Selenium IDE or Core ? IDE • Record & Playback • Export to several languages (C#, Java, Perl, PHP, Python, Ruby). • In browser test execution. • Code refactoring: Re-record your tests... • To avoid: use plugins (do a little programming … ) • Record only on firefox, but run on others (the script, not in IDE … ) • Don't need advanced knowledge on exported code. Core (coding) • API to several languages (C#, Java, Perl, PHP, Python, Ruby). • Build your own test framework, extending selenium. • Test execution on several browser. • Code refactoring: update only affected test code (Page Object Model) • Knowledge of choosen language is essential.
  11. 11. Promote maintainability
  12. 12. Promote maintainability Use fixtures WebDriver driver @BeforeClass public static void runBeforeClass() throws Exception { driver = new FirefoxDriver(); selenium = new WebDriverBackedSelenium(driver, url); capability = DesiredCapabilities.firefox(); capability.setBrowserName("firefox"); capability.setCapability(CapabilityType.TAKES_SCREENSHOT, true); } @After public void runAfterEachTest() { printScreen(); } @AfterClass public static void runAfterClass() { driver.quit(); } WebDriver driver @BeforeClass public static void runBeforeClass() throws Exception { driver = new FirefoxDriver(); selenium = new WebDriverBackedSelenium(driver, url); capability = DesiredCapabilities.firefox(); capability.setBrowserName("firefox"); capability.setCapability(CapabilityType.TAKES_SCREENSHOT, true); } @After public void runAfterEachTest() { printScreen(); } @AfterClass public static void runAfterClass() { driver.quit(); }
  13. 13. Promote maintainability PageObjects Object that represents a web system screen. public Class LoginPO @FindBy(id="user") WebElement user; @FindBy(id="password") WebElement password; @FindBy(id="btnClose") WebElement closeButton; @FindBy(id="btnLogin") WebElement loginButton; String userAcessApp = "automation"; String passwordAcessApp = "selenium"; public void typeAndEnter(String user, String pass) { usuer.sendKeys(user); password.sendKeys(pass); loginButton.click(); } public Class LoginPO @FindBy(id="user") WebElement user; @FindBy(id="password") WebElement password; @FindBy(id="btnClose") WebElement closeButton; @FindBy(id="btnLogin") WebElement loginButton; String userAcessApp = "automation"; String passwordAcessApp = "selenium"; public void typeAndEnter(String user, String pass) { usuer.sendKeys(user); password.sendKeys(pass); loginButton.click(); }
  14. 14. Organize …
  15. 15. Organize ... Using Junit Test Suite @RunWith(Suite.class) @SuiteClasses({ TestClass1.class, TestClass2.class, TestClass3.class }) public class AllMyTests { } @RunWith(Suite.class) @SuiteClasses({ TestClass1.class, TestClass2.class, TestClass3.class }) public class AllMyTests { }
  16. 16. Solid foundations ...
  17. 17. Solid foundations ... Guarantees data integrity Spring's configuration: <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${hibernate.connection.driver_class}" /> <property name="url" value="${hibernate.connection.url}" /> <property name="username" value="${hibernate.connection.username}" /> <property name="password" value="${hibernate.connection.password}" /> </bean> Spring's configuration: <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${hibernate.connection.driver_class}" /> <property name="url" value="${hibernate.connection.url}" /> <property name="username" value="${hibernate.connection.username}" /> <property name="password" value="${hibernate.connection.password}" /> </bean> DBUnit's dataSet to setup and prepare a data base <dataset> <schema.USER LOGIN="User" PASSWORD="781b4ea1d8" NAME="Test User" PASSWORD_EXPIRATION_DATE="2013-03-01 00:00:00.0" LOGIN_COUNT="0" EMAIL="user@test.com" /> </dataset> DBUnit's dataSet to setup and prepare a data base <dataset> <schema.USER LOGIN="User" PASSWORD="781b4ea1d8" NAME="Test User" PASSWORD_EXPIRATION_DATE="2013-03-01 00:00:00.0" LOGIN_COUNT="0" EMAIL="user@test.com" /> </dataset>
  18. 18. Orchestrate ...
  19. 19. Orchestrate ... Manage automated executions
  20. 20. Orchestrate ... Manage test execution results
  21. 21. References Arquillian http://arquillian.org/ Junit http://junit.sourceforge.net/ Selenium http://seleniumhq.org/ DBUnit www.dbunit.org/ Page Objects https://docs.jboss.org/author/display/ARQGRA2/Page+Objects http://code.google.com/p/selenium/wiki/PageObjects Jenkins http://jenkins-ci.org/ TestLink http://www.teamst.org/
  22. 22. Thanks! gustavolabbate@gmail.com linkedin.com/in/gustavolabbate @gustavolabbate

×