46
TestNg vs. JUnit Speaker: Andrey Oleynik Date: 26/12/2016

TestNG vs. JUnit

Embed Size (px)

Citation preview

Page 1: TestNG vs. JUnit

TestNg vs. JUnit

Speaker: Andrey OleynikDate: 26/12/2016

Page 2: TestNG vs. JUnit

About me

Andrey Oleynik

• About 9 years of work in Quality Assurance area

• Senior QA Automation Engineer

• PhD, Theoretical physics

2

Page 3: TestNG vs. JUnit

TestNG vs. JUnit, what do you think?

3

Page 4: TestNG vs. JUnit

Agenda

• JUnit

• TestNG

• Suites

• Fixtures

• Assertions

• Soft Assertions

• Hamcrest

• Ignore

• Timeout

• Exception

• Parameterization

• Grouping

• Dependencies

• Listeners

• Reporters

• Parallelization

4

Page 5: TestNG vs. JUnit

• 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

Page 6: TestNG vs. JUnit

• 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

Page 7: TestNG vs. JUnit

JUnit Suites

7

Page 8: TestNG vs. JUnit

TestNG Suites (testing.xml)

8

Page 9: TestNG vs. JUnit

JUnit Fixtures

• @BeforeClass setUpClass (static)

• @Before setUp

• @Test test1()

• @After tearDown

• @Before setUp

• @Test test2()

• @After tearDown

• @AfterClass tearDownClass (static)

9

Page 10: TestNG vs. JUnit

TestNG Fixtures

• @BeforeMethod

• @AfterMethod

• @BeforeClass

• @AfterClass

• @BeforeTest

• @AfterTest

• @BeforeGroups

• @AfterGroups

• @BeforeSuite

• @AfterSuite

10

Page 11: TestNG vs. JUnit

JUnit Asserts

• assertEquals

• assertTrue

• assertFalse

• assertNull

• assertNotNull

• assertThat

• assertArrayEquals

• assertSame

• fail

11

Page 12: TestNG vs. JUnit

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

Page 13: TestNG vs. JUnit

TestNG Asserts

• assertEquals

• assertEqualsNoOrder

• assertFalse

• assertNotEquals

• assertNotNull

• assertNotSame

• assertNull

• assertSame

• assertThrows

• assertTrue

• expectThrows

• fail

13

Page 14: TestNG vs. JUnit

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

Page 15: TestNG vs. JUnit

TestNG Soft Asserts

15

Page 16: TestNG vs. JUnit

JUnit Soft Asserts

16

Page 17: TestNG vs. JUnit

JUnit Hamcrest

17

Page 18: TestNG vs. JUnit

TestNG Hamcrest

18

Page 19: TestNG vs. JUnit

JUnit (Timeout, Ignoring)

• Timeout for tests

• Ignoring tests

19

Page 20: TestNG vs. JUnit

TestNG timeOut

• Timeout for particular tests

• Timeout for suites

20

Page 21: TestNG vs. JUnit

TestNG test Ignoring• Ignore a test

• Throw SkipException

• Exclude test

21

Page 22: TestNG vs. JUnit

JUnit exception testing

22

Page 23: TestNG vs. JUnit

JUnit exception testing (with @Rule)

23

Page 24: TestNG vs. JUnit

TestNG exception testing

24

Page 25: TestNG vs. JUnit

JUnit parameterization

25

Page 26: TestNG vs. JUnit

JUnit parameterization

26

Page 27: TestNG vs. JUnit

TestNG parameterization

27

Page 28: TestNG vs. JUnit

TestNG parameterization

28

• @Parameters

• @DataProvider

Page 29: TestNG vs. JUnit

JUnit parameterization (JUnitParams)

29

Page 30: TestNG vs. JUnit

JUnit parameterization (junit-dataprovider)

30

Page 31: TestNG vs. JUnit

JUnit parameterization (junit-dataprovider)

31

Page 32: TestNG vs. JUnit

JUnit categories

32

Page 33: TestNG vs. JUnit

TestNG groups

33

• Groups of Methods

• Groups of Classes

• Group of Groups

Page 34: TestNG vs. JUnit

JUnit dependencies

34

Page 35: TestNG vs. JUnit

TestNG dependencies

35

• dependsOnMethods

• dependsOnGroups

Page 36: TestNG vs. JUnit

JUnit RunListener

36

Page 37: TestNG vs. JUnit

TestNG listeners

37

ITestListener

• onStart

• onTestSuccess

• onTestFailure

• onTestSkipped

• onTestFailedButWithinSuccessPercentage

IInvokedMethodListener

• beforeInvocation

• afterInvocation

Page 38: TestNG vs. JUnit

TestNG listeners (how to include)

38

Page 39: TestNG vs. JUnit

TestNG listeners

39

IExecutionListener

onExecutionStart()

onExecutionFinish()

ISuiteListener

onStart

onFinish

IReporter

generateReport

Page 40: TestNG vs. JUnit

JUnit parallelization

40

Page 41: TestNG vs. JUnit

TestNG parallelization

41

Maven plugin

The suite tag has parallel andthread-count attributes

@Test has threadPoolSiseattribute

Page 42: TestNG vs. JUnit

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

Page 43: TestNG vs. JUnit

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

Page 44: TestNG vs. JUnit

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

Page 45: TestNG vs. JUnit

Questions

Page 46: TestNG vs. JUnit

Thank you!