Skip to content

xbns/python-api-tests

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

What this project is for?

This project uses the following API endpoint https://jsonplaceholder.typicode.com/

The scripts therein does the following:

  • Does a GET request to the /users endpoint.
  • Validates the response code to be 200.
  • Validates the response time to be less than 200ms.
  • Iterates over all elements of the json response body and print out all company names ending in “ Group”.

Pre-requisites

  1. Clone the repository
$ git clone https://github.com/jaymutuku/python-api-tests
$ cd to python-api-tests/

Create and Activate a Virtual Environment

$ pip3 install virtualenv
$ virtual -p python3 venv 
$ source venv/bin/activate

Incase you get an error in the second step above: command 'virtualenv' not found ... Then

$ sudo apt install virtualenv

Install the required packages

$ pip install requests ijson

You don't need to install the unittest library is a built-in library.

Save the dependencies to requirements.txt

$ pip freeze > requirements.txt

How do I run the project?

  1. Discover and run the tests!
$ python -m unittest discover
  1. Expected Output
Company Name: Johns Group
Company Name: Abernathy Group
.Response Status: 200
.Response Time: 0.478977
F
======================================================================
FAIL: test_response_time (users_test.test_users.APITests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/<username>/Desktop/humanitec/answers/python-api-tests/users_test/test_users.py", line 20, in test_response_time
    self.assertLess(responseTime,0.2)
AssertionError: 0.478977 not less than 0.2

----------------------------------------------------------------------
Ran 3 tests in 1.587s

FAILED (failures=1)

About

REST API Test using Python Request Library & Unittest

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages