swagger-test-templates icon indicating copy to clipboard operation
swagger-test-templates copied to clipboard

How to use it

Open spacemonkeyHere opened this issue 7 years ago • 2 comments

Hi, it may be a noob question but how do you use the example on this page ? after installing this npm package and adding the example i just get a weird error ode_modules/swagger-test-templates/templates/default/get/get.handlebars' at Error (native) at Object.fs.openSync (fs.js:640:18) at Object.fs.readFileSync (fs.js:508:33) at testGenResponse (

I am guessing you miss some noob tutorial ? I also tried following https://apigee.com/about/blog/developer/swagger-test-templates-test-your-apis installing all the missing packages but still o do not see how this example is connected to what is in the article. My question is how to i generate these api tests based on the specification of my swagger api with load testing ?

thank you

spacemonkeyHere avatar Jul 11 '17 12:07 spacemonkeyHere

You can use it like so:-

var fs = require("fs"),
    stt = require("swagger-test-templates"),
    swagger = require("swagger.json"),
    config = {
        assertionFormat: "expect",
        testModule: "request",
        pathName: [],
        maxLen: -1
    };

var tests = stt.testGen(swagger, config);

tests.forEach(function(element) {
    fs.writeFile("./test/" + element.name, element.test, (err) => {
        if (err) throw err;

        console.log(element.name, " written");
    });
}, this);

This will parse your swagger.json and create several test files in a directory test. At this point you have a folder of tests that can be run with Mocha. So to run them navigate to test then run the following:-

npm install mocha
npm install chai
npm install request
npm install z-schema
npm install dotenv
mocha

I hope that helps.

baynezy avatar Jul 12 '17 16:07 baynezy

How to run this file var fs = require("fs"), stt = require("swagger-test-templates"), swagger = require("swagger.json"), config = { assertionFormat: "expect", testModule: "request", pathName: [], maxLen: -1 };

var tests = stt.testGen(swagger, config);

tests.forEach(function(element) { fs.writeFile("./test/" + element.name, element.test, (err) => { if (err) throw err;

    console.log(element.name, " written");
});

}, this);

shadowman9443 avatar Nov 07 '18 04:11 shadowman9443