TestNG makes Java unit testing easy
For an in-depth look at testng, please visit: http://www.yiibai.com/html/testng/2013/0914295.html
During the construction phase of each package, the test phase plays a central role in the player. In the past, the days of compiling and testing were gone, and now most developers now recognize the need to encode and test software methodologies that are intertwined and synchronized, so that bugs can be identified early and risk identified at the start of the development process.
TestNG Fast Advanced
1. TestNG Overview
TestNG is an annotation-based test framework that overcomes some of the shortcomings of JUnit by adding features such as flexible devices, test classifications, parameter testing, dependency methods, data driven, and so on. Because testng can easily categorize developer tests into unit groups
Components combine system groups, so that build times can be kept manageable. By using group annotations and multiple ant tasks, test groups can run at different frequencies on a single workstation or in an environment that is continuously integrated.
The process of writing a test has 3 typical steps
(1) Write the business logic of the test and insert the testng annotation in the code
(2) Adding test information to Testng.xml or Build.xml
(3) Running testng
2. testng Declaration Period
The complete life cycle of the testng test case goes through the stages: class-level initialization of resource processing, method-level initialization of resource processing, execution of methods in test cases, method-level destruction of resource handling, and class-level destruction of resource processing.
3. Using testng
Use @test annotations in testng to label a test method. You can also import assert assert classes with the JDK5 static import feature, which makes it easy to use assertions in test methods.
Package org.worm.biz;import org.testng.annotations.test;import org.worm.util.stringutil;import org.testng.annotations.BeforeClass;import org.testng.annotations.AfterClass;public class stringutiltest { @Test public void f () { System.out.println ("This is testng test case"); } @Test Public void teststr () { string trimstr = stringutil.gettrimstr (" foo system.out.println ("Test by:" +trimstr); } @Test public void testwithassert () { assert "foo". Equals (Stringutil.gettrimstr ( " foo "); } @BeforeClass public void beforeclass () { system.out.println ("This is before test method"); } @AfterClass publIc void afterclass () { system.out.println ("This is after test method "); }}
However, before running the test, you must configure the testng with a special XML file, which is customary to name the file testng.xml. This file first defines the test suite suite, which contains only one test, which is done by the Stringutiltest class.
<?xml version= "1.0" encoding= "UTF-8"? ><suite name= "Suite" parallel= "false" > <test name= "Test" > < classes> <class name= "org.worm.biz.StringUtilTest"/> </classes> </test> <!--test-->& Lt;/suite> <!--Suite-
Test results:
[TestNG] Running:d:\workspace\workspace_worm\worm-parent\worm-biz\src\test\java\testng.xmlthis is before Test methodthis is TestNG test case tested by: Foothis is after test method===============================================suitetotal tests Run:3, failures:0, skips:0===============================================
This article is from the "Ah Cool blog source" blog, please make sure to keep this source http://aku28907.blog.51cto.com/5668513/1826981
Spring Unit Test Weapon--testng