TestNG
Parce que vos tests le valent bien !
             Romain Linsolas
              @romaintaz




                                       1
Abstract


• TestNG, kézako ?
• Parlons fonctionnalités…


                             2
Romain Linsolas

•Développeur Java / Web depuis 2002
•Éleveur d'usines logicielles
•@ Société Générale
  @romaintaz
  http://linsolas.free.fr/wordpress


                                      3
1. Présentation



                  4
Test « New Generation »
   http://testng.org/

    https://github.com/cbeust/testng

• Créé par Cédric Beust (Google) en avril 2004
• Version 6.5.1
        1.0    2.0   4.0                         5.0                 6.0          6.5

 2004         2005   2006   2007   2008   2009         2010   2011         2012



                                                                                        5
Support IDE / builders
Natif              Par plugin




                                 6
2. Fonctionnalités

   Parce qu'il y en a plein dedans !




                                       7
@Test
               (non parce que c’est une librairie de tests quand même)

expectedExceptions                  Méthode ou classe
timeOut
                                    dependsOnMethods /
                                    dependsOnGroup

                                    expectedExceptionsMessageRegExp

                                    groups

                                    dataProvider

                                    invocationCount, singleThreaded
                                    threadPoolSize

                                    priority

                                                                         8
@Annotations

@BeforeMethod / @AfterMethod   @BeforeTest / @AfterTest

@BeforeClass / @AfterClass     @BeforeSuite / @AfterSuite

                               @BeforeGroups / @AfterGroups




                                                              9
Groupes de tests




                   10
@Test(groups = { "non-regression" })
public class MonTest {


   @Test(groups = { "slow",     "integration" })
   public void sloooooowTest() { ... }


   @Test(groups = { "fast" })
   public void fastTest() { ... }




                                                   11
Avec JUnit…
@Category(Integration.class) @Test
public void unTest { ... }


@RunWith(Categories.class)
@IncludeCategory(IntegrationTest.class)
@SuiteClasses({ Test1.class, Test2.class })
public class IntegrationTestSuite { }



                                              12
java org.testng.TestNG -groups non-regression



 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
      <groups>non-regression</groups>
    </configuration>
 </plugin>



                                                     13
Dépendances de tests




                       14
public class MonTest {
   @Test
   public void deployOnTomcat() { ... }


   @Test(dependsOnMethods = {"deployOnTomcat"})
   public void testWebUI() { ... }


   @Test(dependsOnGroups= {"integration.*"})
   public void someDependentTests() { ... }




                                                  15
public class MonTest {


   @Test
   public void firstTest() { ... }


   @Test(dependsOnMethods = {"firstTest"},
           alwaysRun = true)
   public void secondTest() { ... }




                                             16
Les Listeners




                17
@Test   @Listeners(MonListener.class)
public class maClasseDeTests { ... }




public class MonListener implements ITestListener {
  public void onTestFailure(ITestResult res) { ... }
  public void onFinish(ITestContext cxt) { ... }




                                                       18
IAnnotationTransformer
IAnnotationTransformer2
IHookable
IInvokedMethodListener
IMethodInterceptor
IReporter
ISuiteListener
ITestListener



                          19
Tests paramétrés




                   20
@Test   @Parameters({ "db" })
public void monTest(String db) { … }


<suite name="ma-suite">
 <parameter name="db" value="oracle"/>
 <test name="monTest"/>




                                         21
@DataProvider(name = "mon-provider")

public Object[][] getObjects() {

    return new Object[][] {

         { 1, 1 }, { 5, 120 }

    };

}

@Test(dataProvider = "mon-provider")

public void testFactorielle(int valeur, int resultat) {

    assertEquals(resultat, App.factorielle(valeur));

}




                                                          22
Les Factories




                23
public class TestNGFactory {

    private String foo;

    public TestNGFactory(String foo) { this.foo = foo; }

    @Factory public Object[] factory() {

        return new Object[] {

          new TestNGFactory("hello"),

          new TestNGFactory("Devoxx") };

    }

    @Test public void test() {

        System.out.println("==> " + foo);

    }

}

                                                           24
Et plein d'autres choses…

     Logger inclus           Rapports de résultats
                                 (xml, html)

 Support Guice                   Support du YAML

