问题
I'm aware it is possible to generate code snippets from individual Postman requests, but I can't find similar functionality for a entire collection.
I also know that there are tools like newman for running an exported collection in Postman Collection 2.1 json format.
But specifically what I'm looking for is a tool that generates bash
code from a collection or from a exported collection in Postman Collection 2.1 json format. This way my co-workers who don't use Postman can replicate the API requests.
Is this option available in Postman and I'm just missing it or are there any tools that do this?
回答1:
I'm not sure I understand your question completely. But I can tell you how I export collection and then run tests.
- Export Collection to JSON: choose tab 'Collections' at the left tab of the Postman. Select the 3-dot menu and choose 'Export'.
- Export your test environment: Open 'Manage Environments'. Click download.
- I don't know a tool that generates bash code, I write it manually.
Write .sh file like the following to run your tests.
"newman run your_test_collection.json -r cli -e your_test_environment.json --reporter-cli-no-assertions --global-var"
Usually, I create different .sh files for different sets of tests.
回答2:
I ended using the Postman SDK and postman-code-gen to create code snippets from Postman Collections that I exported from Postman.
Not really sure why this feature isn't provided already by Postman, but I made a simple CLI tool that does it: https://github.com/arashout/postman-collection-gen
# cURL request generation
node main.js -c example_collection.json
# curl --location --request GET 'https://v7rr12wbr7.execute-api.us-west-2.amazonaws.com/prod/courses?c0=PHYS153&c1=APSC160&c2=CHEM154&version_key=1.2'
# curl --location --request GET 'https://v7rr12wbr7.execute-api.us-west-2.amazonaws.com/prod/courses?c0=PHYS153'
# How to generate other languages:
node main.js -c example_collection.json -l shell,httpie
node main.js -c example_collection.json -l Swift,URLSession
来源:https://stackoverflow.com/questions/60514286/how-to-generate-code-using-postman-collection