Join 34,000+ subscribers and receive articles from our blog about software quality, testing, QA and security.
 

Please show java example how to add testplan

#1

Hi,

Testrail is great so far, very beautiful. Using add_milestone I’ve created a new milestone:

/**
 * Creates an example map which later is fed into addMilestone method.
 * */
 public void newMilestoneMap(){
        Map data = new HashMap();
        data.put("name", "New milestone added by API");
        data.put("description", "Description here");
        data.put("due_on", 1394596385);
    }

/**
 * Takes in a map and adds a new milestone. Hardcoded to project id 5 for now.
 * */
public void addMilestone(Map data) throws IOException, APIException {
    JSONObject r = (JSONObject) client.sendPost("add_milestone/2", data);
    System.out.println("kek: \n" + r);
}

How do you guys suggest to add a testplan? I thought to use an approach with pojo and builder. Create 2 classes like:

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Testplan {
	
	@JsonProperty("name")
	public String name;
	@JsonProperty("entries")
	public List<Entry> entries = null;
}

and this:

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Entry {
	
	@JsonProperty("suite_id")
	public Integer suiteId;
	@JsonProperty("name")
	public String name;
	@JsonProperty("include_all")
	public Boolean includeAll;
	@JsonProperty("case_ids")
	public List<Integer> caseIds = null;
}

Then probably creating a template request somehow and feeding it into sendPost().

And I’m stuck with it :slight_smile: If anyone has a working example, please share.

Here’s an example json I try to deserialize into pojo:

{
  "name": "System test",
  "entries": [
    {
      "suite_id": 1,
      "name": "Custom run name"
    },
    {
      "suite_id": 1,
      "include_all": false,
      "case_ids": [1,2,3,5]
    }
  ]
}

Thanks!

#2

Update:

Managed serialize the following json using java:

{
	"name": "System test",
	"entries": [
		{
			"suite_id": 1,
			"name": "Custom run name",
			"include_all": false,
			"case_ids": [
				1,
				2,
				3,
				4,
				5
			]
		}
	]
}

Here’s how: First you create a POJO (Classes used in the method are available in the post above)

public Testplan getNewTestPlanRequest() {
		Testplan testplan = Testplan.builder()
				.name("System test")
				.entries(Lists.newArrayList(Entry.builder()
						.name("Custom run name")
						.suiteId(1)
						.includeAll(false)
						.caseIds(new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5)))
						.build()))
				.build();
		System.out.println(testplan);
		return testplan;
	}

Then you serialize that POJO into JSON. I used this method:

protected <U> String toJson(U pojo) throws JsonProcessingException {
		ObjectMapper mapper = new ObjectMapper();
		return mapper.writeValueAsString(pojo);
	}

Hope this will help anyone who will face the same problem :slight_smile:

#3

A parametrized example:

	public Testplan getNewTestplanRequest(String testPlanName, String testRunName, Integer suiteId, List<Integer>caseIds) {
		Testplan testplan = Testplan.builder()
				.name(testPlanName)
				.entries(Lists.newArrayList(Entry.builder()
						.name(testRunName)
						.suiteId(suiteId)
//						.includeAll(false)
						.caseIds(caseIds)
						.build()))
				.build();
		System.out.println(testplan);
		return testplan;
	}

Haven’t figured a proper way to add a list of testcases, so for now it looks like this :smiley:

List<Integer> list = new ArrayList<>();
		list.add(1);
		list.add(2);
		list.add(3);
		list.add(4);
		list.add(5); 

And you create a new String which is later serialized into json:

		String json = toJson(getNewTestplanRequest("dis be test plan nam", "dis be test run nam", 1337, list));
#4

Using testrail api is a lot of pain. Figuring out how to pass or what’s wrong or what are the mandatory fields is pure agony.

#5

If I may, I suggest you check https://github.com/codepine/testrail-api-java-client for easier implementations.

We use the fork https://github.com/chapeco/testrail-api-java-client here.

#6

Hey, thank you. I found it, started using it. Testrail’s api client it’s … it’s just bad.

#7

You’re welcome, and yes, the official bindings are bad.
It’s kind of what you’d expect from Gurock actually, after using TestRail for a while now.