Compare study, Writing tests in Java : Junit3-Junit4-TestNG

In this article we are going to see learn about TestNG, JUnit 3 & JUnit 4 by Comparison. All of them are Java test runners but, while we are writing tests , what are annotations and basic work differences among Junit3-Junit-TestNG will be described here. It is very helpful for quick review on unit tests.

As we know, all of them are initially Unit test framework and later on they come up with intelligent reporting, more advance testing concepts like theory & assumption. But all of them are commonly use different ways of testing. I wont be talking about in detail differences but, we will talk about more or less overall functionality with comparison.

No

Purpose

Junit 3

Junit 4

TestNG

1

Tests declearation

testXXX pattern

@Test

@Test

2

Run a method Before each Test Method

override setUp()

@Before

@BeforeMethod

3

Run a method after a each Test Method

override tearDown()

@After

@AfterMethod

4

Ignoring a test

Remove/Comment

@ignore

@Test(enabled=false)
[opposite of ignore]/[works for upper levels]

5

Run a method before a Test Class

_

@BeforeClass

@BeforeClass

6

Run a method after a Test Class

_

@AfterClass

@AfterClass

7

Run a method before a Test Class which is decleared with <test> tag in xml configuration

_

_

@BeforeTest

8

Run a method after a Test Class which is decleared with <test> tag in xml configuration

_

_

@AfterTest

9

Runs a method before a suit

_

_

@BeforeSuite

10

Runs a method after a suit

_

_

@AfterSuite

11

Runs a method before a group configured in XML

_

_

@BeforeGroups

12

Runs a method after a group configured in XML

_

_

@AfterGroups

13

Exeption Checking

explicitly call fail & then assert in catch block

@Test(expected = <Name of the Exception>.class)

@Test(expectedExceptions = ExceptionClassName.class)

14

Timeout for test execution

_

@Test(timeout = 1000)

@Test(timeout = timeoutinmilisecond)

15

invoke thereads while running test parallelly

_

_

@Test(threadPoolSize = threadNumber)

16

Run a test method all the time(ignores dependent tests)

_

_

@Test(alwaysRun=true
)[works for upper levels]

17

List of groups a class/method is designed to belong

_

_

@Test(groups = Name of the group specified in XML)[works for upper levels]

18

Description

_

_

@Test(description=The description for the tests)

19

How many times this method will run

_

_

@Test(invocationCount=<an integer number>)

20

When invocation counter set, we can set timeout for that test execution with invocations.

_

_

@Test(invocationTimeOut=)

21

Prioritizing test cases, low value runs first

_

_

@Test(priority=<an integer value>)

22

Defining Expected success percentage

_

_

@Test(successPercentage=<an integer value>)

23

Explicitly difining to run a method in single threaded(of its class)

_

_

@Test(singleThreaded=true)

24

Define name of the data provider(in XML) for a test method

_

_

@Test(dataProvider=NameOfProvider)

25

Define name of the data provider Class for a test method

_

_

@Test(dataProviderClass=DataProviderClassName.class)

26

Run all dependents methods specified here before this test run

_

_

@Test(dependsOnMethods=Name of all methods)[works for upper levels]

27

Run all dependents methods under all dependent groups specified here before this test run

_

_

@Test(dependsOnGroups=Name of all Groups)[works for upper levels]

28

Defining Test Runner/Class

_

@RunWith(SpringJUnit4ClassRunner.class)

_

29

Define all tests include in a suit

_

@SuiteClasses

_

30

Marks a method as supplying data for a test method , must return an Object[][]

_

@DataProvider by TNG

@DataProvider

31

Add multiple imput values as parameter(initialize while adding as parameter of test method)

_

@TestWith by zohhak

_

32

Rules initilize (of Test Rules type)at method level

_

@Rules

_

33

Fixing ordering of test execution at class level

_

@FixMethodOrder

_

34

Rules initilize (of Test Rules type) at class level/static

_

@ClassRule

_

35

Aulternative @test method run as theories by assumptions

_

@Theory

_

36

Multiple Data Input for theries

_

@TestedOn

_

37

iteratable of data points in array

_

@DataPoints

_

38

Assign of a data point(field/method)

_

@DataPoint

_

39

Parametersupplier provided values used as test data in @Theory

_

@ParametersSuppliedBy

_

40

Parameter of @Theory decleared as @datapoints and will be selected by this

_

@FromDataPoints

_

41

Makes a tese/class as belonging of another test catagories

_

@Category

_

42

Validate any test method using annotationValidator(use in your custom annotation)

_

@ValidateWith

_

43

single test data field(you can add values, default start 0)

_

@Parameter

_

44

Test data in a iteratable collection

_

@Parameters

@parameters={arrayOfStrings}

45

Runs only the classes and methods that are annotated with either the category given with this

_

@IncludeCategory

_

46

Excluds classes and methods that are not annotated with either the category or given with this

_

@ExcludeCategory

_

47

Defines a listner in a test class

_

 

@Listeners

48

Marks a method as a factory that returns objects that will be used by TestNG as Test classes. The method must return Object[]

_

 

@Factory



I hope, this will give basic idea to start testing with these unit test framework. I will provide separate post on how to organize & plan your tests with these test frameworks.
Thanks … Smile 

No comments:

Post a Comment