Skip to main content

Package to test Rest API endpoints along with snap mode and image comparison functionality

Project description

SnapyRest

SnapyRest module is to hold all the common methods used in REST API automation in PyRest-Python Framework. This framework includes methods to download the image files from the rest API and then compare with the stored image files. This framework is built in Python and inspired from the simplicity of Karate framework by Intuit and snapshot mode from Jest framework by Facebook.

License: GPL v3 Made with Python StackOverflow Contributions Welcome email me

alt text

Installation

Add this line to your application's requirements.txt file:

snapyrest==<version>

And then execute:

pip3 install -r requirements.txt

Or install it yourself as:

pip3 install snapyrest

Usage

This module is built to replace the library methods in PyRest-Python framework. This allows us to share the methods among different teams and completely ignore the repetitive work. For more details on simple no code Rest API automation verify PyRest-Python page.

Adding new methods

Add all the new methods inside /SnapyRest/ path and add import the class inside __init__.py file in the same path so that the newly added class can be imported by using this module.

Require and Include

To import this module use,

import snapyrest

To import specific class in this module use,

from snapyrest.api import Api

Detailing the module

Api

This file has all the common methods that are used to execute the rest api calls. Allure reporting and attaching the required files are handled by default so that just calling this methods will take care all the actions. Some of the API actions are,

Simple test case with an endpoint

For a very simple basic get request and to validate the response code we could do,

Api.get("/name")
Api.verify_response_code(200)

On calling only these two methods from the Api library, all the allure report actions, attaching the request and the response file to the reports, and asserting the response code of the response is taken care off.

Simple test case with validating the response with test data

To validate the response json with a test data, one could do the following,

Api.get("/name")
Api.verify_response_code(200)
Api.verify_response_json("sample.yml", "test_sample_get_request_001")

Here, we are trying to take the sample.yml file under /Data/DynamicData/ folder and then fetch the data for the key test_sample_get_request_001. After getting the data from the stored file, we will compare that with the response data and generate the allure reports along with necessary attachments.

The YAML file will be looking like,

test_sample_get_request_001:
  age: 20
  name: Naresh

While fetching the key from a yaml file, the above file structure will return the data in JSON format. This in turn gives us the edge while creating the test data. One can always save the key value in direct JSON format as well.

test_sample_get_request_001:
  age: 20
  name: Naresh

OR

test_sample_get_request_001: { "age": 20, "name": "Naresh" }

In either way JSON parser will get the values in JSON format. Whereas when we use snap mode, the file will be saved in the first format which we can see in detail below.

Simple test case with validating the response with test data and ignoring few keys

While validating an api response, we may encounter a scenario where we don't want to validate few keys. In such scenario one can do the following,

Api.get("/name")
Api.verify_response_code(200)
Api.ignore_keys("age")
Api.verify_response_json("sample.yml", "test_sample_get_request_001")

The above code will validate the response status code, response json values except age key. If you want to have more keys that are supposed to be ignored, have that in the comma separated format,

Api.get("/name")
Api.verify_response_code(200)
Api.ignore_keys("age,name")
Api.verify_response_json("sample.yml", "test_sample_get_request_001")

This will ignore the keys age and name while validating the response with the stored data.

Simple test case with validating the response with test data and custom markers

While validating an api response, we may encounter a scenario where we need to validate whether a key is present or not but not the value for that key. In that case one can always have that marked in their test data with the unique markers specified with $ symbol.

Test File:

Api.get("/name")
Api.verify_response_code(200)
Api.verify_response_json("sample.yml", "test_sample_get_request_001")

Data File:

test_sample_get_request_001:
  age: $notnull
  name: Naresh

The above combination will validate the response as,

  1. Whether age key is present without Null value in it.
  2. And name is present with the exact same value Naresh in it.

We can also make the validation so specific for the age field in the above example by mentioning that value corresponds to age should be a number. To achieve this we need to have the following combination.

Test File:

Api.get("/name")
Api.verify_response_code(200)
Api.verify_response_json("sample.yml", "test_sample_get_request_001")

Data File:

test_sample_get_request_001:
  age: $number
  name: Naresh

Apart from the above two there are multiple markers available which are listed as follows,

Marker Description
$notnull Expects actual value to be not-null
$array Expects actual value to be a JSON array
$object Expects actual value to be a JSON object
$boolean Expects actual value to be a boolean true or false
$number Expects actual value to be a number
$string Expects actual value to be a string
$uuid Expects actual (string) value to conform to the UUID format

Images

This module has all the methods corresponding to downloading an image from the REST API endpoint and to validate the same. Similar to API file, all the allure reporting and attachments are taken care here by default, just calling the methods with proper prams will do the necessary steps. Some of the common methods in images file are,

Test cases with validation of images

In few scenarios if we need to validate the image file from the response, first we need to hit the endpoint and get the image URL, after which we need to download the image from the URL and store that in temporary folder, and then compare the image with the stored image. To do this,

