官术网_书友最值得收藏!

TestNG

In TestNG (http://testng.org/doc/index.html), tests are organized in classes, just as in the case of JUnit.

The following Gradle configuration (build.gradle) is required in order to run TestNG tests:

dependencies { 
   testCompile group: 'org.testng', name: 'testng', version: '6.8.21' 
} 
 
test.useTestNG() { 
// Optionally you can filter which tests are executed using 
// exclude/include filters // excludeGroups 'complex' }

Unlike JUnit, TestNG requires additional Gradle configuration that tells it to use TestNG to run tests.

The following test class is written with TestNG and is a reflection of what we did earlier with JUnit. Repeated imports and other boring parts are omitted with the intention of focusing on the relevant parts:

@BeforeClass 
public static void beforeClass() { 
  // This method will be executed once on initialization time 
} 
 
@BeforeMethod 
public void before() { 
  friendships = new Friendships(); 
  friendships.makeFriends("Joe", "Audrey"); 
  friendships.makeFriends("Joe", "Peter"); 
  friendships.makeFriends("Joe", "Michael"); 
  friendships.makeFriends("Joe", "Britney"); 
  friendships.makeFriends("Joe", "Paul"); 
} 

You probably already noticed the similarities between JUnit and TestNG. Both are using annotations to specify what the purposes of certain methods are. Besides different names (@Beforeclass versus @BeforeMethod), there is no difference between the two. However, unlike Junit, TestNG reuses the same test class instance for all test methods. This means that the test methods are not isolated by default, so more care is needed in the before and after methods.

Asserts are very similar as well:

public void alexDoesNotHaveFriends() { 
  Assert.assertTrue(friendships.getFriendsList("Alex").isEmpty(), 
      "Alex does not have friends"); 
} 

public void joeHas5Friends() { Assert.assertEquals(friendships.getFriendsList("Joe").size(),
5, "Joe has 5 friends"); }
public void joeIsFriendWithEveryone() { List<String> friendsOfJoe =
Arrays.asList("Audrey", "Peter", "Michael", "Britney", "Paul");
Assert.assertTrue(friendships.getFriendsList("Joe")
.containsAll(friendsOfJoe)); }

The only notable difference when compared with JUnit is the order of the assert variables. While the JUnit assert's order of arguments is optional message, expected values, and actual values, TestNG's order is an actual value, expected value, and optional message. Besides the difference in the order of arguments we're passing to the assert methods, there are almost no differences between JUnit and TestNG.

You might have noticed that @Test is missing. TestNG allows us to set it on the class level and thus convert all public methods into tests.

The @After annotations are also very similar. The only notable difference is the TestNG @AfterMethod annotation that acts in the same way as the JUnit @After annotation.

As you can see, the syntax is pretty similar. Tests are organized in to classes and test verifications are made using assertions. That is not to say that there are no more important differences between those two frameworks; we'll see some of them throughout this book. I invite you to explore JUnit (http://junit.org/) and TestNG (http://testng.org/) by yourself.

The complete source code with the preceding examples can be found at https://bitbucket.org/vfarcic/tdd-java-ch02-example-testng.

The assertions we have written until now have used only the testing frameworks. However, there are some test utilities that can help us make them nicer and more readable.

主站蜘蛛池模板: 开原市| 雷州市| 漾濞| 金塔县| 抚远县| 新兴县| 楚雄市| 贵南县| 桐城市| 商河县| 三亚市| 南部县| 贵港市| 潮州市| 郴州市| 宾阳县| 湖口县| 平邑县| 万安县| 开鲁县| 武功县| 固镇县| 云龙县| 浦北县| 南靖县| 承德县| 丹凤县| 松潘县| 大丰市| 山丹县| 龙南县| 鹤庆县| 临江市| 拉萨市| 金溪县| 布拖县| 张家川| 大厂| 全椒县| 西充县| 佳木斯市|