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

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 :).

3 / Install pip

It simply install Python for you

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, …

5 / Install Django

This is optional, but when I install this, I will setup a project to write API test much easier.

6 / Create a project

Here is the step to create the project. After this step, you can start writing test ?

Command line 1:

after that

Command line 2:

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

If you copy and run it on the browser URL, you will see it result

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:

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

  • If you have multiple test cases in this and just like to run 1 test case, you run

  • Generally the syntax is:

  • 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:

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:

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

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

Share the news now