TestNGのは、フレームワークをテストオープンソースであり、主に以下の特性が、TestNGのは、次の世代をテストすることを意図し、JUnitのから継承されています。
- このよう@Testとしてノート注釈、@BeforeMethod
- マルチスレッド実行ケースをサポートしています
- サポートデータ駆動型のdataProvider
- サポート参照パラメータ
- これは、プラグイン食として使用することができます
- (reportngとする)ことができる、客観テストレポートを生成します
- ケース管理の実施とスイートtestng.xmlを通じて、
良いテストフレームワークので、どのように使用するには?
ここでは、Eclipseプラグインを参照してくださいをインストールし使用します。http://testng.org/doc/eclipse.html
TestNGの使用
まず、注釈TestNGのを見て
共通するのは、次のとおりです。
@BeforeClass:注釈は、活性化前クラスに行います
@BeforeMethod:この注釈は、メソッドの各実行前に実行されます
@Testアノテーションを使用して、テストを実行する方法です。
@AfterMethod、各テストメソッドの実行後の注釈の実行
@AfterClass注釈は、すべてのテストメソッドの後に実行されます
詳細なライフサイクルは次のとおりです。
ここではすべての注釈です
@BeforeSuite @AfterSuite @BeforeTest @AfterTest @BeforeGroups @AfterGroups @BeforeClass @AfterClass @BeforeMethod @AfterMethod |
TestNGのクラスの構成情報: @BeforeSuite: アノテーション付きメソッドこのスイートのすべてのテストを実行する前に実行されます。 @AfterSuite: このスイートのすべてのテストが実行された後にアノテーション付きメソッドが実行されます。 @BeforeTest:<テスト>タグ内のクラスに属するすべてのテストメソッドが実行される前に、アノテーション付きメソッドが実行されます。 @AfterTest:<テスト>タグ内のクラスに属するすべてのテストメソッドが実行した後に、アノテーション付きメソッドが実行されます。 @BeforeGroups:この設定方法は、前に実行されますグループのリスト。これらのグループのいずれかに属している最初のテストメソッドが呼び出される前に、このメソッドはすぐに実行することが保証されています。 @AfterGroups:この設定方法は、後に実行されますグループのリスト。この方法は、これらのグループのいずれかに属している最後のテストメソッドが呼び出された直後に実行することが保証されています。 @BeforeClass:現在のクラスの最初のテストメソッドが呼び出される前に、アノテーション付きメソッドが実行されます。 @AfterClass:注釈付きメソッドは、現在のクラス内のすべてのテストメソッドが実行された後に実行されます。 @BeforeMethod:注釈付きメソッドは、各テストメソッドの前に実行されます。 @AfterMethod:注釈付きメソッドは、各テストメソッドの後に実行されます。 |
例:私たちは、この場合、2つのテストがあり、TestNGの注釈実行順序を確認する実行順序beforeClass-> beforeMethod-> test1-> afterMethod-> beforeMethod->
test2-> afterMethod-> afterClass。
パッケージcom.dbyl.tests。
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; /** * This is to verify testng annotation execute * @author Young * */ public class TestngExample { private int a; @BeforeMethod(alwaysRun=true) public void beforeMethod() { a = 2; System.out.println("This is beforeMethod method. The Value of a is: " + a); } @BeforeClass public void beforeClass() { a = 1; System.out.println("This is beforeClass method .The Value of a is: " + a); } @Test(groups = "TestngExample") public void testExample1() { a = 3; System.out.println("This is Test method1 .The Value of a is: " + a); } @Test(groups = "TestngExample") public void testExample2() { a = 4; System.out.println("This is Test method2 .The Value of a is: " + a); } @AfterClass public void afterClass() { a = 5; System.out.println("This is AfterClass Method .The Value of a is: " + a); } @AfterMethod public void afterMethod() { a = 6; System.out.println("This is AfterMethod Method .The Value of a is: " + a); } }
所以执行结果为:
1
2
3
4
5
6
7
8
9
10
|
This is beforeClass method .The Value of a is: 1
This is beforeMethod method. The Value of a is: 2
This is Test method1 .The Value of a is: 3
This is AfterMethod Method .The Value of a is: 6
This is beforeMethod method. The Value of a is: 2
This is Test method2 .The Value of a is: 4
This is AfterMethod Method .The Value of a is: 6
This is AfterClass Method .The Value of a is: 5
PASSED: testExample1
PASSED: testExample2
|
当然,还有BeforeSuite 等,不再做深入研究.
annotation后面可以加一些参数,比如alwaysRun=true/false,dependsOnMethods=""
alwaysRun控制是否每次都执行,dependsOnMethods是一种依赖,依赖某个方法执行
之前有提到testng数据驱动,使用dataProvider,dataProvider可以导入text ,excel等数据,
执行case时会从DataProvider依次拿出数据执行,同一个测试方法,会被执行相应的次数
package com.dbyl.tests;
import org.testng.Assert;
import org.testng.annotations.AfterClass; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class Case1 { @DataProvider public Object[][] testData1() { return new Object[][] { { 1, 2, 3 }, { 1, 2, 4 }, { 1, 3, 4 }, { -1, 3, 2 } }; } @DataProvider public Object[][] testData2() { return new Object[][] { { 5, 2, 3 }, { 1, 2, 4 }, { 1, -3, 4 }, { 6, 3, 2 } }; } public static int add(int a, int b) { return a + b; } public static int minus(int a, int b) { return a - b; } @BeforeClass public void beforeClass() { System.out.println("This is Before Class"); } @Test(groups = { "add" }, dataProvider = "testData1") public void addTest(int a, int b, int c) { System.out.println("This is test add method. "+a+" + "+ b+" = "+c); Assert.assertEquals(add(a, b), c); } @Test(groups = { "minus" }, dataProvider = "testData2") public void minusTest(int a, int b, int c) { System.out.println("This is test minus method. "+a+" - "+ b+" = "+c); Assert.assertEquals(minus(a, b), c); } @BeforeMethod public void beforeMethod() { System.out.println("This is Before Method"); } @AfterMethod public void afterMethod() { System.out.println("This is After Method"); } @AfterClass public void afterClass() { System.out.println("This is After Class"); } }
执行结果如下:
This is Before Class
This is Before Method
This is test add method. 1 + 2 = 3
This is After Method
This is Before Method
This is test add method. 1 + 2 = 4 This is After Method This is Before Method This is test add method. 1 + 3 = 4 This is After Method This is Before Method This is test add method. -1 + 3 = 2 This is After Method This is Before Method This is test minus method. 5 - 2 = 3 This is After Method This is Before Method This is test minus method. 1 - 2 = 4 This is After Method This is Before Method This is test minus method. 1 - -3 = 4 This is After Method This is Before Method This is test minus method. 6 - 3 = 2 This is After Method This is After Class PASSED: addTest(1, 2, 3) PASSED: addTest(1, 3, 4) PASSED: addTest(-1, 3, 2) PASSED: minusTest(5, 2, 3) PASSED: minusTest(1, -3, 4) FAILED: addTest(1, 2, 4)
由于天色已晚,明天继续总结.
接下来:testng.xml的配置和使用,已经参数传递
testng配合reportng 生成测试报告
testng 配合ant
转载请注明出处:http://www.cnblogs.com/tobecrazy/