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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章