Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
A little background: over the last few months, I have been contributing in open source organization FOSSASIA, where Iām working on a project called BadgeYaY. It is a badge generator with a simple web UI to add data and generate printable badges inĀ PDF.
Badgeyay back-end is now shifted to REST-API and to test functions used in REST-API, we need some testing technology which will test each and every function used in the API. For our purposes, we chose the popular unit tests Python testĀ suite.
In this blog, Iāll be discussing how I have written unit tests to test Badgeyay REST-API.
First, letās understand what is unit tests and why we have chosen it. Then we will move onto writing API tests for Badgeyay. These tests have a generic structure and thus the code I mention would work in other REST API testing scenarios, often with little to no modifications.
Letās get started and understand API testing step byĀ step.
What is UnitĀ tests?
Unit tests is a Python unit testing framework which supports test automation, sharing of setup and shutdown code for tests, aggregation of tests into collections, and independence of the tests from the reporting framework. The unit test module provides classes that make it easy to support these qualities for a set ofĀ tests.
Why UnitĀ tests?
We get two primary benefits from unit testing, with a majority of the value going to theĀ first:
- Guides your design to be loosely coupled and well fleshed out. If doing test driven development, it limits the code you write to only what is needed and helps you to evolve that code in smallĀ steps.
- Provides fast automated regression for re-factors and small changes to theĀ code.
- Unit testing also gives you living documentation about how small pieces of the systemĀ work.
We should always strive to write comprehensive tests that cover the working code prettyĀ well.
Now, here is glimpse of how I wrote unit tests for testing code in the REST-API back-end of Badgeyay. Using unit tests python package and requests modules, we can test REST API in test automation.
Below is the code snippet for which I have written unit tests in one of my pull requests.
def output(response_type, message, download_link): if download_link == āā: response = [ { ātypeā: response_type, āmessageā: message } ] else: response = [ { ātypeā: response_type, āmessageā: message, ādownload_linkā: download_link } ] return jsonify({āresponseā: response})
To test this function, I basically created a mock object which could simulate the behavior of real objects in a controlled way, so in this case a mock object may simulate the behavior of the output function and return something like an JSON response without hitting the real REST API. Now the next challenge is to parse the JSON response and feed the specific value of the response JSON to the Python automation script. So Python reads the JSON as a dictionary object and it really simplifies the way JSON needs to be parsed andĀ used.
And hereās the content of the backend/tests/test_basic.py file.
#!/usr/bin/env python3
āāāTests for Basic Functionsāāāimport sysimport jsonimport unittest
sys.path.append(ā../..ā)from app.main import *
class TestFunctions(unittest.TestCase): āāāTest case for the client methods.āāā def setup(self): app.app.config[āTESTINGā] = True self.app = app.app.test_client() # Test of Output function def test_output(self): with app.test_request_context(): # mock object out = output(āerrorā, āTest Errorā, ālocal_hostā) # Passing the mock object response = [ { ātypeā: āerrorā, āmessageā: āTest Errorā, ādownload_linkā: ālocal_hostā } ] data = json.loads(out.get_data(as_text=True) # Assert response self.assertEqual(data[āresponseā], response)
if __name__ == ā__main__ā: unittest.main()
And finally, we can verify that everything works by running nosetestsĀ .
This is how I wrote unit tests in BadgeYaY repository.
With that, I have reached the end of our discussion on writing Unit Tests for REST API in Python Web Application. I wrote this post as a solution to this issue in BadgeYaY project. If you liked this post, consider having a look at my other work on GitHubĀ š.
SourcesĀ : Unit testing framework
PS: Iām new to blogging, so constructive criticism is not only welcomed, but very muchĀ wanted!
Writing Unit Tests for REST API in Python was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.