Consider the following graph, each node is a function,
It is used for testing independent components of your software, for example, writing a unittest for function A to check expected output.
Mocking means creating a fake version of an external or internal service that can stand in for the real one, helping your tests run more quickly and more reliably. When your implementation interacts with an object’s properties, rather than its function or behavior, a mock can be used.
For example, function C is highly dependent on A and B, it is hard to test it independently, you might need to fake A and B to make C outputing expected results.
It is used to test the whole program, ignoring individual components. You input expected arguments, and test the expected outputs.
from flask_jsonpify import jsonpify
from flask import Flask, request
app = Flask(__name__)
@app.route("/")
def home():
return jsonpify({'output':'Hello World'})
if __name__ == "__main__":
app.run()
export PYTHONPATH=src
from app import *
import unittest
import json
def test_home():
assert home.__name__=='home'
class MyAppCase(unittest.TestCase):
def setUp(self):
app.config['TESTING'] = True
self.app = app.test_client()
def test_home(self):
resp = self.app.get('/')
data = json.loads(resp.get_data(as_text=True))
self.assertEqual(data['output'],'Hello World')
In your app folder, run pytest
.