Test Environment with Mocked REST API

mock api
jest mock api calls
node mock api
jest mock axios
jest mock api calls example
postman mock server
react native api testing
react mock fetch api

Lets say I have a very simple web app which is presented as blue if the current president is a democrat and red if they are a republican. A REST API is used to get the current president, via the endpoint:


which currently returns the json object:

{name: "Donald Trump", party: "Republican"}

So when my page loads I call the endpoint and I show red or blue depending on who is returned.

I wish to test this HTML/javascript page and I wish to mock the back-end so that I can control from within the test environment the API responses. For example:

def test_republican():
    # configure the response for this test that the web app will receive when it connects to this endpoint
            name="Donald Trump",

    # start the web app in the browser using selenium 
    load_web_app(driver, "http://localhost:8080")  

    e = driver.find_element_by_name("background")
    assert(e.getCssValue("background-color") == "red")

def test_democrat():
    # configure the response for this test that the web app will receive when it connects to this endpoint
            name="Barack Obama",

    # start the web app in the browser using selenium 
    load_web_app(driver, "http://localhost:8080")  

    e = driver.find_element_by_name("background")
    assert(e.getCssValue("background-color") == "blue")

So the question is how should I implement the function configure_endpoint() and what libraries can you recommend me?

As @Kie mentioned, configure_endpoint implementation won't be enough, if you're going to stub the whole server-side within Selenium Python code. You would need a web server or whatever that will response via HTTP to requests from within testing environment.

It looks like the question is partially about testing of client-side code. What I see is that you're trying to make unit-test for client-side logic, but use integration testing suite in order to check this logic (it's strange).

The main idea is as follows.

You're trying to test client-side code. So, let's make mocks client-side too! Because this part of code is completely client-side related stuff.

