Skip to content
This repository has been archived by the owner on Nov 23, 2021. It is now read-only.

K6 JSON handleSummary() #124

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"ecmaVersion": 8
},
"rules": {
"semi": "error",
"semi": [2, "always"],
"space-before-function-paren": ["error", "never"]
}
}
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,19 @@ Pass [K6 parameter options](https://k6.io/docs/javascript-api/k6-http/params) as
$ postman-to-k6 collection.json --k6-params k6-params.json -o k6-script.js
```

### K6 Handle Summary as JSON

Output the [K6 summary](https://k6.io/docs/results-visualization/end-of-test-summary/#handlesummary-callback) as a file in JSON format.
This will add the K6 `handleSummary(data)` to the generated script, providing the functionality that K6 will store the summary output as JSON file locally.

| Flag | Verbose | Default |
| ---- | -------------------------- | ------- |
| | `--k6-handle-summary-json` | N/A |

```shell
$ postman-to-k6 collection.json --k6-handle-summary-json summary-report.json -o k6-script.js
```

### Separate

Split requests into separate files, for easier rearrangement of the logic.
Expand Down
4 changes: 4 additions & 0 deletions bin/postman-to-k6.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ program
.option('--oauth1-version <value>', 'OAuth1 version.')
.option('--oauth1-realm <value>', 'OAuth1 realm.')
.option('-s, --separate', 'Generate a separate file for each request.')
.option('--k6-handle-summary-json <path>', 'Output the K6 handle summary as a JSON file.')
.action(run)
.parse(process.argv);

Expand Down Expand Up @@ -104,6 +105,9 @@ function translateOptions(options) {
pre: options.skipPre,
post: options.skipPost,
},
k6HandleSummary: {
json: options.k6HandleSummaryJson
},
};
}

Expand Down
5 changes: 5 additions & 0 deletions lib/convert/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const Globals = require('../generate/Globals');
const postman = require('postman-collection');
const render = require('../render');
const separate = require('../generate/separate');
const Handlesummary = require('../generate/HandleSummary');
const transformer = require('postman-collection-transformer');

async function convertObject(collection, options = {}) {
Expand Down Expand Up @@ -49,6 +50,9 @@ async function convertObject(collection, options = {}) {
if (options.separate) {
separate(node, result);
}
if (options.k6HandleSummary) {
Handlesummary(options.k6HandleSummary, result);
}
return render(result);
}

Expand All @@ -66,6 +70,7 @@ const upgradeOptions = {
outputVersion: '2.1.0',
retainIds: true,
};

function upgrade(collection) {
return new Promise((resolve, reject) => {
transformer.convert(collection, upgradeOptions, (error, result) => {
Expand Down
22 changes: 22 additions & 0 deletions lib/generate/HandleSummary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function HandleSummary(k6HandleSummaryOptions, result) {
result.handleSummary = [];
if (k6HandleSummaryOptions.json) {
injectJsonHandleSummary(k6HandleSummaryOptions.json, result);
}
if (k6HandleSummaryOptions.junit) {
injectJunitHandleSummary(k6HandleSummaryOptions.junit, result);
}
}

function injectJsonHandleSummary(jsonPath, result) {
const jsonOut = `"${jsonPath}": JSON.stringify(data)`;
result.handleSummary.push(jsonOut);
}

function injectJunitHandleSummary(junitPath, result) {
result.imports.set('jUnit', { base: './libs/shim/handleSummary.js' });
const junitOut = `"${junitPath}": jUnit(data)`;
result.handleSummary.push(junitOut);
}

module.exports = HandleSummary;
13 changes: 13 additions & 0 deletions lib/render/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function render(result) {
data(result),
initial(result),
files(result),
handleSummary(result),
logic(result),
]
.filter(section => section)
Expand Down Expand Up @@ -194,6 +195,18 @@ function fileLoad(path) {
);`;
}

function handleSummary(result) {
if (result.handleSummary && result.handleSummary.length > 0) {
return `export function handleSummary(data) {
console.log('Preparing the end-of-test summary: ');
return {
${aid.indent(result.handleSummary.join(',\n'))}
}
}
`;
}
}

function logic(result) {
return `export default function() {
${aid.indent(outer(result))}
Expand Down