51
Testing basics for developers Anton Udovychenko 11/2013

Testing basics for developers

Embed Size (px)

Citation preview

Page 1: Testing basics for developers

Testing basics for developers

Anton Udovychenko11 / 2013

Page 2: Testing basics for developers

Agenda• Motivation

• Unit testing• JUnit, Mockito, Hamcrest, JsTestDriver

• Integration testing• Persistence testing, Arquillian

• Functional testing• SoapUI, Selenium

• Q&A

Page 3: Testing basics for developers

Why should we care?

Page 4: Testing basics for developers

Automated testing

Functional

Integration

Unit

5%

15%

80%

Page 5: Testing basics for developers

Unit testing

Test small portions of production codeConfidence to change

Quick FeedbackDocumentation

Functional

Integration

Unit

Page 6: Testing basics for developers

Unit testing

TestNG

Page 7: Testing basics for developers

JUnit lifecycle

1. @BeforeClass2. For each @Test

a) Instanciate test classb) @Beforec) Invoke the testd) @After

3. @AfterClass

Page 8: Testing basics for developers

JUnit advanced

1. @Rule and @ClassRule2. Parametrized 3. Mocks4. Hamcrest

Page 9: Testing basics for developers

JUnit @Rule

public class MyRule implements TestRule { @Overridepublic Statement apply( Statement base, Description description ) {

return new MyStatement( base );}

}

Page 10: Testing basics for developers

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 );}

}

Page 11: Testing basics for developers

JUnit @Rule

public 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" );}

}}

Page 12: Testing basics for developers

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

Page 13: Testing basics for developers

JUnit @ClassRule

@RunWith(Suite.class)@SuiteClasses({ TestCase1.class, TestCase2.class })public class AllTests {

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

}

Page 14: Testing basics for developers

JUnit Parametrized@RunWith(value = Parameterized.class)public class MyTest {

private int number;

public MyTest(int number) { this.number = number; }

@Parameters public static Collection<Object[]> data() { Object[][] data = new Object[][] { { 1 }, { 2 }, { 3 }, { 4 } }; return Arrays.asList(data); }

@Test public void pushTest() { System.out.println("Parameterized Number is : " + number); }

}

Page 15: Testing basics for developers

Mockito

Mockito is a mocking framework for unit tests in Java

Page 16: Testing basics for developers

Mockitoimport static org.mockito.Mockito.*;import static org.junit.Assert.*;import java.util.Iterator;import org.junit.Test;....

@Testpublic void iteratorWillReturnHelloWorld(){

//arrangeIterator i=mock(Iterator.class);

when(i.next()).thenReturn("Hello").thenReturn("World");//actString result=i.next()+" "+i.next();//assertassertEquals("Hello World", result);

}

Page 17: Testing basics for developers

Hamcrest

Hamcrest is a matchers framework that assists writing software tests

Page 18: Testing basics for developers

Hamcrest

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

Page 19: Testing basics for developers

Hamcrest

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

assertThat(foo, hasItems("someValue", "anotherValue"));

vs

Page 20: Testing basics for developers

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

assertThat(foo, hasItems("someValue", "anotherValue"));

vs

assertThat( table, column("Type",contains("A","B","C")).where(cell("Status", is("Ok")))

);

Another example:

Page 21: Testing basics for developers

JsTestDriver

JsTestDriver is an open source JavaScript unit tests runner

Page 22: Testing basics for developers

JsTestDriver

Page 23: Testing basics for developers

Integration testing

Test collaboration between componentsDatabaseIO system

Special environment configuration

Functional

Integration

Unit

Page 24: Testing basics for developers

Persistence testing

In memory databases

DBUnit

Page 25: Testing basics for developers

Arquillian

Arquillian is a platform that simplifies integration testing for Java middleware

Page 26: Testing basics for developers

Arquillian

• Real Tests (no mocks)

Page 27: Testing basics for developers

Arquillian

• Real Tests (no mocks)• IDE Friendly

Page 28: Testing basics for developers

Arquillian

• Real Tests (no mocks)• IDE Friendly• Test Enrichment

Page 29: Testing basics for developers

Arquillian

• Real Tests (no mocks)• IDE Friendly• Test Enrichment• Classpath Control

Page 30: Testing basics for developers

Arquillian

• Real Tests (no mocks)• IDE Friendly• Test Enrichment• Classpath Control• Drive the Browser

Page 31: Testing basics for developers

Arquillian

• Real Tests (no mocks)• IDE Friendly• Test Enrichment• Classpath Control• Drive the Browser• Debug the Server

Page 32: Testing basics for developers

Arquillian

• Real Tests (no mocks)• IDE Friendly• Test Enrichment• Classpath Control• Drive the Browser• Debug the Server• Container agnostic

Page 33: Testing basics for developers

Arquillian

• Real Tests (no mocks)• IDE Friendly• Test Enrichment• Classpath Control• Drive the Browser• Debug the Server• Container agnostic• Extensible platform

Page 34: Testing basics for developers

Arquillian

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

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

Page 35: Testing basics for developers

Arquillian

public 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")); }}

Page 36: Testing basics for developers

Functional testing

Test customer requirements

Functional

Integration

Unit

Page 37: Testing basics for developers

SoapUI

SoapUI is a web service testing application

Page 38: Testing basics for developers

SoapUI

• Main technologies: SOAP, REST, XML-RPC, HTTP(S), AMF, JDBC, JMS

SoapUI is a web service testing application

Page 39: Testing basics for developers

SoapUI

• Main technologies: SOAP, REST, XML-RPC, HTTP(S), AMF, JDBC, JMS• Allows security and load testing

SoapUI is a web service testing application

Page 40: Testing basics for developers

SoapUI

• Main technologies: SOAP, REST, XML-RPC, HTTP(S), AMF, JDBC, JMS• Allows security and load testing• Service mocking

SoapUI is a web service testing application

Page 41: Testing basics for developers

SoapUI

• Main technologies: SOAP, REST, XML-RPC, HTTP(S), AMF, JDBC, JMS• Allows security and load testing• Service mocking• Logging of the test results

SoapUI is a web service testing application

Page 42: Testing basics for developers

SoapUI

• Main technologies: SOAP, REST, XML-RPC, HTTP(S), AMF, JDBC, JMS• Allows security and load testing• Service mocking• Logging of the test results• Groovy API

SoapUI is a web service testing application

Page 43: Testing basics for developers

SoapUI

public 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() );

}

Page 44: Testing basics for developers

Selenium

Selenium is a portable software GUI testing framework for web applications

Page 45: Testing basics for developers

Selenium

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

Selenium is a portable software GUI testing framework for web applications

Page 46: Testing basics for developers

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

Page 47: Testing basics for developers

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 deploys on Windows, Linux, and Macintosh platforms

Selenium is a portable software GUI testing framework for web applications

Page 48: Testing basics for developers

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 deploys on Windows, Linux, and Macintosh platforms

• Selenium 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

Page 49: Testing basics for developers

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<Boolean>() { public Boolean apply(WebDriver d) { return d.getTitle().toLowerCase().startsWith("cheese!"); } }; System.out.println("Page title is: " + driver.getTitle()); driver.quit(); }}

Page 50: Testing basics for developers

Summary

Functional

Integration

Unit

5%

15%

80%

TestNG

Hamcrest

Page 51: Testing basics for developers

Questions?