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.
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 requestspip install jsonpathpip install pytestoptional: This is to generate an HTML report
pip install pytest-html- PythonAPITest/
- countries/
- init.py
- test_countries.py
- main.py
- countries/
The test_countries.py file consists of 5 basic tests(functions) with some assertions included.
- test_get_all_countries : Function to test for all the available countries
- test_search_by_country_name : Function to test for search by country name.
- test_search_by_invalid_country_name : As the function name suggest, test to search for country by passing invalid name criteria
- test_get_country_name_by_currency_code : Test to retrieve country by passing valid currency code
- 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
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.testOR using
py.test countries/test_countries.pyresults of the test is displayed on the console showing the number of test passed (and if any failed)
To generate the report in html format run the below command
py.test --html=report.html