Create vim function to generate postman tests
Create vim function to generate postman tests — practical walkthrough with examples.
This Vim function generates a JSON array of Postman test cases by iterating over a list of field names and test values. It creates every combination of name and value, producing objects with query parameters, expected HTTP response codes, and descriptions. This is useful for quickly scaffolding negative/boundary tests for API endpoints.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function! CreateTest()
let a = 0
let names = ['name', 'vlan_id', 'subnet', 'mask', 'name_network', 'description']
let values = ['"*"', 'true', '""', '"some_string"', '0', '-1', '100000', '3.543']
put='['
for n in names
for v in values
let a +=1
if matchstr(v, '""') == '""'
let _v = substitute(v,'"\+', '', 'g')
elseif matchstr(v, '*') == '*'
let _v = substitute(v,'"\+', '', 'g')
elseif matchstr(v, '"some_string"') == '"some_string"'
let _v = substitute(v,'"\+', '', 'g')
else
let _v = v
endif
put='{\"_query_key\": \"'.n.'\",
\ \"_query_value\": '.v.',
\ \"_response\": 200,
\ \"_description\": \"Invalid search string for '.n.' with value of: '._v.'\",
\ \"_reason\": \"\",
\ \"_iteration\": '.a.'}, '
endfor
endfor
put=']'
endfunction
This post is licensed under CC BY 4.0 by the author.