Write API automation test in python
Hi guys, this is a short article to show you how to setup the API test using Python language, on Mac OS (you can repeat the steps on the headline to install on the window). Hope you will get used to API test quickly because it is not too difficult. If you want to find out when testing the API, focus on what to test, check out this article , and have some useful tips in it.
Before starting, Open Iterm, and the Home folder of the Mac. You can choose another folder, depending on your needs.
1 / Install Homebrew
You install Homebrew on Mac to install other components, for example pip is simpler
1 | / usr / bin / ruby -e "$ (curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" |
Press ENTER to continue installing you :). It will ask for your password too.
If successful, it will show like this:
2 / Install python
I chose to use python, simply because I like: D. It is quite simple and easy to understand when programming. I won't say much, the next examples will use python, you'll catch up with it quickly :).
1 | brew install python |
3 / Install pip
It simply install Python for you
1 | sudo easy_install pip |
4 / Install requests
These "requests" are the necessary components of python to write test API code, in the request will provide enough functions for you to call GET, PUT, …
1 | Pip install requests |
5 / Install Django
This is optional, but when I install this, I will setup a project to write API test much easier.
1 | pip install django |
6 / Create a project
Here is the step to create the project. After this step, you can start writing test ?
Command line 1:
1 | django-admin startproject demotvn |
after that
1 | cd demotvn |
Command line 2:
1 | python manage.py startapp apidemo |
Finally in your directory there will be something like this:
7 / Demo write FPT public API test code, Method GET
document here http://doc.openfpt.vn/services/cyradar/documentation.html#/README
I will test this game V c loving this API
1 | http://api.openfpt.vn/cyradar/?url=https://dubkill.com |
If you copy and run it on the browser URL, you will see it result
1 2 3 4 5 6 7 8 | { "power": "danger", "domain": "dubkill.com", "threat": [ "malicious" ], "uri": "https://dubkill.com" } |
M ình will write code, to test whether you call this API, affinity parameter c "uri" returns c ó is "https://dubkill.com".
I will write code in decent test.py file. I like to use Pycharm, but you can even write code with text editor like sublime ^^.
I will write a test section for 1 public API:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import json import requests import unittest class TVNTestCase (unittest.TestCase): # fixtures = ['data / test_data_dump.json',] def setUp (self): # self.client = Client () self.request = requests def test_demo_get (self): url = 'http://api.openfpt.vn/cyradar/?url=https://dubkill.com' test_request = self.request.get (url) .json () #import pdb; # pdb.set_trace () self.assertEqual (test_request.get ('uri'), 'http: //dubkill.com', 'chet whip, wrong ha: D') |
You see, the syntax is nothing terrible:
- You define the URL
- Follow the pattern above, you request.get that URL, format it as json
- Then the returned data, according to the code, will be returned for test_request
- You get the data return from test_request. For example, to get the data of uri, then test_request.get ('uri'). This depends on the dental data format, but I will only.
- You assert it with data expect, not right, it says: "dead, wrong: D"
Don't be afraid, just copy the whole thing, override the tests.py file in your apidemo directory
Then to run this test file, do the following steps.
- You cd into the demotvn catalog
- Here, Run command: it will run all test cases in the tests.py file
1 | python manage.py apidemo.tests |
- If you have multiple test cases in this and just like to run 1 test case, you run
1 | python manage.py apidemo.tests.TVNTestCase.test_demo_get |
- Generally the syntax is:
1 | python manage.py folder_api_name.file_name.class_name.test_case_name |
- If you prefer to run the entire test case of a class, remove test_case_name
Then, you go to this step, follow me only, then the result is like this ?
FAILED! The reason is because you have the expectation result of http://dubkill.com while right, it must be https://dubkill.com. Fix the above code that will run the pass
8 / Demo API POST method
I will demo a decent post API, also from OpenFPT:
Document here http://doc.openfpt.vn/services/name2gender/documentation.html#/README
There you will see if you call the API you can call by copying and pasting this line into the terminal:
1 | curl -X POST -H "api_key: xxxxxxxxxxxxx" -H "Cache-Control: no-cache" -d 'Nguyen Duong Hai' "http://api.openfpt.vn/name2gender/json" |
You notice that 'xxxxxxxx' is the key you create by registering OpenFPT and create new app . If you do it yourself, I won't have my public key
Here is a good example to demo how to write code
- Header parameter
- Transmitting data at API call
- POST
Follow all steps as item 7, with the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | def test_demo_post (self): # this is URL url = 'http://api.openfpt.vn/name2gender/json' # this is custom headers. # you need to send api_key and Cache-Control headers = {"api_key": "xxxxxxxxxxxxxxx", "Cache-Control": "no-cache"} # This is Where bạn xác định dữ liệu data = {'Nguyen Duong Hai': ''} # -> Just want to say, IT IS WEIRD, usually the first is parameter # and come later is the value, but this API is just weird. #Now send your request post with data and header test_request = self.request.post (url, params = data, headers = headers) .json () # import pdb; # pdb.set_trace () self.assertEqual (test_request.get ('gender'), 'female', 'chet whip, wrong ha: D') |
ah, remember to make sure you have finished copying it right away! Python is based on alignment to know which part of the code =. =
9 / Debug with Python
For now, you probably have noticed, in my code, there is always a paragraph
1 2 | # import pdb; # pdb.set_trace () |
When you uncomment it and run it will be the debug gate. Your program will stop there for you to debug.
Try it, run here and then try:
- test_request
- test_request.get ('name')
- test_request.get ('gender')
If you want to continue running the code, type continue!
My guide is here, I wish you all the pleasure in testing API to develop your career.
ITZone via Nguyen Duong Hai