Jacek
Jacek

Reputation: 87

How to add Test Case into TFS via REST

I'm implementing lib to export/synchronize test cases into TFS automaticaly during test run. One of my requirements is that I need to use NodeJS for that, so I decided to use TFS REST API. In my code I'm using "azure-devops-node-api " lib, I can connect and get different elements, no luck with adding test cases.

I've found on the web that TestCase is kind of WorkItem and as WI should be added. Unfortunately I didn't find a way to add one with azure-devops-node-api.

I tried also to send manually constructed json, unfortunatelly no luck with finding proper url to sent as I'm allways getting :

Error: {"statusCode":404,"body":"Page not found."

My example request:

 request.post({
            url: 'https://<url>/tfs/<default collection maybe?>/<project>/_apis/wit/workItems/test%20case',
            headers: {
                'Content-Type': 'application/json',
                'Authorization':'Basic ' + this.encodePat('<my auth token>')
            },
            rejectUnauthorized: false,//add when working with https sites
            requestCert: false,//add when working with https sites
            agent: false,//add when working with https sites
            json: rq
        },function (response, err, body){

            if (err) throw new Error(JSON.stringify(err));

            console.log('Body:',JSON.parse(body));
            console.log('Response:',response)
        });  

Does anyone know what wrom I'm doing or if azure-devops-node-api is able to add WorkItems ?

Upvotes: 0

Views: 255

Answers (1)

Shamrai Aleksander
Shamrai Aleksander

Reputation: 16018

If you want to add a test case only just as a work item you have to use the template from this link Work Items - Create.

POST https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/${type}?api-version=5.0

You have to add to url "?api-version=X.0". The example:

https://<server name and port>/tfs/<collection name should be>/<project>/_apis/wit/workItems/$test%20case?api-version=3.0

Also you have to encode your pat by this template:

'Authorization':'Basic ' + this.encodePat(':<my auth token>')

Here you can find an example for a build task with node.js: https://github.com/ashamrai/AzureDevOpsExtensions/blob/master/CustomBuildTask/NewWICustomTask/index.ts

Upvotes: 2

Related Questions