Click here to load reader

Testing basics for developers

  • View
    192

  • Download
    1

Embed Size (px)

Text of Testing basics for developers

Levi9 Presentation One Comon Goal Template

Testing basics for developersAnton Udovychenko11/2013

AgendaMotivationUnit testingJUnit, Mockito, Hamcrest, JsTestDriverIntegration testingPersistence testing, ArquillianFunctional testingSoapUI, SeleniumQ&A

Why should we care?

Testing process

Functional requirement analysisBug report

Manual TestingWriting automated script

Writing test casesTest resultsRelease

Automated testing

FunctionalIntegrationUnit5% 15%80%

5

Unit testingTest small portions of production codeConfidence to changeQuick FeedbackDocumentation

FunctionalIntegrationUnit

Unit testing

TestNG

JUnit lifecycle @BeforeClass For each @Test Instanciate test class @Before Invoke the test @After @AfterClass

JUnit advanced @Rule and @ClassRule Parametrized Mocks Hamcrest

JUnit @Rulepublic class MyRule implements TestRule { @Overridepublic Statement apply( Statement base, Description description ) {return new MyStatement( base );}}

JUnit @Rulepublic class MyStatement extends Statement { private final Statement base;public MyStatement( Statement base ) {this.base = base;}

@Overridepublic void evaluate() throws Throwable {System.out.println( "before" );try { base.evaluate();} finally { System.out.println( "after" );}}}public class MyRule implements TestRule { @Overridepublic Statement apply( Statement base, Description description ) {return new MyStatement( base );}}

JUnit @Rulepublic class MyTest { @Rule public MyRule myRule = new MyRule(); @Test public void testRun() { System.out.println( "during" ); }}public class MyRule implements TestRule { @Overridepublic Statement apply( Statement base, Description description ) {return new MyStatement( base );}}

public class MyStatement extends Statement { private final Statement base;public MyStatement( Statement base ) {this.base = base;}

@Overridepublic void evaluate() throws Throwable {System.out.println( "before" );try {base.evaluate();} finally {System.out.println( "after" );}}}

JUnit @Rulepublic class MyTest { @Rule public MyRule myRule = new MyRule(); @Test public void testRun() { System.out.println( "during" ); }}public class MyRule implements TestRule { @Overridepublic Statement apply( Statement base, Description description ) {return new MyStatement( base );}}

public class MyStatement extends Statement { private final Statement base;public MyStatement( Statement base ) {this.base = base;}

@Overridepublic void evaluate() throws Throwable {System.out.println( "before" );try { base.evaluate();} finally { System.out.println( "after" );}}}Output:beforeduringafter

JUnit @[email protected](Suite.class)@SuiteClasses({ TestCase1.class, TestCase2.class })public class AllTests {

@ClassRulepublic static Timeout timeout = new Timeout(3000);

}

JUnit [email protected](value = Parameterized.class)public class MyTest { private int number; public MyTest(int number) { this.number = number; } @Parameters public static Collection data() { Object[][] data = new Object[][] { { 1 }, { 2 }, { 3 }, { 4 } }; return Arrays.asList(data); } @Test public void pushTest() { System.out.println("Parameterized Number is : " + number); } }

Mockito

Mockito is a mocking framework for unit tests in Java

