Skip to content

A simple REST API python test using the Pytest unit test framework

Notifications You must be signed in to change notification settings

kunle-johnson/PythonAPITesting

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Python API Testing - Readme

This is a simple RESTful APIs test exercise using the Python requests library and the pytest unit testing framework.

The pytest framework makes it easy to write simple tests and scale to support complex functional testing of applications.

The API used for this test is available at restcountries.eu, which shows vital information about the different countries in the world.

Usage

To get started, clone or download the project. We then need to open the project in our IDE (PyCharm, but any decent IDE works) by browsing to the download folder directory. We need a python interpreter to run this test if you do not have this in your IDE, click HERE to download

Now, the following are python modules that needs to be installed to successfully execute test. Use the python package installer pip to install the following modules: requests, jsonpath and pytest. From the terminal run the following commands:

pip install requests
pip install jsonpath
pip install pytest

optional: This is to generate an HTML report

pip install pytest-html

Code Structure

  • PythonAPITest/
    • countries/
      • init.py
      • test_countries.py
    • main.py

The test_countries.py file consists of 5 basic tests(functions) with some assertions included.

  1. test_get_all_countries : Function to test for all the available countries
  2. test_search_by_country_name : Function to test for search by country name.
  3. test_search_by_invalid_country_name : As the function name suggest, test to search for country by passing invalid name criteria
  4. test_get_country_name_by_currency_code : Test to retrieve country by passing valid currency code
  5. test_search_by_list_of_codes_all_invalid_codes : Function to test for search for country by list of (different) codes by passing invalid codes

Run Test

The test can be run using any IDE or from the command line. On the IDE, you can run the test from the project root folder (PythonAPITest/) using the command

py.test

OR using

py.test countries/test_countries.py

results of the test is displayed on the console showing the number of test passed (and if any failed)

HTML Report

To generate the report in html format run the below command

py.test --html=report.html

About

A simple REST API python test using the Pytest unit test framework

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages