我目前正在尝试使用 API 将自动化测试与我们的测试管理工具集成。每个测试(包含在单个类中)都有一个唯一的 ID,需要在 API 调用中引用。我可以在测试本身中声明测试 ID(首选),也可以创建一个包含所有 ID 的单独类以供参考。我遇到的问题是想出一种在运行特定测试时将这些 ID 表示为变量的好方法,因此我不必为每个唯一 ID 重复 API 框架。
这是 API 类/侦听器类的一般设置。
package testPackage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;
import org.json.simple.JSONObject;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
import com.gurock.testrail.APIClient;
import com.gurock.testrail.APIException;
public class MyTestResultsListener extends TestListenerAdapter {
public final APIClient client = new APIClient("https://api.url/");
public final Map data = new HashMap();
public final void APISendCredentials() {
client.setUser("username");
client.setPassword("password");
}
@Override
public void onTestFailure(ITestResult result) {
APISendCredentials();
data.put("status_id", new Integer(5));
data.put("comment", "This test failed");
try {
JSONObject r = (JSONObject) client.sendPost("add_result_for_case/" + "Unique ID", data);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (APIException e) {
e.printStackTrace();
}
}
@Override
public void onTestSuccess(ITestResult result) {
APISendCredentials();
data.put("status_id", new Integer(1));
data.put("comment", "This test passed");
try {
JSONObject r = (JSONObject) client.sendPost("add_result_for_case/" + TestClass.AutomationID, data);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (APIException e) {
e.printStackTrace();
}
}
@Override
public void onTestSkipped(ITestResult result) {
APISendCredentials();
data.put("status_id", new Integer(2));
data.put("comment", "This test was blocked or skipped");
try {
JSONObject r = (JSONObject) client.sendPost("add_result_for_case/" + TestClass.AutomationID, data);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (APIException e) {
e.printStackTrace();
}
}
}
测试班
package testPackage;
import java.io.IOException;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import com.gurock.testrail.APIException;
@Listeners(MyTestResultsListener.class)
public class TestClass {
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface TestData {
String testId() default "0";
}
public static String AutomationID = "236/50819";
@Test
@TestData(testId = "236/50819")
public void test1() {
//sometest
}
@AfterTest
public void aftertest() {
//This was my initial blind attempt at reflection. I am getting
//an error below stating "test1" cannot be resolved to a variable
//also unclear on how to feed this to the APIclass
Method testMethod = getClass().getMethod(test1);
if (testMethod.isAnnotationPresent(TestData.class)) {
TestData testData = testMethod.getAnnotation(TestData.class);
//stuck at this point
}
}
}
正如您在开头的行中看到的,JSONobject r
我有一个 uniqueID 变量的占位符。什么是创建可以从不同测试类中获取这些 unqiue ID 的变量的最佳方式/最有效的方式?
Thx,让我知道是否需要任何其他有用的信息!