Testing API with python


#1

I am using python + requests to do some post request
_**

  • I have a python function that generates a json which i send with the post request

**_

def capacity_types_gen():
capacity_types = {}
capacity_types[“total_capacity”] = random.uniform(0.5, 30.0)
capacity_types[“wet_percentage”] = random.uniform(0.0, 100.0)
capacity_types[“dry_percentage”] = 100 -
capacity_types[“wet_percentage”]
return capacity_types

My api rules at the end point are:

total_capacity: ‘required|number|above:0|unique:capacity_types’,
wet_percentage: ‘number|range:0,100.01’,
dry_percentage: ‘number|range:0,100.01’

Then i use python requests to post the json generated

post_response = requests.post(BASE_URL + “capacity_types”, data = capacity_types_gen()).json()

The validation fails at total_capacity for the rule number.
Copying the generated json from capacity_types_gen() and posting manually using postman works fine, so I don’t think the issue is datatype for total_capacity.

How can I post data from python code.

Thanks for any help.


#2

Sorry, I got my issues resolved.
Apparently requests module has a way to convert python dictionary to json

So instead of

post_response = requests.post(BASE_URL + “capacity_types”, data = capacity_types_gen()).json()

We use

post_response = requests.post(BASE_URL + “capacity_types”, json= capacity_types_gen()).json()


#3

Simple question, why do you not use the testing framework provided by Adonis to test your application?