+ Start a Discussion
Diwakar G 7Diwakar G 7 

Conversion from Apex to Python


Please help me to convert the below code to Python. 
var xhttp = new XMLHttpRequest(); 
xhttp.open("POST", "http://instance.salesforce.com/services/data/v44.0/tooling/runTestsSynchronous/", true) 
xhttp.setRequestHeader("Authorization", "OAuth <SESSION_ID>") 
testObject = {tests: [{classId: "N0tARealClassId", testMethods: ["testMethod1", "testMethod2"]}]} 
requestObject = json.stringify(testObject); 
response = xhttp.send(requestObject) 
response = JSON.parse(response)

This code is used to run Apex test. Please help me.

Thanks and Regards,
Diwakar G
SandhyaSandhya (Salesforce Developers) 

You can try below sample code
import urllib2
from urllib2 import Request, urlopen
import json
values =[{"className":"TestVerifyDate","testMethods": ["testMethod1","testMethod2","testMethod3"]}]
values = json.dumps(values).encode('utf8') 
req = Request(your_url, data=values,headers = {'Accept':'application/vnd.rn+json','Content-Type':'application/vnd.rn+json','Authorization':'Token token="<hexstring>"'})
response_body = urlopen(req).read()
print response_body

Refe​​​​​​rence​ link.

Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
Best Regards
Diwakar G 7Diwakar G 7

What I need to give in token?

I tried security token, API key but it is not working. I am getting 401 unauthorized error.

Please help me.

Thanks and Regards,
Diwakar G