Giter Site home page Giter Site logo

tamalnayek / springboot-openapi-postman Goto Github PK

View Code? Open in Web Editor NEW

This project forked from csieflyman/springboot-openapi-postman

0.0 1.0 0.0 162 KB

Convert Spring Boot Controller to Postman Collection for REST API Automated Testing

License: MIT License

JavaScript 10.60% HTML 0.09% Java 89.31%

springboot-openapi-postman's Introduction

spring boot-openapi-postman

  • Convert spring boot controllers to a postman collection automatically to reduce effort in writing API test and ensure test cases are up-to-date.
  • Integrate gradle with Node.js script provider a convenient way to run test for developers
  • Run newman for each folder of the collection in parallel to reduce execution time.
  • You can control more fine-grained testing behaviors by passing postman test data files to change postman's default behaviors
    • You can specify which one request you want to execute, rather than all requests of the collection (Postman execute all requests of a collection in declared order for each test data. It's inappropriate in most situations)
    • You can run test with varying request body and script assertion for each test data (Postman run the same script assertion which be written in the request for all test data. It's inappropriate in most situations)
    • You can compose many test data to a test suite for a complicated scenario without duplicating the request. For example, in the create -> get -> update -> get scenario, you have to create two get requests in a collection in postman application, but actually all you need to do is just add two "get" test data in the test data file
  • Developers can upload tested collection to postman server, then sync it with other team members.
  • Testers neither use this tool nor modify collection, they just sync collection with postman application and run test with test data files

Workflow

  1. Developers generate postman collection json file
  2. Developers prepare test data files for simple test cases
  3. Developers run test with this tool in the localhost or dev envrionment
  4. Developers upload collection to postman server if test has passed
  5. Testers prepare test data files for complicated test cases
  6. Testers use postman application to run test with test data files

Usage

  1. Run gradle task "myNpmInstall" to install Node.js packages
  2. Run gradle task "generatePostmanCollection" to generate collection json file
    • GeneratePostmanCollection task = generateSwaggerDocumentation task + swaggerToPostmanCollection task
    • Note: Because this example is a gradle multi-project. If you get an ClassNotFound error in library project, you can run it again successfully while library jar file is exist after first run.
  3. Prepare postman files
    • Edit "/postman/globals.json" if you want to define global environment variables.
    • Put your environment json file "${envName}-env.json" info "/postman/env" directory
    • Put your test data json files "${folderName}-data.json" into "/postman/folder" directory
    • Put your test data json files "${suiteName}-data.json" into "/postman/suite" directory if you want to run comlicated test cases by executing requests cross folders within the collection.
  4. Run gradle task "newman" to run test and you can checkout test report in "/postman/report" directory
    • You have to assign argument value ${envName}(required), ${folderName}(optional, run all folders if empty)
  5. Run gradle task "uploadToPostmanServer" to upload collection and env to postman server
    • You have to assign envrionment variable "POSTMAN_API_KEY"
    • You can specify the argument "uploadEnv"(boolean) and "uploadCollection"(boolean)

Test data file

  • You have to specify three pre-defined variables value for each test data
    • _requestName(required): The request which you what to execute.
    • _requestBody(optional): The request body of request if need (POST, PUT...etc)
    • _test(required, but can be empty string): The javascript code executed after the request is sent. For example, you can check the response for assertion. We can think of code as data and pass it to postman. When postman run test script pm.iterationData.get("_test");eval(script);, it will get "_test" value and run our script code.
  • Example
[
  {
    "_requestName": "create User",
    "_requestBody": "{\"gender\": \"MALE\",\"age\": 30,\"isMember\": false}",
    "_test": "pm.test('check name required', function(){pm.response.to.have.status(400);});"
  },
  {
    "_requestName": "create User",
    "_requestBody": "{\"name\": \"csieflyman\",\"gender\": \"MALE\",\"age\": 30,\"isMember\": false}",
    "_test": "pm.test('200 ok', function(){pm.response.to.have.status(200);}); pm.test('check userId after create', function(){Number.isInteger(responseBody);}); postman.setEnvironmentVariable(\"userId\", responseBody);"
  },
  {
    "_requestName": "get User",
    "_test": "pm.test('200 ok', function(){pm.response.to.have.status(200);}); pm.test('check username', function(){pm.expect(pm.response.json().name).to.eql('csieflyman');});"
  },
  {
    "_requestName": "update User",
    "_requestBody": "{\"name\": \"csieflyman\",\"gender\": \"MALE\",\"age\": -1,\"isMember\": true}",
    "_test": "pm.test('check age >= 0', function(){pm.response.to.have.status(400);});"
  },
  {
    "_requestName": "update User",
    "_requestBody": "{\"name\": \"csieflyman\",\"gender\": \"MALE\",\"age\": 30,\"isMember\": true}",
    "_test": "pm.test('200 ok', function(){pm.response.to.have.status(200);});"
  },
  {
    "_requestName": "get User",
    "_test": "pm.test('200 ok', function(){pm.response.to.have.status(200);}); pm.test('check data after update', function(){pm.expect(pm.response.json().name).to.eql('csieflyman');pm.expect(pm.response.json().isMember).to.eql(true);});"
  },
  {
    "_requestName": "find Users",
    "_test": "pm.test('200 ok', function(){pm.response.to.have.status(200);});pm.test('check array size', function(){pm.response.json().length === 1});"
  },
  {
    "_requestName": "delete User",
    "_test": "pm.test('200 ok', function(){pm.response.to.have.status(200);});"
  },
  {
    "_requestName": "get User",
    "_test": "pm.test('404 not found', function(){pm.response.to.have.status(404);});"
  }
]

Mock request

  • For specify the request you want to execute rather than all requests of the collection, this tool manipulate the generated collection json with the following modifications
    • Create a mock request in each folder automatically and put it to the first position.
    • Add postman.setNextRequest(pm.iterationData.get("_requestName")); in the Pre-request Script of the mock request, then postman will jump to your specified request and execute it.
    • Add postman.setNextRequest(null); in the Test Script of each request, then postman will stop current test iteration after executing request
    • You can send a GET mock request and return 200 OK simply, then ignore the test result.

This project use the following APIs and tools...

springboot-openapi-postman's People

Contributors

csieflyman avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.