Friday, December 23, 2011

Better testing with Testng


Most of the programmers busy with writing code and forget the importance of having tested the implementations then and there.When you start writing tests you should select the best suitable framework for your job or else you will make things worse. There are few different tests such as
  • Unit test : You have a clear narrow and well defined scope to check a single point of large story/ contract
  • Integration test : In this you will be checking inter operation with more than one sub systems and may expand across system boundaries.
  • Smoke test/ sanity tests: This is a basic integration test for a product to verify system perform its basic functionality as expected
  • Regression test : Test to verify a fixed bug is actually fixed and it will not encounter again
  • Acceptance test : This is also a form of integration test which carried out to verify a use case or a user story
Out of these tests JUnit is adequate to perform Unit tests but for integration tests it has some limitations. If you have some complex dependencies between modules or states such as a sever needed to be started with deployed artiface before all the tests execute or establish a database connection with some preconditions you will find hard time if you choose JUnit .One key important thing you need to keep in mind is “JUnit instantiate class before it runs each and every test method”.
There are lots of comparisons between JUnit and Testng [1][2].When you go through them you will realize how Testng becomes your trusted companion for tests such as integration tests.Testng provides you annotations to control things better and a very cleaner way.Not only that you have custom testng.xml file which enables to customize things to accomplish lots of things as desirable.
If you wish to migrate to Testng there are lots of resources available to learn about Testng.But i advice do not try to convert JUnit tests directly but rather modify them to inherit elegance of Testng :) .
  • Add Testng as a maven dependency
<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>6.1.1</version>
  <scope>test</scope>
</dependency>

  • Annotations available [3]
@BeforeSuite: The annotated method will be run before all tests in this suite have run.
@AfterSuite: The annotated method will be run after all tests in this suite have run.
@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.
@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run.
@BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.
@AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.
@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.
@AfterClass: The annotated method will be run after all the test methods in the current class have been run.
@BeforeMethod: The annotated method will be run before each test method.
@AfterMethod: The annotated method will be run after each test method.
  • How to handle Expected exceptions

@Test(expectedExceptions = ExpectedException.class)
  public void expectedExceptionTester() {
    throwAnException();
  }
The above method should throw the expected exception otherwise it should fail.

6 comments:



  1. Nhìn bản thể của Thái Dương Chân Hỏa ở trên không trung, trong lòng Nhạc Thành không khỏi cảm thấy run run, may mà mình đã luyện hóa được linh trí của nó nếu không thì phiền toái sẽ rất là lớn, bản thể của Thái Dương Chân Hỏa mình tuyệt đối không thể nào đối phó.

    - Thu.
    dongtam
    mu private
    tim phong tro
    http://nhatroso.com/
    nhac san cuc manh
    tổng đài tư vấn luật
    http://dichvu.tuvanphapluattructuyen.com/
    văn phòng luật
    tổng đài tư vấn luật
    dịch vụ thành lập công ty
    http://we-cooking.com/
    chém gió
    trung tâm ngoại ngữ
    Thủ ấn của Nhạc Thành tu lại, một luồng Thái Dương Chân Hỏa bao vây lấy bản thể của Thái Dương Chân Hỏa thu hồi từ trên không trung.

    - Xèo xèo.

    Nhạc Thành khẽ há miệng hút lấy, Thái Dương Chân Hỏa đã bị Nhạc Thành hút vào trong bụng.

    - A…

    Một thanh âm thảm thiết kêu lên từ trong miệng của Nhạc Thành, Thái Dương Chân Hỏa tiến vào trong người của Nhạc Thành xong thì biến thành một ngọn lửa nóng bỏng, ngọn lửa này so với

    ReplyDelete