当前位置:网站首页>Several common implementation methods of mock interface test

Several common implementation methods of mock interface test

2022-06-21 15:17:00 m0_ sixty-one million eighty-three thousand four hundred and ni

What is an interface Mock test

  • Mock Test definition

    Mock Testing is in the process of testing , For some complex objects that are not easy to construct or obtain , Use a mock object to create a test method for testing

  • Mock Test application scenarios

    • The test interface needs to rely on the return value of the third-party interface for logical processing , For example, the payment interface of the bank
    • Simulate abnormal data return , For example, you need to return special characters 、 Different lengths 、 Type format content
    • Front and back end development parallel work mode , When the back-end interface is not completed , Front end calls Mock Data is adjusted
    • Isolation environment , Ensure data security and correctness , about POST、PUT、DELETE Wait for the request to be isolated , Prevent other unknown errors in modifying data

Mock Several common implementation methods

  • fiddler/Charles Tools
  • Mock Server-Moco
  • Self development Mock platform

adopt fiddler

  • stay fiddler Interface ---- Find out what you want mock The interface of

    With URI:/api/v1/my/index For example ,fiddler Of web session Interface find the following interface /api/v1/my/index This interface

  • Save interface response data

    Right click on the interface , Choose Save -> Respond to -> Response subject , Save to the specified folder :my_index.json
     Insert picture description here

  • Modifying data

    Modify the saved to local my_index.json file , Add special characters to the user name .

  • add to /api/v1/my/index To mock The rules

    stay web session Find the corresponding request in the panel , Then drag it to or select the interface and click Add to AutoResponder The palette , stay RuleEditor Middle click “Find a file…”, Select local json Path to file , Click save .
     Insert picture description here
     Insert picture description here
     Insert picture description here

  • Activation rules

    Check enable rule and request delivery , Activation rules .
     Insert picture description here

  • Refresh the page

    Refresh the browser ( Or direct access interface ), And you can see the effect , I want to modify other data later , Just modify the locally saved json File can .

  • Cancel mock

    You only need to use the auto response rule list area interface , Uncheck it
     Insert picture description here

Mock Server-Moco

Server-Moco explain

  • Moco Is a simple framework to build a simulation server ( Tools ), Can simulate http、https、socket Such agreement
  • be based on Java Open source project developed ,Github Address :https://github.com/dreamhead/moco
  • principle :Moco According to some configuration , Start a real HTTP service ( Will listen to a local port ). When an initiated request meets a certain condition , The specified response data will be returned , Need modification

Environment building

  • Download and install : rely on JDK Environmental Science , Perform computer installation JDK, Proposed installation JDK1.8 edition
  • Server-Moco Download address :https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner, Select the appropriate version , Download the latest version 1.3.0, Download it moco-runner-1.3.0-standalone.jar package
     Insert picture description here
     Insert picture description here

Moco Use

Start parameter description
java -jar <path-to-moco-runner> http -p <monitor-port> -c <configuration-file>

java -jar <path-to-moco-runner> http -p <monitor-port> -g <configuration-file>
  • path-to-moco-runner:jar The path of the package
  • monitor-port:http The port the service listens on
  • configuration-file: Profile path
Mock A single request
  • Create a json file , be known as login.json, The contents are as follows :

    [
    {
    “description”: “ Sign in ”,
    “request”: {
    “uri”: “/login”
    },
    “response”: {
    “text”:“ Login successful ”
    }

    }
    

    ]

  • start-up Mock service

    java -jar moco-runner-1.1.0-standalone.jar http -p 8082 -c login.json

 Insert picture description here

  • stay Postman Send a request :http://localhost:8082/login
     Insert picture description here