Api.get("/image")
Api.verify_response_code(200)
Api.verify_response_json("sample.yml", "test_sample_get_request_003")
image_url = Api.get_params_from_response("image")
Img.download_image(image_url, "downloaded_file")
Img.is_equal("Naresh", "downloaded_file")

The above code will save a value from the response json through Api.get_params_from_response. If the URL is present inside the nested json one can always give the path to the image url using comma separated value like,

Api.get_params_from_response("image,0,user,profile,image")

After getting the Image URL, we need to download it and save it in the temporary folder under reports/images. We are also supposed to send the name for the downloading image file. All the download and comparison of images are happening in png format. We need a change in framework if we want to compare images with some other format.

Now after downloading, directly give the image name against which we need to compare the downloaded image. The stored image must be under the folder /Data/Images/.

The method Img.is_equal takes care of all the allure reporting part, attaching the images to the report and if there is a mismatch between the images, difference between two images also will be attached to the allure report. as mentioned in the above allure report topic.

Test cases with validation of images along with tolerance

In few scenarios if we need to validate the image file from the response along with the allowed tolerance. The above method will result in failure even if there is a minute change in the image file. To validate the images along with tolerance one has to change,

From:

Img.is_equal("Naresh", "downloaded_file")

To:

Img.is_equal_with_tolerance("Naresh", "downloaded_file")

This will take the tolerance level from the global data file and validate. Its always recommended to use same tolerance level across the project, but in few cases if one need to have custom tolerance level to an image compare one has to do,

Img.is_equal_with_tolerance("Naresh", "downloaded_file", 0.5)

The above code will validate the images with 0.5 percent tolerance level.

Store

This class is to store the run time configurations for this module. Kind of a memcached or redis for our framework. Centralized run time data which are needed by other modules are being stored here and retrieved by other modules.

Variable

This file is used to have the variable data in the runtime. We have a capability to make environment variable have more precedence than the global variable, to implement this we use this file.

Snap Mode

Snap mode is handled in both API and Images file, where if the pytest suite is triggered with the environment variable snap=1 if there is any failure, the file (either YAML data file or the Image file) will be replaced by the response data.

snap=1 pytest

But, only this will not ensure the data is getting saved in to dynamic file or image file. One has to script their automation code in such a way that snap feature is supported. For example one can look in to PyRest-Python framework inside Tests/test_sample.py

Upon running the tests in normal mode pytest the dynamic data will not be overridden rather it will assert the data response data with the stored data.

To get this feature running smoothly and to access the variables in smoother way, one has to configure the following params in their framework in pytest_configure method (so that these path variables will be set on initiating the pytest).

from selpy.store import Store

def pytest_configure(config):
    # Configuring the selpy with data path location
    Store.global_data_path = os.path.dirname(os.path.abspath(__file__)).replace("/Tests", "") + '/Data/GlobalData/global_data.yml'
    Store.static_data_path = os.path.dirname(os.path.abspath(__file__)).replace("/Tests", "") + '/Data/TestData/'
    Store.dynamic_data_path = os.path.dirname(os.path.abspath(__file__)).replace("/Tests", "") + '/Data/DynamicData/'
    Store.root_path = os.path.dirname(os.path.abspath(__file__)).replace("/Tests", "")

To publish a module in pypi

  1. Install the following dependencies
python3 -m pip install --user --upgrade setuptools wheel
  1. In the root directory of your newly created module run,
python3 setup.py sdist bdist_wheel
  1. Then add the username and password and upload to the pypi server,
python3 -m twine upload -u <username> -p <password> --repository-url https://upload.pypi.org/legacy/ dist/* --verbose

Ensure that you have deleted the old files from your dist directory.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/nareshnavinash/selpy/. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

Authors

License

The gem is available as open source under the terms of the GPL-3.0 License.

Code of Conduct

Everyone interacting in the Teber project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

snapyrest-0.2.0.tar.gz (25.1 kB view details)

Uploaded Source

Built Distribution

snapyrest-0.2.0-py3-none-any.whl (26.6 kB view details)

Uploaded Python 3

File details

Details for the file snapyrest-0.2.0.tar.gz.

File metadata

  • Download URL: snapyrest-0.2.0.tar.gz
  • Upload date:
  • Size: 25.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.9

File hashes

Hashes for snapyrest-0.2.0.tar.gz
Algorithm Hash digest
SHA256 a33e4bc6acfac6c2ca1a4fe1ebccc2822fbdf17b8f72a612d5e352b086337d64
MD5 9398b1df70d95b52401fb02264608ac9
BLAKE2b-256 20348a32f1db9ccf4977a455ab4d34cadf9f9a8579a79961eb3aef240528be62

See more details on using hashes here.

File details

Details for the file snapyrest-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: snapyrest-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 26.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.9

File hashes

Hashes for snapyrest-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2bbec5a075a73bb75bad6369ce3020264a7c895a90738dddf0607c5e6b78fa26
MD5 55161932bc222bf677b7c2aea90ac0f9
BLAKE2b-256 c12c744983d75d5f5f6a64feaf118cf196d4f5707c61a9549f2c754659880b77

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page