我目前正嘗試使用API將自動化測試與我們的測試管理工具集成。每個測試(包含在一個類中)都有一個唯一的ID,需要在API調用中引用。我可以在測試中聲明測試ID(首選),也可以創建一個包含所有ID的獨立類以供參考。我遇到的問題是,在運行特定測試時將這些ID表示爲變量的好方法,因此我不必爲每個唯一ID重複使用API框架。創建一個實例變量,它代表具有相同名稱但具有不同類中的唯一值的其他變量(Java)
以下是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,讓我知道是否有任何額外的信息需要幫助!
我不確定「重複API框架」是什麼意思,但這種事情的通常答案是創建一個接口,如「HasTestId」。 – chrylis
'onTestFailure'方法在哪裏?我的意思是,它宣佈哪一類?你有所有的測試超類嗎?請顯示您的測試的骨架。 –