TestNG vs. JUnit
-
Upload
andrey-oleynik -
Category
Software
-
view
242 -
download
1
Transcript of TestNG vs. JUnit
![Page 1: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/1.jpg)
TestNg vs. JUnit
Speaker: Andrey OleynikDate: 26/12/2016
![Page 2: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/2.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/3.jpg)
TestNG vs. JUnit, what do you think?
3
![Page 4: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/4.jpg)
Agenda
• JUnit
• TestNG
• Suites
• Fixtures
• Assertions
• Soft Assertions
• Hamcrest
• Ignore
• Timeout
• Exception
• Parameterization
• Grouping
• Dependencies
• Listeners
• Reporters
• Parallelization
4
![Page 5: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/5.jpg)
• 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](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/6.jpg)
• 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](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/7.jpg)
JUnit Suites
7
![Page 8: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/8.jpg)
TestNG Suites (testing.xml)
8
![Page 9: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/9.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/10.jpg)
TestNG Fixtures
• @BeforeMethod
• @AfterMethod
• @BeforeClass
• @AfterClass
• @BeforeTest
• @AfterTest
• @BeforeGroups
• @AfterGroups
• @BeforeSuite
• @AfterSuite
10
![Page 11: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/11.jpg)
JUnit Asserts
• assertEquals
• assertTrue
• assertFalse
• assertNull
• assertNotNull
• assertThat
• assertArrayEquals
• assertSame
• fail
11
![Page 12: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/12.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/13.jpg)
TestNG Asserts
• assertEquals
• assertEqualsNoOrder
• assertFalse
• assertNotEquals
• assertNotNull
• assertNotSame
• assertNull
• assertSame
• assertThrows
• assertTrue
• expectThrows
• fail
13
![Page 14: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/14.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/15.jpg)
TestNG Soft Asserts
15
![Page 16: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/16.jpg)
JUnit Soft Asserts
16
![Page 17: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/17.jpg)
JUnit Hamcrest
17
![Page 18: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/18.jpg)
TestNG Hamcrest
18
![Page 19: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/19.jpg)
JUnit (Timeout, Ignoring)
• Timeout for tests
• Ignoring tests
19
![Page 20: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/20.jpg)
TestNG timeOut
• Timeout for particular tests
• Timeout for suites
20
![Page 21: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/21.jpg)
TestNG test Ignoring• Ignore a test
• Throw SkipException
• Exclude test
21
![Page 22: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/22.jpg)
JUnit exception testing
22
![Page 23: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/23.jpg)
JUnit exception testing (with @Rule)
23
![Page 24: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/24.jpg)
TestNG exception testing
24
![Page 25: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/25.jpg)
JUnit parameterization
25
![Page 26: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/26.jpg)
JUnit parameterization
26
![Page 27: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/27.jpg)
TestNG parameterization
27
![Page 28: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/28.jpg)
TestNG parameterization
28
• @Parameters
• @DataProvider
![Page 29: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/29.jpg)
JUnit parameterization (JUnitParams)
29
![Page 30: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/30.jpg)
JUnit parameterization (junit-dataprovider)
30
![Page 31: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/31.jpg)
JUnit parameterization (junit-dataprovider)
31
![Page 32: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/32.jpg)
JUnit categories
32
![Page 33: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/33.jpg)
TestNG groups
33
• Groups of Methods
• Groups of Classes
• Group of Groups
![Page 34: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/34.jpg)
JUnit dependencies
34
![Page 35: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/35.jpg)
TestNG dependencies
35
• dependsOnMethods
• dependsOnGroups
![Page 36: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/36.jpg)
JUnit RunListener
36
![Page 37: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/37.jpg)
TestNG listeners
37
ITestListener
• onStart
• onTestSuccess
• onTestFailure
• onTestSkipped
• onTestFailedButWithinSuccessPercentage
IInvokedMethodListener
• beforeInvocation
• afterInvocation
![Page 38: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/38.jpg)
TestNG listeners (how to include)
38
![Page 39: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/39.jpg)
TestNG listeners
39
IExecutionListener
onExecutionStart()
onExecutionFinish()
ISuiteListener
onStart
onFinish
IReporter
generateReport
![Page 40: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/40.jpg)
JUnit parallelization
40
![Page 41: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/41.jpg)
TestNG parallelization
41
Maven plugin
The suite tag has parallel andthread-count attributes
@Test has threadPoolSiseattribute
![Page 42: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/42.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/43.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/44.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/45.jpg)
Questions
![Page 46: TestNG vs. JUnit](https://reader033.fdocuments.net/reader033/viewer/2022051006/58aa44f61a28ab4c348b5e5f/html5/thumbnails/46.jpg)
Thank you!