Mockitoimport static org.mockito.Mockito.*;import static org.junit.Assert.*;import java.util.Iterator;import org.junit.Test;[email protected] void iteratorWillReturnHelloWorld(){//arrangeIterator i=mock(Iterator.class);when(i.next()).thenReturn("Hello").thenReturn("World");//actString result=i.next()+" "+i.next();//assertassertEquals("Hello World", result);}

HamcrestHamcrest is a matchers framework that assists writing software tests

HamcrestassertTrue(foo.contains("someValue") && foo.contains("anotherValue"));

HamcrestassertTrue(foo.contains("someValue") && foo.contains("anotherValue")); assertThat(foo, hasItems("someValue", "anotherValue"));

vs

HamcrestassertTrue(foo.contains("someValue") && foo.contains("anotherValue")); assertThat(foo, hasItems("someValue", "anotherValue"));

vsassertThat( table, column("Type",contains("A","B","C")).where(cell("Status", is("Ok"))));Another example:

JsTestDriverJsTestDriver is an open source JavaScript unit tests runner

JsTestDriver

Integration testing

Test collaboration between componentsDatabaseIO systemSpecial environment configuration

FunctionalIntegrationUnit

Persistence testingIn memory databases

DBUnit

Arquillian

Arquillian is a platform that simplifies integration testing for Java middleware

Arquillian

Real Tests (no mocks)

Arquillian

Real Tests (no mocks)IDE Friendly

Arquillian

Real Tests (no mocks)IDE FriendlyTest Enrichment

Arquillian

Real Tests (no mocks)IDE FriendlyTest EnrichmentClasspath Control

Arquillian

Real Tests (no mocks)IDE FriendlyTest EnrichmentClasspath ControlDrive the Browser

Arquillian

Real Tests (no mocks)IDE FriendlyTest EnrichmentClasspath ControlDrive the BrowserDebug the Server

Arquillian

Real Tests (no mocks)IDE FriendlyTest EnrichmentClasspath ControlDrive the BrowserDebug the ServerContainer agnostic

Arquillian

Real Tests (no mocks)IDE FriendlyTest EnrichmentClasspath ControlDrive the BrowserDebug the ServerContainer agnosticExtensible platform

Arquillianpublic class Greeter { public void greet(PrintStream to, String name) { to.println(createGreeting(name)); }

public String createGreeting(String name) { return "Hello, " + name + "!"; }}

Arquillianpublic class Greeter {public void greet(PrintStream to, String name) { to.println(createGreeting(name)); }

public String createGreeting(String name) { return "Hello, " + name + "!"; }}

@RunWith(Arquillian.class)public class GreeterTest {

@Deployment public static JavaArchive createDeployment() { return ShrinkWrap.create(JavaArchive.class) .addClass(Greeter.class) .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); }

@Inject Greeter greeter;

@Test public void should_create_greeting() { Assert.assertEquals("Hello, Earthling!", greeter.createGreeting("Earthling")); }}

Functional testing

Test customer requirements

FunctionalIntegrationUnit

SoapUI

SoapUI is a web service testing application

SoapUI

Main technologies: SOAP, REST, XML-RPC, HTTP(S), AMF, JDBC, JMSSoapUI is a web service testing application

SoapUI

Main technologies: SOAP, REST, XML-RPC, HTTP(S), AMF, JDBC, JMSAllows security and load testingSoapUI is a web service testing application

SoapUI

Main technologies: SOAP, REST, XML-RPC, HTTP(S), AMF, JDBC, JMSAllows security and load testingService mockingSoapUI is a web service testing application

SoapUI

Main technologies: SOAP, REST, XML-RPC, HTTP(S), AMF, JDBC, JMSAllows security and load testingService mockingLogging of the test resultsSoapUI is a web service testing application

SoapUI

Main technologies: SOAP, REST, XML-RPC, HTTP(S), AMF, JDBC, JMSAllows security and load testingService mockingLogging of the test resultsGroovy APISoapUI is a web service testing application

SoapUIpublic void testTestCaseRunner() throws Exception { WsdlProject project = new WsdlProject( "src/dist/sample-soapui-project.xml" ); TestSuite testSuite = project.getTestSuiteByName( "Test Suite" ); TestCase testCase = testSuite.getTestCaseByName( "Test Conversions" ); // create empty properties and run synchronously TestRunner runner = testCase.run( new PropertiesMap(), false ); assertEquals( Status.FINISHED, runner.getStatus() ); }

Selenium

Selenium is a portable software GUI testing framework for web applications

Selenium

Domain specific language (DSL): Java, C#, Groovy, Perl, PHP, Python and Ruby.

Selenium is a portable software GUI testing framework for web applications

Selenium

Domain specific language (DSL): Java, C#, Groovy, Perl, PHP, Python and Ruby. The tests can then be run against most modern web browsers

Selenium is a portable software GUI testing framework for web applications

Selenium

Domain specific language (DSL): Java, C#, Groovy, Perl, PHP, Python and Ruby. The tests can then be run against most modern web browsersSelenium deploys on Windows, Linux, and Macintosh platforms

Selenium is a portable software GUI testing framework for web applications

Selenium

Domain specific language (DSL): Java, C#, Groovy, Perl, PHP, Python and Ruby. The tests can then be run against most modern web browsersSelenium deploys on Windows, Linux, and Macintosh platformsSelenium provides a record/playback tool for authoring tests without learning a test scripting language (Selenium IDE)

Selenium is a portable software GUI testing framework for web applications

Seleniumpublic class Selenium2Example { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("http://www.google.com"); WebElement element = driver.findElement(By.name("q")); element.sendKeys("Cheese!"); element.submit(); System.out.println("Page title is: " + driver.getTitle()); new WebDriverWait(driver, 10)).until(new ExpectedCondition() { public Boolean apply(WebDriver d) { return d.getTitle().toLowerCase().startsWith("cheese!"); }