TestNG vs. JUnit

Preview:

Citation preview

TestNg vs. JUnit

Speaker: Andrey OleynikDate: 26/12/2016

About me

Andrey Oleynik

• About 9 years of work in Quality Assurance area

• Senior QA Automation Engineer

• PhD, Theoretical physics

2

TestNG vs. JUnit, what do you think?

3

Agenda

• JUnit

• TestNG

• Suites

• Fixtures

• Assertions

• Soft Assertions

• Hamcrest

• Ignore

• Timeout

• Exception

• Parameterization

• Grouping

• Dependencies

• Listeners

• Reporters

• Parallelization

4

• JUnit is a unit testing framework for the Javaprogramming language. JUnit has beenimportant in the development of test-drivendevelopment, and is one of a family of unittesting frameworks which is collectivelyknown as xUnit that originated with SUnit.

5

• TestNG is a testing framework for the Javaprogramming language created by CédricBeust and inspired by JUnit and NUnit. Thedesign goal of TestNG is to cover a widerrange of test categories: unit, functional, end-to-end, integration, etc., with more powerfuland easy-to-use functionalities.

6

JUnit Suites

7

TestNG Suites (testing.xml)

8

JUnit Fixtures

• @BeforeClass setUpClass (static)

• @Before setUp

• @Test test1()

• @After tearDown

• @Before setUp

• @Test test2()

• @After tearDown

• @AfterClass tearDownClass (static)

9

TestNG Fixtures

• @BeforeMethod

• @AfterMethod

• @BeforeClass

• @AfterClass

• @BeforeTest

• @AfterTest

• @BeforeGroups

• @AfterGroups

• @BeforeSuite

• @AfterSuite

10

JUnit Asserts

• assertEquals

• assertTrue

• assertFalse

• assertNull

• assertNotNull

• assertThat

• assertArrayEquals

• assertSame

• fail

11

JUnit Asserts

• assertEquals(java.lang.Object expected, java.lang.Object actual)

• assertEquals(java.lang.String message, java.lang.Objectexpected, java.lang.Object actual)

• fail()

• fail(java.lang.String message)

• …

12

TestNG Asserts

• assertEquals

• assertEqualsNoOrder

• assertFalse

• assertNotEquals

• assertNotNull

• assertNotSame

• assertNull

• assertSame

• assertThrows

• assertTrue

• expectThrows

• fail

13

TestNG Asserts

• assertEquals(java.lang.Object actual, java.lang.Object expected)

• assertEquals(java.lang.Object actual, java.lang.Object expected,java.lang.String message )

• fail()

• fail(java.lang.String message)

• fail(java.lang.String message, java.lang.Throwable realCause)

• …

14

TestNG Soft Asserts

15

JUnit Soft Asserts

16

JUnit Hamcrest

17

TestNG Hamcrest

18

JUnit (Timeout, Ignoring)

• Timeout for tests

• Ignoring tests

19

TestNG timeOut

• Timeout for particular tests

• Timeout for suites

20

TestNG test Ignoring• Ignore a test

• Throw SkipException

• Exclude test

21

JUnit exception testing

22

JUnit exception testing (with @Rule)

23

TestNG exception testing

24

JUnit parameterization

25

JUnit parameterization

26

TestNG parameterization

27

TestNG parameterization

28

• @Parameters

• @DataProvider

JUnit parameterization (JUnitParams)

29

JUnit parameterization (junit-dataprovider)

30

JUnit parameterization (junit-dataprovider)

31

JUnit categories

32

TestNG groups

33

• Groups of Methods

• Groups of Classes

• Group of Groups

JUnit dependencies

34

TestNG dependencies

35

• dependsOnMethods

• dependsOnGroups

JUnit RunListener

36

TestNG listeners

37

ITestListener

• onStart

• onTestSuccess

• onTestFailure

• onTestSkipped

• onTestFailedButWithinSuccessPercentage

IInvokedMethodListener

• beforeInvocation

• afterInvocation

TestNG listeners (how to include)

38

TestNG listeners

39

IExecutionListener

onExecutionStart()

onExecutionFinish()

ISuiteListener

onStart

onFinish

IReporter

generateReport

JUnit parallelization

40

TestNG parallelization

41

Maven plugin

The suite tag has parallel andthread-count attributes

@Test has threadPoolSiseattribute

More details about JUnit

• Official Website: http://junit.org/junit4/

• Assert: http://junit.org/junit4/javadoc/latest/org/junit/Assert.html

• Junit Wiki on GitHub: https://github.com/junit-team/junit4/wiki

• JUnit Tutorial (Mkyong.com) http://www.mkyong.com/tutorials/junit-tutorials/

• RunListener: http://junit.sourceforge.net/javadoc/org/junit/runner/notification/RunListener.html

• How to implement RunListener a JUnit Listener example: http://memorynotfound.com/add-junit-listener-example/

42

More details about some additional libs

• AssertJ http://joel-costigliola.github.io/assertj/index.html

• Hamcrest http://hamcrest.org/JavaHamcrest/

• The Benefits of Using assertThat over other Assert Methods in Unit Tests https://objectpartners.com/2013/09/18/the-benefits-of-using-assertthat-over-other-assert-methods-in-unit-tests/

• Hamcrest matchers tutorial https://www.javacodegeeks.com/2015/11/hamcrest-matchers-tutorial.html

• JUnitParams: https://github.com/Pragmatists/JUnitParams

• junit-dataprovider: https://github.com/TNG/junit-dataprovider

43

More details about TestNG

• Official Website: http://testng.org/doc/documentation-main.html

• Assert: http://testng.org/javadocs/org/testng/Assert.html

• ITestListener: http://testng.org/javadocs/org/testng/ITestListener.html

• IInvokedMethodListener: http://testng.org/javadocs/org/testng/IInvokedMethodListener.html

• TestNG Listeners Example: https://examples.javacodegeeks.com/enterprise-java/testng/testng-listeners-example/

• TestNG Tutorial (Mkyong.com) http://www.mkyong.com/tutorials/testng-tutorials/

44

Questions

Thank you!

Recommended