我目前正在尝试使用API​​将自动化测试与我们的测试管理工具集成在一起。每个测试(包含在一个类中)都有一个唯一的ID,该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变量有一个占位符。创建可以使用来自不同测试类的这些不稳定ID的变量的最佳方法/最有效的方法是什么?

谢谢,让我知道是否需要其他任何有用的信息!

最佳答案

如果每个测试的ID均未更改,则可以创建一个注释,例如保留测试ID的@TestId。每个测试类(或方法取决于所需的粒度级别)都可以使用其自己的测试ID进行注释。然后,在API代码中,您可以简单地通过反射来获取它。

09-11 18:33
查看更多