This is the 15th day of my participation in the August More Text Challenge

A lot of people can probably use Postman, Python, but they can’t write test scripts, and they want to quickly write scripts for python automated tests, and postman has the completed use cases in it. But can’t write python script, want to convert, this time, I will copy the postman code into the Python code, the required steps. I’ll explain it to you.

So first we go to Postman, and we can export our use cases into Python code,

Turing interface for example:

So we have this postman use case, we click Code,

Select the language to export, in this case Python, and select the Requests library to export the code

So if we look at the generated code,


import requests

url = "http://openapi.tuling123.com/openapi/api/v2"

payload = "{\r\n\t\r\n \"userInfo\": {\r\n \"apiKey\": \"\",\r\n \"userId\": \"\"\r\n }\r\n}"
headers = {
    'Content-Type': "application/json".'User-Agent': "PostmanRuntime / 7.19.0".'Accept': "* / *".'Cache-Control': "no-cache".'Postman-Token': "25132 ec6-9 d02-421 - c - b1fd70035 ab22-773, 65 c29f56-030 - a - 4 d - 862 - f - ad0de3ed50a6".'Host': "openapi.tuling123.com".'Accept-Encoding': "gzip, deflate".'Content-Length': "78".'Connection': "keep-alive".'cache-control': "no-cache"
    }

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)
Copy the code

We copy the code into the editor,

If we don’t have the Reuqests library, we might get an error, so we need to install the Reuqests library.

Command:

pip install reuqests
Copy the code

So let’s run it.

So if we look at the execution,

There’s a lack of assertions here, so let’s just add our assertions.

Our assertion is that it is successful assuming there is a code field in it. Running code results

This is a simple test script.

One might ask, how do I convert it to unitTest?

Let’s first introduce unitTest

Then define a test class that extends from UnitTest.testCase

Write test cases.

import requests
import unittest

class Testcase(unittest.TestCase) :
    def tearDown(self) - >None:
        pass
    def setUp(self) - >None:
        pass
    def testone(self) :
        url = "http://openapi.tuling123.com/openapi/api/v2"

        payload = "{\r\n\t\r\n \"userInfo\": {\r\n \"apiKey\": \"\",\r\n \"userId\": \"\"\r\n }\r\n}"
        headers = {
            'Content-Type': "application/json".'User-Agent': "PostmanRuntime / 7.19.0".'Accept': "* / *".'Cache-Control': "no-cache".'Postman-Token': "25132 ec6-9 d02-421 - c - b1fd70035 ab22-773, 65 c29f56-030 - a - 4 d - 862 - f - ad0de3ed50a6".'Host': "openapi.tuling123.com".'Accept-Encoding': "gzip, deflate".'Content-Length': "78".'Connection': "keep-alive".'cache-control': "no-cache"
            }

        response = requests.request("POST", url, data=payload, headers=headers)

        self.assertTrue("code" in response.text)
if __name__=="__main__":
    unittest.main()
Copy the code

Let’s go ahead and execute the test case.

In this way, we take a use case from Postman directly into Python to form an automated test case. But this is just the beginning.