TestNG - Тест на зависимость
В TestNG мы используемdependOnMethods иdependsOnGroups для реализации тестирования зависимостей. Если зависимый метод завершился ошибкой, все последующие методы тестирования будут пропущены, НЕ завершились ошибкой.
1. Пример зависимости от метода зависимости
Простой пример: «method2 ()» зависит от «method1 ()».
1.1 Еслиmethod1() передан, будет выполнен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.2 Еслиmethod1() не удалось,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. зависимости от примера
Давайте создадим несколько тестовых примеров, чтобы продемонстрировать смешанное использование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 ===============================================
