简体   繁体   中英

How do you simulate a REST API interface?

I have a web app which I would like to test using Selenium, with this app communicating with the backend using a REST API.

It is my understanding that Selenium is mainly used to test the flows through an application and the appearance/presence of widgets for each of these states. This would suggest to me that it makes a lot of sense when writing Selenium tests to simulate the backend. Python is my language of choice but I am also familiar with node.js, javascript and JAVA. What approach would you recommend with regard to simulating the REST API. I was thinking of writing a server using Python. I can create this server within my test environment and configure how it responds to requests from the front-end on a test by test basis. Are there any tools, libraries you might recommend me?

I should also add that I am using raml to define my api.

So with my simulation of the backend, the tests would look something like this:

def test_no_table_for_one_user():
    # configure reply for api request
    rest_sim.get_users_response = (200, [{name: "Foo Bar", address: "West side"}])
    navigate_to_users_page()

    # test that this users details are presented without the use of a table  
    ...

def test_table_for_multiple_users():
    # configure reply for api request
    rest_sim.get_users_response = (200, [{name: "Foo Bar", address: "West side"}, {name: "Foo Baz", address: "East side"}])
    navigate_to_users_page()

    # test that the two users are presented in the form of a table
    ...

For mocking simple REST API you can try node.js-based json-server .

It's easy to setup, you just need to create JSON file with some data to simulate db, json-server will create all common REST API routes for you.

I would choose Python without any problems.

Please have a look at the requests module: http://docs.python-requests.org/en/master/

It is really easy to install and to make requests by using REST messages. No matter what you have to do, I will continue with Python. How to use REST API from requests after installation (with pip for instance):

import requests
class RESTIF():
    '''
    Class to handle a connection towards your server
    ''' 

    def __init__(self):
        '''Initialization of a single Session and header dictionary to be used for REST requests.'''    
        self.nSession = requests.Session()
        # Default header values to get initial connection:
        self.header = {"Content-Type":"application/json",
                       "KeepAlive":"true",
                       "Authorization":"Basic ",
                       "Cookie": None}

       def action(self, URL, JSONdata):
           myCreate = self.nSession.post(URL, headers=self.header, data=JSONdata)

The header is quite useful in case of Cookie exchange. Requests will take care of that.

You can handle login to a rest api or send PUT/POST/DELETE/GET messages easily! Everything is possible and no need to switch to Java or other languages. Please let me know if you have additional queries or that solved your question. Have a great one!

There are many libraries and tools available to help you with this. What you are talking about is creating a test harness, or emulator/simulator. In most situations, it is generally recommended that the person who builds and develops the backend, provide you with the harness since they are owners of the API and control different versions and change. Reciprocally, you can provide them with your client so they can understand how you use the API.

If they cannot do this, then you will need to create a harness yourself. The best tool to help you do this for HTTP API's is WireMock

http://wiremock.org/

In your example you will probably want to run this as Stand Alone:

http://wiremock.org/docs/running-standalone/

And then using JSON file configuration to define the behaviour.

My preference is to also wrap and deploy you WireMock test harness as a Docker image and publish to a Docker repository so that other people can use it. In this case its simply a case of creating a Dockerfile with the following and running a docker container:

Dockerfile

FROM java:8
WORKDIR /opt
RUN apt-get install wget
RUN wget http://repo1.maven.org/maven2/com/github/tomakehurst/wiremock-standalone/2.5.0/wiremock-standalone-2.5.0.jar
RUN mkdir mappings
VOLUME /opt/mappings
EXPOSE 8080
CMD java -jar wiremock-standalone-2.5.0.jar

command line

docker build -t wiremock/apiname:[version] .
docker run -d -p [exposedport]:8080 -v /directory/with/json:/opt/mappings --name apiname wiremock/apiname:[version]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM