- 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.
- Design Principles for Process:driven Architectures Using Oracle BPM and SOA Suite 12c
- 工程軟件開發技術基礎
- Python 深度學習
- 數據結構與算法JavaScript描述
- Learning OpenStack Networking(Neutron)
- 利用Python進行數據分析
- Rust游戲開發實戰
- R語言數據挖掘:實用項目解析
- 大學計算機應用基礎(Windows 7+Office 2010)(IC3)
- 計算機應用基礎案例教程(第二版)
- Python網絡運維自動化
- Prezi Cookbook
- 活文檔:與代碼共同演進
- Visual C++2013從入門到精通(視頻教學版)
- OpenCV Computer Vision with Python