Java測試新技術 ---- TestNG

持續更新中…關於TestNg問題可留言討論

1、TestNg組成結構

1.1 Annotation

配置annotation(Before&After開頭):

@BeforeSuite、@BeforeTest、@BeforeClass、@BeforeMethod、@BeforeGroups

@AfterSuite、@AfterTest、@AfterClass、@AfterMethod、@AfterGroups

@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.

其它annotation

    @DataProvider
    
    @ExpectedExceptions
    
    @Factory
    
    @Test
    
    @Parameters

1.2 testng.xml

以xml記錄所有測試的文件,其描述所有測試塊和其參數,可嵌入到代碼庫中,制定測試內容,編譯器會根據testng.xml執行測試任務。

suite由一個或多個測試組成;
test由一個或多個類組成;
class由一個或多個方法組成。

指定類名

<suite name="Suite1" verbose="1" >
    <test name="Nopackage" >
        <classes>
            <class name="NoPackageTest" />
        </classes>
    </test>

    <test name="Regression1">
        <classes>
            <class name="test.sample.ParameterSample"/>
            <class name="test.sample.ParameterTest"/>
    </classes>
    </test>
</suite>

指定包名

<suite name="Suite1" verbose="1" >
    <test name="Regression1"   >
        <packages>
            <package name="test.sample" />
        </packages>
    </test>
</suite>

指定包含和排除的組和方法

<test name="Regression1">
    <groups>
        <run>
            <exclude name="brokenTests"  />
            <include name="checkinTests"  />
        </run>
    </groups>

    <classes>
        <class name="test.IndividualMethodsTest">
            <methods>
                <include name="testMethod" />
            </methods>
        </class>
    </classes>
</test>

2、測試設計模式

2.1 錯誤報告

  • 返回錯誤碼
  • 返回參數
  • 異常

2.2 測試方法對比

任務:目標對象兩個任務:座位是否滿了 & 航班是否取消了

方法一:Java異常

@Test
public void....
try{
    ...
}catch{
    ...
}

缺點:<逆向邏輯難理解:拋出異常測試就會通過,沒拋出異常測試失敗>

方法二:利用@Test annotation

@Test(expectedException = planeFullException.class)
public void shouldThrowIfPlaneIsFull{
    Plane plane = new Plane();
	plane.bookAllSeats();
	plane.bookPlane(createValidItinerary(),null);
}

@Test(expectedException = FlightCancelException.class)
public void shouldThrowIfFlightIsCanceled(){
    Plane plane = new Plane();
	CancelFlight();
	plane.bookPlane(createValidItinerary(),null);
}

優點:
1.通過@Test annotation就可以知道模塊功能;
2.消除try/catch/fail,使其專注於業務邏輯。

缺點:違反了DRY原則。

方法三:利用Configuration annotation 和 @Test annotation

public class BookingTest{
private Plane plane;

//將planeFullException.class & FlightCancelException.class兩個類的共同部分放在@BeforeMethod下

@BeforeMethod
public void init(){
	Plane plane = new Plane();
}

@Test(expectedException = planeFullException.class)
public void shouldThrowIfPlaneIsFull{
	plane.bookAllSeats();
	plane.bookPlane(createValidItinerary(),null);
}

@Test(expectedException = FlightCancelException.class)
public void shouldThrowIfFlightIsCanceled(){
	CancelFlight();
	plane.bookPlane(createValidItinerary(),null);
}
}

優點:利用@BeforeMethod使得代碼整潔,模塊更專注業務邏輯

2.3 testng-failed.xml

Testng將測試失敗的method記錄下來,自動生成testng-failed.xml文件。因此Testng的執行方法如下:

$ java org.testng.TestNG testng.xml
$ java org.testng.TestNG test-output/testng-failed.xml
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章