创建一个实例变量,表示具有相同名称但不同 类 (Java) 中唯一值的其他变量
Creating an instance variable that represents other variables with same names but unique values in different classes (Java)
我目前正在尝试使用 APIs 将自动化测试与我们的测试管理工具集成。每个测试(包含在单个 class 中)都有一个唯一的 ID,需要在 API 调用中引用。我可以在测试本身中声明测试 ID(首选)或创建一个包含所有 ID 的单独 class 以供参考。我遇到的问题是在特定测试为 运行 时想出一种将这些 ID 表示为变量的好方法,因此我不必为每个唯一 ID 重复 API 框架。
这是 API class/listener class.
的一般设置
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();
}
}
}
测试class
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 变量的占位符。什么是最好的 way/most 创建变量的有效方法,该变量可以从不同的测试 classes 中获取这些独特的 ID?
谢谢,如果需要任何其他有用的信息,请告诉我!
如果每个测试的 id 都没有改变,您可以创建一个注释,例如@TestId
持有测试id。每个测试 class(或取决于所需粒度级别的方法)都可以用自己的测试 ID 进行注释。然后在 API 代码中你可以通过反射简单地获取它。
我目前正在尝试使用 APIs 将自动化测试与我们的测试管理工具集成。每个测试(包含在单个 class 中)都有一个唯一的 ID,需要在 API 调用中引用。我可以在测试本身中声明测试 ID(首选)或创建一个包含所有 ID 的单独 class 以供参考。我遇到的问题是在特定测试为 运行 时想出一种将这些 ID 表示为变量的好方法,因此我不必为每个唯一 ID 重复 API 框架。
这是 API class/listener class.
的一般设置 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();
}
}
}
测试class
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 变量的占位符。什么是最好的 way/most 创建变量的有效方法,该变量可以从不同的测试 classes 中获取这些独特的 ID?
谢谢,如果需要任何其他有用的信息,请告诉我!
如果每个测试的 id 都没有改变,您可以创建一个注释,例如@TestId
持有测试id。每个测试 class(或取决于所需粒度级别的方法)都可以用自己的测试 ID 进行注释。然后在 API 代码中你可以通过反射简单地获取它。