with rpc_client: how to find (filter) all TestCases for a (one) TestPlan?

156 Views Asked by At

Could somebody write an example of how to query (filter) all testcases for a testplan?

I can't find the relation between the testcases and testplan in rpc.

from tcms_api import TCMS

rpc_client = TCMS()

for test_case in rpc_client.exec.TestCase.filter({'pk': 1}):
    print(test_case)
3

There are 3 best solutions below

0
On

If you would like to use REST API you can run this:

curl --request POST \
  --url https://kiwi-test.ulakhaberlesme.com.tr/json-rpc/ \
  --header 'Content-Type: application/json' \
  --header 'Cookie: sessionid=aanwmhos1ki3x0ofaq5er8n5xajwnmeg' \
  --cookie csrftoken=S27Ijt6MWT5vpWkHo7iAlM2ELzkGc9st \
  --data '{
    "jsonrpc":"2.0",
    "method":"TestCase.filter",
    "params":[
    {
      "plan__id": 3
    }],
    "id":"jsonrpc"
}'

Or if you want test cases for multiple test plans:

import http.client

conn = http.client.HTTPSConnection("kiwi-test.ulakhaberlesme.com.tr")

payload = "{\n\t\"jsonrpc\":\"2.0\",\n\t\"method\":\"TestCase.filter\",\n\t\"params\":[\n    {\n      \"plan__id__in\": [3,4]\n    }],\n\t\"id\":\"jsonrpc\"\n}"

headers = {
    'cookie': "csrftoken=S27Ijt6MWT5vpWkHo7iAlM2ELzkGc9st",
    'Content-Type': "application/json",
    'Cookie': "sessionid=aanwmhos1ki3x0ofaq5er8n5xajwnmeg"
    }

conn.request("POST", "/json-rpc/", payload, headers)

res = conn.getresponse()
data = res.read()

print(data.decode("utf-8"))

enter image description here

0
On

I can't find the relation between the testcases and testplan in rpc.

Because the relationship between them is not in the RPC layer but on the ORM(database) layer. The API being a thin wrapper around ORM queries doesn't explicitly document all of the fields/lookups because that's been documented in Django and by the source code itself: https://kiwitcms.readthedocs.io/en/latest/modules/tcms.rpc.api.html#how-does-the-rpc-interface-work

The UI is also a stand alone API client and you can explore it for hints as well.

Fetching all TestPlans for a given TestCase:

TestPlan.filter({"cases":10260})

Fetching all TestCases for a given TestPlan:

TestCase.filter({"plan": 1234})

0
On

I found this solution.

from tcms_api import TCMS
import json 

rpc_client = TCMS()

print(" *** Plans & cases *** ")

for test_plan in rpc_client.exec.TestPlan.filter():
    print(test_plan['id'], "[", test_plan['name'], "]",test_plan['type__name'],
    test_plan['product__name'],
    test_plan['product_version__value'])

    for test_case in rpc_client.exec.TestCase.filter({"plan":test_plan['id']}):
      print("  ", test_case['id'], "[",test_case['summary'],"]",test_case['case_status__name'])

print(" *** Run & executions *** ")

for run_plan in rpc_client.exec.TestRun.filter({}):
    print(run_plan['id'],run_plan['summary'],run_plan['plan__name'])
    for exec_plan in rpc_client.exec.TestExecution.filter({"run_id":run_plan['id']}):
        print("  ", exec_plan['id'], exec_plan['case__summary'], exec_plan['status__name'])