【问题标题】:How to retrieve Test Cases associated with the Test Set如何检索与测试集关联的测试用例
【发布时间】:2013-11-12 16:42:18
【问题描述】:

我尝试为jar 2.0.2 和现在为2.0.4 运行提供的代码,两次都在以下行收到错误:int numberOfTestCases = testSetJsonObject.get("TestCases").getAsJsonArray().size();

Exception in thread "main" java.lang.IllegalStateException: This is not a JSON Array. at com.google.gson.JsonElement.getAsJsonArray(JsonElement.java:106) at GetTCofTS.main(GetTCofTS.java:50)

我可以克服这个错误(因为它实际上不是一个返回的数组),但它不能解决我的问题,我不仅试图获取与当前 TestSet 关联的 TestCases 的数量,而且测试用例列表 - 至少是它们的格式化 ID。它是拉力赛的包吗?

public class GetTCofTS {

public static void main(String[] args) throws Exception {

    String host = "https://rally1.rallydev.com";

    String username = "user@co.com";
    String password = "secret";

    String applicationName = "RESTExampleFindTestCasesOfTestSet";
    String workspaceRef = "/workspace/1111";
    String projectRef = "/project/2222";
    String wsapiVersion = "1.43";


    RallyRestApi restApi = null;
    try {
        restApi = new RallyRestApi(
                new URI(host),
                username,
                password);
        restApi.setApplicationName(applicationName); 

        QueryRequest testSetRequest = new QueryRequest("TestSet");
        testSetRequest.setWorkspace(workspaceRef);
        restApi.setWsapiVersion(wsapiVersion);
        testSetRequest.setFetch(new Fetch(new String[] {"Name", "TestCases", "FormattedID"}));

        testSetRequest.setQueryFilter(new QueryFilter("Name", "=", "someTS"));

        QueryResponse testSetQueryResponse = restApi.query(testSetRequest);
        System.out.println("Successful: " + testSetQueryResponse.wasSuccessful());
        System.out.println("Size: " + testSetQueryResponse.getTotalResultCount());
        for (int i=0; i<testSetQueryResponse.getResults().size();i++){
            JsonObject testSetJsonObject = testSetQueryResponse.getResults().get(i).getAsJsonObject();
            System.out.println("Name: " + testSetJsonObject.get("Name") + " ref: " + testSetJsonObject.get("_ref").getAsString() + " Test Cases: " + testSetJsonObject.get("TestCases"));
            int numberOfTestCases = testSetJsonObject.get("TestCases").getAsJsonArray().size();
            System.out.println(numberOfTestCases);
            if(numberOfTestCases>0){
                  for (int j=0;j<numberOfTestCases;j++){
                  System.out.println(testSetJsonObject.get("TestCases").getAsJsonArray().get(j).getAsJsonObject().get("FormattedID"));
                 }
            }

        }

    } finally {
        if (restApi != null) {
            restApi.close();
        }
    }
}
}

【问题讨论】:

  • 只要 api 版本是 1.43,这应该可以工作。在 v2.0 中,集合不会在结果中返回,而是在对象中返回计数和集合 url。难道是wsapi版本设置不正确?我运行了您的代码,没有收到任何错误...
  • 好吧,在 Rally 提供的示例中,他们建议使用 api 版本“v2.0”如果我将另一段代码更改为使用“1.43”,则它不起作用。但是这个特定的类确实适用于 1.43 并带来了测试用例编号。不应该在所有代码中都相同吗?
  • 我们找到解决方案了吗?

标签: java rally


【解决方案1】:

v2.0 是未来的首选版本。下面的小调整应该能让你振作起来。

而不是这个(在 1.43 中有效,因为返回了集合):

int numberOfTestCases = testSetJsonObject.get("TestCases").getAsJsonArray().size();
if(numberOfTestCases>0) {
    for (int j=0;j<numberOfTestCases;j++) {
       System.out.println(testSetJsonObject.get("TestCases").getAsJsonArray().get(j).getAsJsonObject().get("FormattedID"));
    }
}

这样做:

//TestCases is an object with a Count and a ref to load the collection
int numberOfTestCases = testSetJsonObject.getAsJsonObject("TestCases").get("Count").getAsInt();

if(numberOfTestCases > 0) {
    QueryRequest testCaseRequest = new QueryRequest(testSetJsonObject.getAsJsonObject("TestCases"));
    testCaseRequest.setFetch(new Fetch("FormattedID"));
    //load the collection
    JsonArray testCases = restApi.query(testCaseRequest).getResults();
    for (int j=0;j<numberOfTestCases;j++){
        System.out.println(testCases.get(j).getAsJsonObject().get("FormattedID").getAsString());
    }
}

【讨论】:

  • 我刚刚意识到返回的测试用例集合的顺序错误。在我的情况下,我需要在 TestSet 中保留 TestCases 的顺序。我看到“DragAndDropRank”字段,但它完全不可读,就像这样。 “P!”zG~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~" 那么如何按照网页上显示的顺序返回TestCases集合呢?
  • 试试 testCaseRequest.setOrder('DragAndDropRank ASC');方向也可能是 DESC - 不确定。
  • 我从一开始就尝试了这个,但是回来的订单又回到了格式化 ID。我怀疑这是一个错误。我们的 QA 自动化严重依赖这个订单,所以我现在不知道如何进行。我有屏幕截图来显示 Rally 中的顺序和来自 Java 的顺序。
  • 您能否向 Rally Support 提交包含此信息的案例?我能够重现您的问题,这似乎是 WSAPI 中的一个有效缺陷。
猜你喜欢
  • 2017-11-18
  • 1970-01-01
  • 2016-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-28
相关资源
最近更新 更多