If you actually want to have mocks, not stubs (watch the difference here: https://stackoverflow.com/a/3459491/882187) it is a better way to mock out HTTP requests inside your Javascript code. Just because you're testing a client-side piece of code, not some parts of server-side logic.

Having it isolated from whatever server-side is - is a great idea that you would love when your project become grow, while more and more endpoints will be appearing.

For example, you can use the following approach:

var restResponder = function() { // the original responder your client-side app will use
  this.getCurrentPresident = function(successCallback) {
    $.get('/presidents/current', callback);

var createMockResponder = function(president, party){ // factory that creates mocks
  var myPresident = president;
  var myParty = party;

  return function() {
    this.getCurrentPresident = function (successCallback) {
      successCallback({"name": myPresident, "party": myParty});

// somewhere swap the original restResponder with new mockResponder created by 'createMockResponder'

// then use it in your app:

function drawColor(restResponder, backgroundEl) {
     if (data.party == "Democrat") $(backgroundEl).style('background-color', 'blue')
     else if (data.party == "Republican") $(backgroundEl).style('background-color', 'red')
     else console.info('Some strange response from server... Nevermind...');

Practically, this implementation depends on what do you have at the client-side as a framework. If jQuery, then my example is enough, but it looks very wordy. In case you have something more advanced, like AngularJS, you can do the same in 2-3 lines of code:

// Set up the mock http service responses
$httpBackend = $injector.get('$httpBackend');
// backend definition common for all tests
authRequestHandler = $httpBackend.when('GET', '/auth.py')
                                 .respond({userId: 'userX'}, {'A-Token': 'xxx'});

Check out the docs: https://docs.angularjs.org/api/ngMock/service/$httpBackend

If you're still stick to the idea, that you need mocks inside Selenium tests, please try this project: https://turq.readthedocs.io/en/latest/

It serves with Python DSL for describing REST responders. Using turq your mocks will look as follows:

path('/presidents/current').json({'name':'Barack Obama', 'party': 'Democrat'}, jsonp=False)

Also, I would recommend to try stubs instead of mocks and use this Python module: mock-server https://pypi.python.org/pypi/mock-server/0.3.7 You are required to create the directory layout containing corresponding pre-populated JSON responses and to add some boilerplate code in order to make the mock-server respond on 'localhost:8080'. The directory layout for your example will look like this:

      GET_200.json      # will contain {"name": "Barack Obama", "party": "Democrat"}
      GET_200.json      # will contain {"name": "Donald Trump", "party": "Republican"}

But the mock_server is based on Tornado, it is very heavy solution for using in tests I think.

I hope, my answer is helpful and informative. Welcome to discuss it! I made tons of projects with Selenium, big and small tests, tested client-side and server-side.

Developing mock API calls can help you use valuable unit tests, without the problems associated with calling a live API. Node.js offers several  Test Environment with Mocked REST API. Ask Question Asked 2 years, 10 months ago. Active 1 year, 1 month ago. Viewed 674 times 8. 1. Lets say I have a very simple web app which is presented as blue if the current president is a democrat and red if they a

I would use tornado web framework.

import json
import functools
import operator
from tornado import ioloop, web, gen
from tornado.options import define, options

define("data_file", default='default/mock.json', type=str)

class Handler(web.RequestHandler):

    def data_received(self, chunk):

    def initialize(self, data):
        self.data = data

    def get(self, *args, **kwargs):
        path = self.request.path.split("/")[1:]
        path = functools.reduce(
            [[k, v[0].decode("utf-8")] for k, v in         self.request.query_arguments.items()],

            self.write(functools.reduce(operator.getitem, path, self.data))
        except KeyError:

class Application(web.Application):
    def __init__(self):
        data = {}
        with open(options.data_file) as data_file:
            data = json.load(data_file)

        handlers = [
            ('(.*)', Handler, {"data": data})
        settings = dict(
        web.Application.__init__(self, handlers, **settings)

    def main():
        io_loop = ioloop.IOLoop.instance()
        backend_application = Application()

    if __name__ == "__main__":

This is a code I used for mocking a REST-API which is a standalone script, but it can be embedded into your test environment as well.

I defined a JSON file which defines the different path components and what should be returned. Like this:

    "presidents": {
        "current": {
            "name": "Donald Trump", 
            "party": "Republican"

I saved this to a mock.json and called the script with a parameter mock_rest.py --data-file="./mock.json".

I hope that gives you a starting point and a good example.

SoapUI, is the world leading Open Source Functional Testing tool for API Testing. Placing the Mock in the Live Environment can reveal problems with network  Test Environment with Mocked REST API. 279. January 27, 2017, at 07:27 AM. Lets say I have a very simple web app which is presented as blue if the current president is a democrat and red if they are a republican. A REST API is used to get the current pres

If your load_web_app function uses the requests library to access the REST API, using requests-mock is a convenient way to fake that library's functionality for test purposes.

Let's look at how to test the use of an external API using Python mock objects. predictable ways, and you need your tests to execute in a controlled environment​. Free Bonus: Click here to download a copy of the "REST API Examples" Guide  How to use JSON Server to mock REST API in local environment 1. Install JSON Server. Open your Terminal. Execute the following command. npm install -g json-server 2. Go to the folder where you want to store the json files. Create json file as per the response body of the API you 3. Start JSON

For those who stumble upon this question, and do not want to end up writing the code to create their own mock server implementations of the API, you can use Mocktastic, which is a downloadable desktop application for Windows, MacOS and Linux, which provides an easy to use GUI to setup your mock API servers.

Beeceptor is a free online tool for mocking a REST API interaction using without the need to redeploy or utilize multiple testing environments,  We do not want our unit test to be dependent on a third-party service, and therefore, we need a way to mock the Rest API. There are several options to mock a REST API, but we will make use of the

Mock tests isolate both the issues of environmental impact and says on the tin – a sandbox for testing your RESTful APIs and SOAP services. Test faster, earlier, and more completely. Remove testing roadblocks. Close the gap that exists with incomplete or capacity constrained test environments. Eliminate fees for accessing third-party systems. Give QAs access to the exact test environment they need without delays. Streamline test environment provisioning time and costs.

Sending API Requests Mock Servers in Postman let you simulate APIs. Check the checkbox if you want to save the mock server URL as an environment​  The Spring Test module includes a mock server named MockRestServiceServer. With this approach, we configure the server to return a particular object when a specific request is dispatched through our RestTemplate instance. Finally, we can verify () on that server instance whether all expectations have been met or not.

Sending API Requests Mocking with the Postman API Echo service which you can use to test out your REST clients and make sample API calls. The single saved example (P1) does not use environment variables, so you can create a  Testing async API calls using Jest’s mocking features Jest is a great JavaScript testing framework by Facebook. It’s often used for testing React components, but it’s also a pretty good general purpose testing framework. In this tutorial I’ll give a quick and simple demo of it’s mocking capabilities for testing async functions.

  • No load_web_app() simply loads the html/js file in the browser using selenium. I need to mock the backend by creating a api server to which the web app connects to. This mock server should be configurable from within the test environment.
  • Do you have a good reason to want to isolate the system under test that far away from the tested client business logic rather than much closer to it? (I'm assuming you use some already well-tested library for network access and are not coding that up yourself. If you do, your test(s) would of course have to cover that part the, too.)
  • The API is already covered with tests. These tests involve calling the api and testing that the correct response is received. I'd like to now test the flow within the web app and to test that the app behaves as expected for those browsers I wish to support. In other words, I'd like to treat the front-end as a subsystem and write sub system tests for it.