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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值