TestNG –依存関係テスト
TestNGでは、dependOnMethods
とdependsOnGroups
を使用して依存関係テストを実装します。 依存メソッドが失敗した場合、後続のすべてのテストメソッドはスキップされ、失敗しません。
1. dependOnMethodsの例
簡単な例「method2()」は「method1()」に依存しています。
1.1method1()
が渡されると、method2()
が実行されます。
App.java
package com.example.testng.examples.dependency; import org.testng.annotations.Test; public class App { @Test public void method1() { System.out.println("This is method 1"); } @Test(dependsOnMethods = { "method1" }) public void method2() { System.out.println("This is method 2"); } }
出力
This is method 1 This is method 2 PASSED: method1 PASSED: method2 =============================================== Default test Tests run: 2, Failures: 0, Skips: 0 ===============================================
1.2method1()
が失敗した場合、method2()
はスキップされます。
App.java
package com.example.testng.examples.dependency; import org.testng.annotations.Test; public class App { //This test will be failed. @Test public void method1() { System.out.println("This is method 1"); throw new RuntimeException(); } @Test(dependsOnMethods = { "method1" }) public void method2() { System.out.println("This is method 2"); } }
出力
This is method 1 FAILED: method1 java.lang.RuntimeException at com.example.testng.examples.dependency.App.method1(App.java:10) //... SKIPPED: method2 =============================================== Default test Tests run: 2, Failures: 1, Skips: 1 ===============================================
2. dependOnGroupsの例
dependsOnMethods
とdependsOnGroups
の混合使用を示すために、いくつかのテストケースを作成しましょう。 自明のコメントを参照してください。
TestServer.java
package com.example.testng.examples.dependency; import org.testng.annotations.Test; //all methods of this class are belong to "deploy" group. @Test(groups="deploy") public class TestServer { @Test public void deployServer() { System.out.println("Deploying Server..."); } //Run this if deployServer() is passed. @Test(dependsOnMethods="deployServer") public void deployBackUpServer() { System.out.println("Deploying Backup Server..."); } }
TestDatabase.java
package com.example.testng.examples.dependency; import org.testng.annotations.Test; public class TestDatabase { //belong to "db" group, //Run if all methods from "deploy" group are passed. @Test(groups="db", dependsOnGroups="deploy") public void initDB() { System.out.println("This is initDB()"); } //belong to "db" group, //Run if "initDB" method is passed. @Test(dependsOnMethods = { "initDB" }, groups="db") public void testConnection() { System.out.println("This is testConnection()"); } }
TestApp.java
package com.example.testng.examples.dependency; import org.testng.annotations.Test; public class TestApp { //Run if all methods from "deploy" and "db" groups are passed. @Test(dependsOnGroups={"deploy","db"}) public void method1() { System.out.println("This is method 1"); //throw new RuntimeException(); } //Run if method1() is passed. @Test(dependsOnMethods = { "method1" }) public void method2() { System.out.println("This is method 2"); } }
XMLファイルを作成し、一緒にテストします。
testng.xml
出力
Deploying Server... Deploying Backup Server... This is initDB() This is testConnection() This is method 1 This is method 2 =============================================== TestDependency Total tests run: 6, Failures: 0, Skips: 0 ===============================================