2023拉

TestNG执行程序

上一篇 / 下一篇  2014-06-23 15:37:24 / 个人分类:Selenium测试

   

本教程介绍了TestNG中执行程序的方法,这意味着该方法被称为第一和一个接着。下面是执行程序的TestNG测试API的方法的例子。

创建一个Java类文件名TestngAnnotation.java在C:\>TestNG_WORKSPACE测试注解。

importorg.testng.annotations.Test;importorg.testng.annotations.BeforeMethod;importorg.testng.annotations.AfterMethod;importorg.testng.annotations.BeforeClass;importorg.testng.annotations.AfterClass;importorg.testng.annotations.BeforeTest;importorg.testng.annotations.AfterTest;importorg.testng.annotations.BeforeSuite;importorg.testng.annotations.AfterSuite;publicclassTestngAnnotation{// test case 1@TestpublicvoidtestCase1(){System.out.println("in test case 1");}// test case 2@TestpublicvoidtestCase2(){System.out.println("in test case 2");}@BeforeMethodpublicvoidbeforeMethod(){System.out.println("in beforeMethod");}@AfterMethodpublicvoidafterMethod(){System.out.println("in afterMethod");}@BeforeClasspublicvoidbeforeClass(){System.out.println("in beforeClass");}@AfterClasspublicvoidafterClass(){System.out.println("in afterClass");}@BeforeTestpublicvoidbeforeTest(){System.out.println("in beforeTest");}@AfterTestpublicvoidafterTest(){System.out.println("in afterTest");}@BeforeSuitepublicvoidbeforeSuite(){System.out.println("in beforeSuite");}@AfterSuitepublicvoidafterSuite(){System.out.println("in afterSuite");}}

接下来,让我们创建的文件 testng.xml 在 C:\ > TestNG_WORKSPACE 执行注解。

<?xml version="1.0"encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" ><suitename="Suite1"><testname="test1"><classes><classname="TestngAnnotation"/></classes></test></suite>

编译使用javac测试用例类。

C:\TestNG_WORKSPACE>javac TestngAnnotation.java

现在运行testng.xml,将运行提供的测试用例类中定义的测试用例

C:\TestNG_WORKSPACE>java org.testng.TestNG testng.xml

验证输出。

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
===============================================

见上面的输出,TestNG是执行过程如下:

  • 首先所有beforeSuite()方法只执行一次。

  • 最后,afterSuite的()方法只执行一次。

  • 即使方法 beforeTest(), beforeClass(), afterClass() 和afterTest() 方法只执行一次。

  • beforeMethod()方法执行每个测试用例,但在此之前执行的测试用例。

  • afterMethod()方法执行每个测试用例,但测试用例执行后。

  • In between beforeMethod() and afterMethod() each test case executes.


TAG:

 

评分:0

我来说两句

Open Toolbar