Search code examples
pythontfsazure-devopstest-plan

Automatic creation of Test Plan in Azure TFS using python


I have a problem with automatic creation of Test Plans in Azure TFS, by POST method in python. When I'm creating it manually, in UI, everything works, and when I try to find this work item eg. http://xxx/tfs/test/Project/_apis/test/plans/266801?api-version=5.0 - everything is OK and I get JSON.

My code:

def TestPlan(title=None, description=None):
    def wrapper(func):
        def inner_wrapper(*args, **kwargs):
            authorization = str(base64.b64encode(bytes(':' + token, 'ascii')), 'ascii')
            headers = {
                'Content-Type': 'application/json-patch+json',
                'Authorization': 'Basic ' + authorization
            }
            URL = url + '/' + project + '/_apis/wit/workitems/$Test Plan?' + api_version
            body = [
                {
                    "op": "add",
                    "path": "/fields/System.Title",
                    "value": title
                },
                {
                    "path": "/fields/System.Description",
                    "op": "add",
                    "value": description
                },
                {
                    "path": "/fields/Microsoft.VSTS.TCM.AutomatedTestId",
                    "op": "add",
                    "value": automationId(func)
                }
            ]
            requests.post(URL, json=body, headers=headers)
            func(*args, **kwargs)
        return inner_wrapper
    return wrapper

When I'm using my code, work item is created, but only in UI. When I try to find it in API, a have an error: {"$id":"1","innerException":null,"message":"Test plan 266800 not found.","typeName":"Microsoft.TeamFoundation.TestManagement.WebApi.TestObjectNotFoundException, Microsoft.TeamFoundation.TestManagement.WebApi","typeKey":"TestObjectNotFoundException","errorCode":0,"eventId":3000} When I click in this work item in UI, I have this: azure test plan error I think I need to add a rootSuite durring creation of Test Plan, but how to do that? Could you help me?


Solution

  • Why do you use the work item API to create a test plan (_apis/wit/workitems)?

    You have to use the test plan API (_apis/testplan/plans). Here are examples: Test Plans - Create