`

testng笔记2

 
阅读更多
1  各类@before,@after注解的例子:
    package test.beforeafter;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TestClass {
/**
* Before suite method which gets executed before
* starting of any of the test in the suite.
*/
@BeforeSuite
public void beforeSuite(){
System.out.println("Before Suite method");
}

/**
* After suite method which gets executed after
* execution of all the tests in a suite.
*/
@AfterSuite
public void afterSuite(){
System.out.println("After Suite method");
}

/**
* Before Test method which gets executed before
* each test mentioned inside the 'test' tag inside a test suite.
*/
@BeforeTest
public void beforeTest(){
System.out.println("Before Test method");
}

/**
* After Test method which gets executed after
* each test mentioned inside the 'test' tag inside a test suite.
*/
@AfterTest
public void afterTest(){
System.out.println("After Test method");
}

/**
* Before Class method which gets executed before
* any of the test-methods inside a class.
*/
@BeforeClass
public void beforeClass(){
System.out.println("Before Class method");
}

/**
* After Class method which gets executed after
* all of the test-methods inside a class gets executed.
*/
@AfterClass
public void afterClass(){
System.out.println("After Class method");
}

/**
* Before group method gets executed before executing the tests
* belonging to the group as mentioned in the 'groups' attribute.
* The following method gets executed before execution of the test-method belonging to group "testOne".
*/
@BeforeGroups(groups={"testOne"})
public void beforeGroupOne(){
System.out.println("Before Group Test One method");
}

/**
* After group method gets executed after executing the tests
* belonging to the group as mentioned in the 'groups' attribute.
* The following method gets executed after execution of the test-methods belonging to group "testOne".
*/
@AfterGroups(groups={"testOne"})
public void afterGroupOne(){
System.out.println("After Group Test One method");
}

/**
* Before group method gets executed before executing the tests
* belonging to the group as mentioned in the 'groups' attribute.
* The following method gets executed before execution of the test-method belonging to group "testTwo".
*/
@BeforeGroups(groups={"testTwo"})
public void beforeGroupTwo(){
System.out.println("Before Group Test two method");
}

/**
* After group method gets executed after executing the tests
* belonging to the group as mentioned in the 'groups' attribute.
* The following method gets executed after execution of the test-methods belonging to group "testTwo".
*/
@AfterGroups(groups={"testTwo"})
public void afterGroupTwo(){
System.out.println("After Group Test two method");
}

/**
* Before method which gets executed before each test-method.
*/
@BeforeMethod
public void beforeMethod(){
System.out.println("Before Method");
}

/**
* After method which gets executed after each test-method.
*/
@AfterMethod
public void afterMethod(){
System.out.println("After Method");
}

/**
* Test-method which belongs to group "testOne".
*/
@Test(groups={"testOne"})
public void testOneMethod(){
System.out.println("Test one method");
}

/**
* Test-method which belongs to group "testTwo".
*/
@Test(groups={"testTwo"})
public void testTwoMethod(){
System.out.println("Test two method");
}
}

输出:
   
Before Suite method
Before Test method
Before Class method
Before Group Test One method
Before Method
Test one method
After Method
After Group Test One method
After Class method
After Test method
Before Test method
Before Class method
Before Group Test two method
Before Method
Test two method
After Method
After Group Test two method
After Class method
After Test method
After Suite method


2 对于注解,也是可以继承的,比如:
一个基类:
   public class BaseClass {
@BeforeClass
public void beforeBaseClass(){
System.out.println("Parent Before Class method");
}

@AfterClass
public void afterBaseClass(){
System.out.println("Parent After Class method");
}

@BeforeMethod
public void beforeBaseMethod(){
System.out.println("Parent Before method");
}

@AfterMethod
public void afterBaseMethod(){
System.out.println("Parent After method");
}



}

   一个子类继承:
       public class TestClass extends BaseClass{
@BeforeClass
public void beforeChildClass(){
System.out.println("Child Before Class method");
}

@AfterClass
public void afterChildClass(){
System.out.println("Child After Class method");
}

@BeforeMethod
public void beforeChildMethod(){
System.out.println("Child Before method");
}

@AfterMethod
public void afterChildMethod(){
System.out.println("Child After method");
}

@Test
public void testMethod(){
System.out.println("Test method under TestClass");
}
}
    输出:  E:\java资料2\TestNG Beginner's Guide\Codes-6009OS - Reviewed\Chapter 3\BeforeAfterExtend\testng.xml

Parent Before Class method
Child Before Class method
Parent Before method
Child Before method
Test method under TestClass
Child After method
Parent After method
Child After Class method
Parent After Class method


3  private等修饰的方法是不纳入测试的,即使所在类加了@Test

4 禁用某个测试
   
@Test(enabled=false)
public void testMethodTwo(){
System.out.println("Test method two.");
}

5 测试异常
   @Test(expectedExceptions={IOException.class})
public void exceptionTestOne() throws Exception{
throw new IOException();
}

@Test(expectedExceptions={IOException.class,NullPointerException.class})
public void exceptionTestTwo() throws Exception{
throw new Exception();
}


测试异常信息
     public class ExceptionMessageTest {
/**
* Verifies the exception message based on the exact error message thrown.
* @throws Exception
*/
@Test(expectedExceptions={IOException.class},expectedExceptionsMessageRegExp="Pass Message test")
public void exceptionMsgTestOne() throws Exception{
throw new IOException("Pass Message test");
}

/**
* Verifies the exception message using the regular exception.
* This test verifies that the exception message contains a test "Message" in it.
* @throws Exception
*/
@Test(expectedExceptions={IOException.class},expectedExceptionsMessageRegExp=".* Message .*")
public void exceptionMsgTestTwo() throws Exception{
throw new IOException("Pass Message test");
}

/**
* Verifies the exception message based on the exact error message thrown.
* This is to show that TestNg fails a test when the exception message does not match.S
* @throws Exception
*/
@Test(expectedExceptions={IOException.class},expectedExceptionsMessageRegExp="Pass Message test")
public void exceptionMsgTestThree() throws Exception{
throw new IOException("Fail Message test");
}
  输出:PASSED: exceptionMsgTestOne
PASSED: exceptionMsgTestTwo
FAILED: exceptionMsgTestThree

6 测试超时
   public class TimeSuite {
@Test
public void timeTestOne() throws InterruptedException{
Thread.sleep(1000);
System.out.println("Time test method one");
}

@Test
public void timeTestTwo() throws InterruptedException{
Thread.sleep(400);
System.out.println("Time test method two");
}
如果在testng.xml中可以定义整个suite中的方法不能超过 time-out指定的500微秒,如:
   <suite name="Time test Suite" time-out="500" verbose="1" >
  <test name="Timed Test" >
    <classes>
    <class name="test.timetest.TimeSuite" />
    </classes>
  </test>
</suite>

  也可以指定某个方法的限制时间,比如:
      @Test(timeOut=500)
public void timeTestOne() throws InterruptedException{
Thread.sleep(1000);
System.out.println("Time test method one");
}

7  输入参数
    @Parameters({ "suite-param", "test-three-param" })
@Test
public void prameterTestThree(String param, String paramTwo) {
System.out.println("Test three suite param is: " + param);
System.out.println("Test three test level param is: " + paramTwo);
}
     <test name="Parameter Test three">
<parameter name="suite-param" value="overiding suite parameter" />
<parameter name="test-three-param" value="test three parameter" />
<classes>
<class name="test.parameter.ParameterTest">
<methods>
<include name="prameterTestThree" />
</methods>
</class>
</classes>
</test>
8  可选参数
      public class OptionalTest {
@Parameters({"optional-value"})
@Test
public void optionTest(@Optional("optional value")String value){
System.out.println("This is: "+value);
}

<test name="Optional Test one">
<classes>
<class name="test.parameter.OptionalTest" />
</classes>
</test>
<test name="Optional Test two">
<parameter name="optional-value" value="passed from xml" />
<classes>
<class name="test.parameter.OptionalTest" />
</classes>
</test>
    由于在Optional Test one中,没在xml中找到parameter,所以使用@Optional中的参数了
     ,输出:
     This is: optional value
This is: passed from xml



9 dataprovider
    public class SameClassDataProvider {
@DataProvider(name = "data-provider")
public Object[][] dataProviderMethod() {
return new Object[][] { { "data one" }, { "data two" } };
}

@Test(dataProvider = "data-provider")
public void testMethod(String data) {
System.out.println("Data is: " + data);
}

  输出;Data is: data one
Data is: data two
    当然,dataprovider的类也是可以单独的,如:
        @Test(dataProvider = "data-provider",dataProviderClass=DataProviderClass.class)
public void testMethod(String data) {
System.out.println("Data is: " + data);
}
    
0
0
分享到:
评论

相关推荐

    TestNG笔记

    2. **面向对象和Java支持**:TestNG充分利用了Java的面向对象特性,允许更加复杂的测试结构。 3. **类测试支持**:不必为每个测试方法创建单独的测试类,TestNG支持默认情况下重用类实例。 4. **独立的编译时间与...

    TestNG中文手册学习笔记

    2. 面向对象:TestNG 支持使用 Java 和面向对象的方式来组织和执行测试。 3. 综合类测试:不需要为每个测试方法创建单独的测试类实例。 4. 运行时配置:测试代码与运行时配置分离,允许动态数据驱动测试。 5. 灵活的...

    TestNG学习笔记

    ### TestNG 学习笔记概览 #### 一、JUnit 的局限性与 TestNG 的优势 ##### JUnit 缺陷概述 - **最初的用途限制**:JUnit 最初被设计为一个仅适用于单元测试的框架,但随着时间的发展,其功能已扩展至支持多种类型...

    Selenium+Eclipse+Junit+TestNG自动化学习笔记

    ### Selenium+Eclipse+JUnit+TestNG自动化测试学习笔记 #### 一、环境搭建与配置 **1. 安装 JDK** - **版本**: JDK 1.7 - **下载地址**: ...

    【TestNG自动化测试框架】TestNG自动化测试框架入门到实战完整笔记

    TestNG自动测试框架是当今流行的自动化测试框架之一 它可以帮助自动化测试工程师把精力集中在编写和实施测试用例和测试脚本上,提升软件测试执行和回归测试效率 分成4大模块 第一模块java 基础知识:JDK安装以及环境...

    达内Java项目云笔记12天完整源码cloudnote_day12_all.zip

    2. **Spring框架**:作为Java企业级应用的主流框架,Spring在该项目中起到了核心作用。它包含了依赖注入(DI)、面向切面编程(AOP)、Spring MVC以及Spring Boot等模块,用于构建高效、可维护的后端服务。 3. **Spring...

    云笔记资源代码

    测试是任何软件项目的重要组成部分,可能会使用JUnit或TestNG进行单元测试,确保每个独立的代码模块都能正常工作。而Spring Boot自带的MockMVC可以用来进行模拟HTTP请求的集成测试。 在部署方面,项目可能使用...

    ssh学习笔记

    - **spring-test**:提供了对 JUnit 或 TestNG 进行集成测试的支持。 - **junit**:一个常用的单元测试框架。 ### 二、数据库访问 #### Spring JDBC 模块 - **spring-jdbc**:提供了对 JDBC 的封装,使得数据访问...

    java学习笔记 良格格

    ### Java学习笔记要点 #### 一、了解Java ##### 1.1 Java的起源与发展历程 - **起源**: Java 最初是由 Sun 公司在 Green Project 中为了开发 Star7 应用程序而创建的一种编程语言。 - **命名**: 语言的名字来源于 ...

    java体系笔记

    Eclipse、IntelliJ IDEA等IDE为Java开发提供了强大的集成环境,Maven或Gradle用于项目构建和依赖管理,Junit、TestNG等支持单元测试。 本Java体系笔记覆盖了从基础知识到高级特性的全面内容,帮助读者建立扎实的...

    Selenium WebDriver的笔记整理

    - 创建TestNG测试类:在Package中创建一个TestNG测试类,使用注解`@BeforeMethod`和`@AfterMethod`来定义测试前后的操作。 **编写测试代码** 1. 引入必要的类库,如`org.openqa.selenium.WebDriver`,`org.openqa....

    软件测试内部教程笔记

    2. **测试生命周期**(V模型、W模型、H模型):笔记会讲解测试过程的各个阶段,如需求分析、设计、实施和评估,以及如何与开发周期对应,例如V模型的验证和确认过程。 3. **测试策略**:包括黑盒测试与白盒测试的...

    Java+JDK+6学习笔记.pdf

    - **测试工具**:JUnit、TestNG 等工具用于进行单元测试和集成测试。 - **项目库**:Maven Central Repository 等仓库提供大量的 Java 项目依赖。 - **论坛与文档**:Stack Overflow、Oracle Java 文档等资源对于...

    Java+JDK6学习笔记(PDF版书籍,免费下载)

    ### Java+JDK6学习笔记知识点详解 #### 一、Java简介 - **起源与历史:** - 最初由Sun公司的Green Project发起,旨在创建一个名为Star7的应用程序编程语言。 - 名称来源于创始人James Gosling窗外的一棵橡树...

    Maven 2 学习笔记.txt

    ### Maven 2 学习笔记 #### 一、Maven 项目的基本结构及创建 Maven使用特定的目录结构来组织项目文件,通过`mvn archetype:create`命令可以快速创建项目模板。例如: ```shell mvn archetype:create -DgroupId=...

    web笔记两连发

    2. **JavaBean**:从压缩包中的文件名"317_JavaBean.pdf"来看,JavaBean是本笔记的重点。JavaBean是一种遵循特定规范的Java类,常用于构建可重用的组件,便于在Web应用中进行数据封装和传输。它们通常是公开的、无...

    Selenium学习笔记源代码

    2. **元素定位**:Selenium提供了多种方法来定位网页上的元素,如ID、Name、XPath、CSS选择器等。理解这些定位策略的差异和应用场景至关重要。 3. **元素操作**:一旦找到元素,你可以进行点击、输入、检查属性等一...

    Spring学习总结笔记

    - **Test**:支持测试Spring应用,包括JUnit和TestNG集成。 2. **Spring框架的搭建** 搭建Spring环境首先需要引入必要的依赖库,例如:aopalliance、commons-logging、spring-aop、spring-beans、spring-context...

    HSAE自动化工具,随手笔记

    2. 测试框架:如Selenium用于网页自动化测试,Appium用于移动应用测试,它们提供了丰富的API来模拟用户交互,验证功能正确性。 3. 数据驱动测试:通过外部数据文件(如CSV或Excel)提供测试输入,使测试用例更灵活...

    IDEA快速使用入门笔记.zip

    2. **创建新项目**: IDEA支持多种类型的项目创建,包括Java项目、Maven项目、Spring Boot项目等。在"File" -&gt; "New" -&gt; "Project"中选择相应的模板,按照向导填写相关信息即可。 3. **编写Java代码**: IDEA强大...

Global site tag (gtag.js) - Google Analytics