 Rerun failing tests first     Peut lancer du JUnit 3.x


                                                          25
26
Questions…




             27

Devoxx test ng

  • 1.
    TestNG Parce que vostests le valent bien ! Romain Linsolas @romaintaz 1
  • 2.
    Abstract • TestNG, kézako? • Parlons fonctionnalités… 2
  • 3.
    Romain Linsolas •Développeur Java/ Web depuis 2002 •Éleveur d'usines logicielles •@ Société Générale @romaintaz http://linsolas.free.fr/wordpress 3
  • 4.
  • 5.
    Test « New Generation » http://testng.org/ https://github.com/cbeust/testng • Créé par Cédric Beust (Google) en avril 2004 • Version 6.5.1 1.0 2.0 4.0 5.0 6.0 6.5 2004 2005 2006 2007 2008 2009 2010 2011 2012 5
  • 6.
    Support IDE /builders Natif Par plugin 6
  • 7.
    2. Fonctionnalités Parce qu'il y en a plein dedans ! 7
  • 8.
    @Test (non parce que c’est une librairie de tests quand même) expectedExceptions Méthode ou classe timeOut dependsOnMethods / dependsOnGroup expectedExceptionsMessageRegExp groups dataProvider invocationCount, singleThreaded threadPoolSize priority 8
  • 9.
    @Annotations @BeforeMethod / @AfterMethod @BeforeTest / @AfterTest @BeforeClass / @AfterClass @BeforeSuite / @AfterSuite @BeforeGroups / @AfterGroups 9
  • 10.
  • 11.
    @Test(groups = {"non-regression" }) public class MonTest { @Test(groups = { "slow", "integration" }) public void sloooooowTest() { ... } @Test(groups = { "fast" }) public void fastTest() { ... } 11
  • 12.
    Avec JUnit… @Category(Integration.class) @Test publicvoid unTest { ... } @RunWith(Categories.class) @IncludeCategory(IntegrationTest.class) @SuiteClasses({ Test1.class, Test2.class }) public class IntegrationTestSuite { } 12
  • 13.
    java org.testng.TestNG -groupsnon-regression <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <groups>non-regression</groups> </configuration> </plugin> 13
  • 14.
  • 15.
    public class MonTest{ @Test public void deployOnTomcat() { ... } @Test(dependsOnMethods = {"deployOnTomcat"}) public void testWebUI() { ... } @Test(dependsOnGroups= {"integration.*"}) public void someDependentTests() { ... } 15
  • 16.
    public class MonTest{ @Test public void firstTest() { ... } @Test(dependsOnMethods = {"firstTest"}, alwaysRun = true) public void secondTest() { ... } 16
  • 17.
  • 18.
    @Test @Listeners(MonListener.class) public class maClasseDeTests { ... } public class MonListener implements ITestListener { public void onTestFailure(ITestResult res) { ... } public void onFinish(ITestContext cxt) { ... } 18
  • 19.
  • 20.
  • 21.
    @Test @Parameters({ "db" }) public void monTest(String db) { … } <suite name="ma-suite"> <parameter name="db" value="oracle"/> <test name="monTest"/> 21
  • 22.
    @DataProvider(name = "mon-provider") publicObject[][] getObjects() { return new Object[][] { { 1, 1 }, { 5, 120 } }; } @Test(dataProvider = "mon-provider") public void testFactorielle(int valeur, int resultat) { assertEquals(resultat, App.factorielle(valeur)); } 22
  • 23.
  • 24.
    public class TestNGFactory{ private String foo; public TestNGFactory(String foo) { this.foo = foo; } @Factory public Object[] factory() { return new Object[] { new TestNGFactory("hello"), new TestNGFactory("Devoxx") }; } @Test public void test() { System.out.println("==> " + foo); } } 24
  • 25.
    Et plein d'autreschoses… Logger inclus Rapports de résultats (xml, html) Support Guice Support du YAML Rerun failing tests first Peut lancer du JUnit 3.x 25
  • 26.
  • 27.

Editor's Notes

  • #6 JUnit (créé en 1997) 4.0 est sorti en début 2006, introduction de @Test Junit est une déesse égyptienne :o) (Iounyt)
  • #19 JUnit : @Rule TestWatcher (TestWatchman deprecated)