testng

22
TestNG Testing code as you write it Haritha K

Upload: harithakannan

Post on 26-Jun-2015

352 views

Category:

Technology


3 download

DESCRIPTION

java unit testing through testng

TRANSCRIPT

Page 1: testng

TestNGTesting code as you write it

Haritha K

Page 2: testng

What is TestNG

A testing framework designed to simplify a broad range of development testing needs.

• Unit testing (testing a class in isolation of the others)

• Integration testing (testing entire systems made of several classes, several packages and even several external frameworks, such as application servers).

Page 3: testng

What is TestNG?

• Automated testing framework• NG = Next Generation• Similar to JUnit (especially JUnit 4)• Not a JUnit extension (but inspired by JUnit)• Designed to be better than JUnit, especially

when testing integrated classes• Created by Dr. Cédric Beust (of Google)• Open source (http://testng.org)

Page 4: testng

Installing in eclipse

• The latest version of TestNG can be downloaded from

http://search.maven.org/• In Eclipse, Select Help / Software updates / Find and Install.• Search for new features to install.• New remote site.• For Eclipse 3.4 and above, enter http://beust.com/eclipse.• For Eclipse 3.3 and below, enter http://beust.com/eclipse1.• Make sure the check box next to URL is checked and

click Next.• Eclipse will then guide you through the process and restart

eclipse.

Page 5: testng

Basic Three steps

• Write the business logic of your test and insert TestNG Annotations in your code.

• Add the information about your test (e.g. the class name, the groups you wish to run, etc...) in a testng.xml file.

• Run TestNG.

Page 6: testng

Keywords

• A suite is represented by one XML file. It can contain one or more tests and is defined by the <suite> tag.

• A test is represented by <test> and can contain one or more TestNG classes.

• A TestNG class is a Java class that contains at least one TestNG annotation. It is represented by the <class> tag and can contain one or more test methods.

• A test method is a Java method annotated by @Test in your source.

Page 7: testng

Possible configurations in xml file

• Class names• Package names ( will execute all test classes)• Groups and methods (include/exclude)• run the tests in parallel, how many threads to use• TestNG will run your tests in the order they are found

in the XML file. If you want the classes and methods listed in this file to be run in an unpredictable order, set the preserve-order attribute to false

Page 8: testng

Annotations

@Test @BeforeSuite@AfterSuite@BeforeTest@AfterTest@BeforeGroups@AfterGroups@BeforeClass@AfterClass@BeforeMethod@AfterMethod@DataProvider @Parameters

Page 9: testng

Assertions

• assertEquals• assertNotEquals• assertNotNull• assertNull• assertSame• assertNotSame• assertTrue• assertFalse• fail

Page 10: testng

Groups

• Each test method is tagged with any number of groups.• @Test // no groups• @Test (groups = “group1”)• @Test (groups = { “g1”, “g2”, ... })

• A group therefore contains any number of test methods.• Groups can span classes.• Groups can also be externally defined (TestNG xml

configuration file).• A group is identified by a unique string (don’t use white space).

• There are no pre-defined group names.• E.g., “slow”, “fast”, “gui”, “check-in”, “week-end”

“unit”,“regression”,“integration”,“broken.unknownReason”

Page 11: testng

Groups continued…

• TestNG community suggests hierarchical names from more general to less. E.g.:• database.table.CUSTOMER• alarm.severity.cleared

• Design group names so that you can select them with prefix patterns.

• Groups complement other features

Page 12: testng

Groups continued…

You can define groups at the class level and then add groups at the method level

@Test(groups = { “goldenRegression" })public class All {    @Test(groups = { “regression" ) 

 public void method1() { }   public void method2() { ... }}In this class, method2() is part of the group “goldenRegression",

which is defined at the class level, while method1() belongs to both “goldenRegression" and “regression".

Page 13: testng

Exceptions

• Methods can have more than one exception thrown

@Test(expectedExceptions = NullPointerException.class)

Or

@Test(expectedExceptions = { T1.class, ... })

Page 14: testng

Ignored Test cases

Enable or disable tests• @Test(enabled = false)• Add to a group which is excluded• Exclude in other ways in testng.xml

Page 15: testng

Timeout

• @Test(timeOut = 1000)• testng.xml <suite|test> time-out attribute• The test case will be failed if time period is

exceeded

Page 16: testng

Dependencies

Sometimes, you need your test methods to be invoked in a certain order

• To make sure a certain number of test methods have completed and succeeded before running more test methods.

• To initialize your tests while wanting this initialization methods to be test methods as well.

Page 17: testng

Dependency continued

• fail fast:• run Selenium tests only if application was deployed properly,• run full system tests only if smoke tests passed,

• logical dependencies between tests:• execute shouldDeleteUserFromDatabase test only

ifshouldAddUserToDatabase worked• Fail fast means that the feedback will be much quicker in case

of failed tests.Logical dependencies gives you a much more realistic error information - you learn that 1 tests has failed and 99 has been skipped, which is much easier to fix than the information about 100 failed tests (OMG! what’s going on!? All tests failed)

Page 18: testng

Parameterized tests

• In general, it is a good practice, to test your code with different sets of parameters:

• expected values: sqrt(4), sqrt(9),• boundary values: sqrt(0),• strange/unexpected values: sqrt(-1), sqrt(3)

Page 19: testng

Parameterized Tests continued

• Parameterized tests are very simple with TestNG.

• You can have as many data providers in one class as you wish. You can reuse them (call them from other classes), and you can make them "lazy", so each set of parameters is created when required.

Page 20: testng

Parameterized Tests continued

@Parameters({ "datasource", "jdbcDriver" }) @BeforeMethod public void beforeTest(String ds, String driver) { m_dataSource = ...; m_jdbcDriver = driver; }

@DataProvider(name = "test1")

public Iterator<Object[]> createData() {  return new MyIterator(DATA);}

Page 21: testng

References

• http://testng.org/doc/index.html• http://testng.org/doc/documentation-main.htm

l• http://testng.org/testng-1.0.dtd.php• http://testng.org/javadoc/

Page 22: testng

Thank you