Mock Multiple requests
  • Creating a json file , be known as home.json, The contents are as follows :

    [
    {
    “description”: “ home page ”,
    “request”: {
    “uri”: “/home”
    },
    “response”: {
    “text”:“ Enter the home page ”
    }

    }
    

    ]

  • Create a request management profile , be known as config.json, The contents are as follows :

    [
    {“include”: “login.json”},
    {“include”: “home.json”}
    ]

  • start-up Mock service

    java -jar moco-runner-1.1.0-standalone.jar http -p 8082 -g config.json

 Insert picture description here

  • stay Postman Send a request :http://localhost:8082/login and http://localhost:8082/home
     Insert picture description here
Common parameter configuration
  • Request mode , adopt method Parameters are defined

    [
    {
    “description”: “ Sign in ”,
    “request”: {
    “uri”: “/login”,
    “method”: “post”
    },
    “response”: {
    “text”: “ Login successful ”
    }
    }
    ]

  • Request parameters , adopt queries Parameters are defined

    [
    {
    “description”: “ Sign in ”,
    “request”: {
    “uri”: “/login”,
    “method”: “post”,
    “queries”: {
    “username”: “king”,
    “pwd”: 123456
    }
    },
    “response”: {
    “text”: “ Login successful ”
    }
    }
    ]

  • Request header , adopt headers Parameters are defined

    [
    {
    “description”: “ Sign in ”,
    “request”: {
    “uri”: “/login”,
    “method”: “post”,
    “headers”: {
    “appId”: “4275CCE0-CE16-4BCC-9AF5-31CA133AC9AD”
    }
    },
    “response”: {
    “text”: “ Login successful ”
    }
    }
    ]

  • Form request body , adopt forms Parameters are defined

    [
    {
    “description”: “ Sign in ”,
    “request”: {
    “uri”: “/login”,
    “method”: “post”,
    “forms”: {
    “username”: “king”,
    “password”: 123456
    }
    },
    “response”: {
    “text”: “ Login successful ”
    }
    }
    ]

  • JSON Request body , adopt json Parameters are defined

    [
    {
    “description”: “ Sign in ”,
    “request”: {
    “uri”: “/login”,
    “method”: “post”,
    “headers”: {
    “Content-Type”: “application/json”
    },
    “json”: {
    “username”: “king”,
    “password”: 123456
    }
    },
    “response”: {
    “text”: “ Login successful ”
    }
    }
    ]

  • HTTP Response status code , adopt status Parameters are defined

    [
    {
    “description”: “ Sign in ”,
    “request”: {
    “uri”: “/login”,
    “method”: “post”
    },
    “response”: {
    “status”: 500,
    “text”: “ Server exception !”
    }
    }
    ]

  • JSON The response data , adopt json Parameters are defined

    [
    {
    “description”: “ Sign in ”,
    “request”: {
    “uri”: “/login”,
    “method”: “post”
    },
    “response”: {
    “headers”: {
    “Content-Type”: “application/json;charset=UTF-8”
    },
    “json”: {
    “code”: “0”,
    “msg”: “ Successful operation ”,
    “data”: {
    “userID”: 3175896456,
    “token”: “17e6ad42cff592-0dfc3f6fa02aee-c343365-1fa400-17e6ad42d00891”
    }
    }
    }
    }
    ]

Self development Mock platform

A little

Use Mock Be careful

  • When put mock After the interface is replaced with the actual interface , The test must also be repeated
  • The test is over , Before going online, make sure , in order to mock And do the relevant code / Modification of configuration file

The above content is purely personal understanding , If there is any deficiency , Welcome to correct , Reprint please indicate the source !

If you think the article is good , Welcome to WeChat official account. , The official account of WeChat is regularly pushing the relevant test technology articles.

《 Front end interview questions of first-line large factories + Develop learning notes + The latest architecture explanation video + Handout of the event station project 》
【https://docs.qq.com/doc/DQVJzbnRIWWNtcVR4】 Full content open source sharing

原网站

版权声明
本文为[m0_ sixty-one million eighty-three thousand four hundred and ni]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202221215476317.html