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

[API] Python - can't get custom_steps_separated to update with variable


#1

I’m having a bit of trouble getting multiple steps in a test case to update using Python. When I run this:

result = client.send_post(
        'update_case/' + caseID, {
            'custom_preconds' : precondition,
            'custom_test_data' : testData,
            'custom_steps_separated' : [
                {
                    "content" : "This is step 1",
                    "expected" : "This is also step 1"
                },{
                    "content" : "This is step 2",
                    "expected" : "This is also step 2"
                }
            ]
        }
    )

It works flawlessly. However, when I run this:

separatedSteps = '{
                    "content" : "This is step 1",
                    "expected" : "This is also step 1"
                 },{
                    "content" : "This is step 2",
                    "expected" : "This is also step 2"
                }'
result = client.send_post(
        'update_case/' + caseID, {
            'custom_preconds' : precondition,
            'custom_test_data' : testData,
            'custom_steps_separated' : [
                separatedSteps
            ]
        }
    )

I get a “Field :custom_steps_separated is not a valid array.” error. Is there a way I can use a variable for the custom_steps_separated field?


#2

Ah, I’ve actually figured out where I went wrong. I had tried using json.dumps/loads() beforehand, but it was giving me errors every time. Going back to that and looking at the data again, I actually had two sets of square brackets around the steps when using json.loads()

What ended up working for me:

separatedStepsNew = json.dumps("[" + separatedSteps + "]")
separatedStepsNew = json.loads(separatedStepsNew)

result = client.send_post(
        'update_case/' + caseID, {
            'custom_preconds' : precondition,
            'custom_test_data' : testData,
            'custom_steps_separated' :
                eval(separatedStepsNew)
        }
    )

I struggled with this all day yesterday, of course I figure it out almost as soon as I ask haha…


#3

Had a co-worker in my first IT job in the very late 90s-early 2000 that when he was stuck would ask me to come over. When he explained the problem to me the answer came to him usually while he was explaining it.


#4

Hi Sarah,

Thank you for your post. I am glad you were able to get this working. Thank you for coming back and posting what worked for you as well.