测试之TestNG基础
2018-06-20 10:53
148 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_40919671/article/details/80742727
TestNG是一个开源的自动化测试框架。Junit最初的设计是用于单元测试的,TestNG这个测试框架,灵感来源于Junit&Nunit,但是它还引入了很多新的功能。TestNG的特点:1·注解2·java,面向对象3·支持综合类测试4·支持多线程。。。
注解是强类型的,所以编译器会标记任何的错误
import org.testng.annotations.Test;import org.testng.annotations.BeforeMethod;import org.testng.annotations.AfterMethod;import org.testng.annotations.BeforeClass;import org.testng.annotations.AfterClass;import org.testng.annotations.BeforeTest;import org.testng.annotations.AfterTest;import org.testng.annotations.BeforeSuite;import org.testng.annotations.AfterSuite;public class TestngAnnotation {// test case 1 @Test public void testCase1() { System.out.println("in test case 1"); } // test case 2 @Test public void testCase2() { System.out.println("in test case 2"); } @BeforeMethod public void beforeMethod() { System.out.println("in beforeMethod"); } @AfterMethod public void afterMethod() { System.out.println("in afterMethod"); } @BeforeClass public void beforeClass() { System.out.println("in beforeClass"); } @AfterClass public void afterClass() { System.out.println("in afterClass"); } @BeforeTest public void beforeTest() { System.out.println("in beforeTest"); } @AfterTest public void afterTest() { System.out.println("in afterTest"); } @BeforeSuite public void beforeSuite() { System.out.println("in beforeSuite"); } @AfterSuite public void afterSuite() { System.out.println("in afterSuite"); }}输出的顺序为
in beforeSuite
in beforeTest
in beforeClass
in beforeMethod
in test case 1
in afterMethod
in beforeMethod
in test case 2
in afterMethod
in afterClass
in afterTest
in afterSuite
===============================================
Suite
Total tests run: 2, Failures: 0, Skips: 0
===========================================
所以可以看到的是beforesuite()只执行一次,beforeTest(),beforeclass()只执行一次而beforemethod()方法每个测试用例之前都行执行。 阅读更多
测试和编写业务逻辑,在代码中插入TestNG的注解.
添加一个testng.xml文件或build.xml中在测试信息(例如类名,您想要运行的组,等..)
运行 TestNG.
注解 | 描述 |
---|---|
@BeforeSuite | 注解的方法将只运行一次,运行所有测试前此套件中。 |
@AfterSuite | 注解的方法将只运行一次此套件中的所有测试都运行之后。 |
@BeforeClass | 注解的方法将只运行一次先行先试在当前类中的方法调用。 |
@AfterClass | 注解的方法将只运行一次后已经运行在当前类中的所有测试方法。 |
@BeforeTest | 注解的方法将被运行之前的任何测试方法属于内部类的 <test>标签的运行。 |
@AfterTest | 注解的方法将被运行后,所有的测试方法,属于内部类的<test>标签的运行。 |
@BeforeGroups | 组的列表,这种配置方法将之前运行。此方法是保证在运行属于任何这些组第一个测试方法,该方法被调用。 |
@AfterGroups | 组的名单,这种配置方法后,将运行。此方法是保证运行后不久,最后的测试方法,该方法属于任何这些组被调用。 |
@BeforeMethod | 注解的方法将每个测试方法之前运行。 |
@AfterMethod | 被注释的方法将被运行后,每个测试方法。 |
@DataProvider | 标志着一个方法,提供数据的一个测试方法。注解的方法必须返回一个Object[] [],其中每个对象[]的测试方法的参数列表中可以分配。该@Test 方法,希望从这个DataProvider的接收数据,需要使用一个dataProvider名称等于这个注解的名字。 |
@Factory | 作为一个工厂,返回TestNG的测试类的对象将被用于标记的方法。该方法必须返回Object[]。 |
@Listeners | 定义一个测试类的监听器。 |
@Parameters | 介绍如何将参数传递给@Test方法。 |
@Test | 标记一个类或方法作为测试的一部分。 |
import org.testng.annotations.Test;import org.testng.annotations.BeforeMethod;import org.testng.annotations.AfterMethod;import org.testng.annotations.BeforeClass;import org.testng.annotations.AfterClass;import org.testng.annotations.BeforeTest;import org.testng.annotations.AfterTest;import org.testng.annotations.BeforeSuite;import org.testng.annotations.AfterSuite;public class TestngAnnotation {// test case 1 @Test public void testCase1() { System.out.println("in test case 1"); } // test case 2 @Test public void testCase2() { System.out.println("in test case 2"); } @BeforeMethod public void beforeMethod() { System.out.println("in beforeMethod"); } @AfterMethod public void afterMethod() { System.out.println("in afterMethod"); } @BeforeClass public void beforeClass() { System.out.println("in beforeClass"); } @AfterClass public void afterClass() { System.out.println("in afterClass"); } @BeforeTest public void beforeTest() { System.out.println("in beforeTest"); } @AfterTest public void afterTest() { System.out.println("in afterTest"); } @BeforeSuite public void beforeSuite() { System.out.println("in beforeSuite"); } @AfterSuite public void afterSuite() { System.out.println("in afterSuite"); }}输出的顺序为
in beforeSuite
in beforeTest
in beforeClass
in beforeMethod
in test case 1
in afterMethod
in beforeMethod
in test case 2
in afterMethod
in afterClass
in afterTest
in afterSuite
===============================================
Suite
Total tests run: 2, Failures: 0, Skips: 0
===========================================
所以可以看到的是beforesuite()只执行一次,beforeTest(),beforeclass()只执行一次而beforemethod()方法每个测试用例之前都行执行。 阅读更多
相关文章推荐
- selenium 自动化测试工具基础知识
- testng入门教程6 TestNG忽略测试
- 软件测试基础
- vSphere 5 HA 基础功能测试
- (转)接口自动化测试 – Java+TestNG 测试 Restful Web Service
- TestNG 学习总结 - 测试结果报告 - 自定义日志记录(十五)
- 软件测试策略基础
- CSS3基础知识小测验上线,大家有兴趣可以测试一下!
- APP测试入门篇之APP基础知识(001)
- 转载:测试基础
- C语言深度解剖读书笔记之——C语言基础测试题
- 测试基础----测试用例-等价类划分
- BTS基础测试服务
- 软件测试基础知识 day 3
- JSP学习的基础工具之四(续)—SQLyog的连接测试与表操作
- 软件测试面试必备的一些基础理论概念
- python基础测试题
- 软件测试基础知识二——编写测试用例的方法
- 关于java基础--File类与FileFilter过滤器的详解与测试
- 测试回顾版-第二讲:性能测试基础知识大温习