- Test-Driven Java Development(Second Edition)
- Alex Garcia Viktor Farcic
- 480字
- 2021-06-24 18:31:47
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.
- Oracle從入門到精通(第3版)
- Computer Vision for the Web
- 大學(xué)計算機基礎(chǔ)實驗教程
- 精通API架構(gòu):設(shè)計、運維與演進
- Building a Recommendation Engine with Scala
- Reactive Programming With Java 9
- Apex Design Patterns
- AutoCAD VBA參數(shù)化繪圖程序開發(fā)與實戰(zhàn)編碼
- 學(xué)Python也可以這么有趣
- Android Wear Projects
- Android傳感器開發(fā)與智能設(shè)備案例實戰(zhàn)
- 深入理解BootLoader
- Modernizing Legacy Applications in PHP
- After Effects CC技術(shù)大全
- Instant GLEW