-
Notifications
You must be signed in to change notification settings - Fork 58
mock test data generation #115
Comments
@Maples7 your first comment:
I understand the sentiment and it has merit. The issue is that this creates "inter-test dependencies." Meaning that one or more test depends on the others succeeding. This isn't something that we want :) These are unit tests, and as such, need to be tested independently of each other. @mm-gmbd that last sentence there ^ also kind of applies to what you were implying if I understood correctly. But you acknowledged that in your comment. I would still say that using the "well-known" |
I just present a tiny thought about the solution for the "well-known" Of course, that's just a proposal, the developer of this function makes the final decision. 🍒 |
@noahdietz / @Maples7 / @Remco75 - I've got a little time to flesh out my idea now, so here goes... In Swagger, we define request schemas and response schemas. Using the paths:
users/{userID}/firstName:
put:
summary: Change User First Name
Description: A longer description
parameters:
- name: body
in: body
schema:
type: object
properties:
newName:
type: string
minLength: 2
maxLength: 100
required: ['newName']
additionalProperties: false
responses:
200:
description: Name successfully changed
400:
description: Name unsuccessfully changed because `newName` was invalid
schema:
type: object
properties:
error:
type: string
required: ['error']
additionalProperties: false
Notice that, in the API itself, we do not define the exact value for Also, notice the same thing for the response STT ensures that the schema provided in the API matches the schema of the data in the response -- note that it compares response schema, not response data, because the data is not something that one would include in the API definition:
As far as using a "well-known" The worst that could happen is that, the user provides something like a known |
@mm-gmbd ,cool idea's. I will look into it somewhere the coming 2 weeks (srry, kinda busy). As far as data generation goes: let's look into other modules already doing this to see if we can use that as dependency and join forces there. |
@Remco75 -- that is part of the basis of this discussion. If you take a look at the discussion in #107, I suggested utilizing json-schema-test-data-generator. I am already doing this today in a gulp task, but it could be easily pulled into this project: var swagger = //some swagger
var config = { ... }
var pathsArr = [];
for (path in swagger["paths"]) {
pathsArr.push(path);
}
//NOTE: Looper so we don't exceed call stack... it may actually not be required because the error MAY have been happening by the "generator" trying to generate a buttload of tests because the schema for some operations was just "type: object"
function looper(x, completeFunc) {
if (x < pathsArr.length) {
var path = pathsArr[x];
// console.log("Checking path "+path);
for (operation in swagger["paths"][path]) {
if (swagger["paths"][path][operation].parameters) {
for (var i = 0; i < swagger["paths"][path][operation].parameters.length; i++) {
if (swagger["paths"][path][operation].parameters[i].in && swagger["paths"][path][operation].parameters[i].in == 'body' && swagger["paths"][path][operation].parameters[i].schema) {
var jsonData = generate(swagger["paths"][path][operation].parameters[i].schema);
if (!config.inputTesting) config.inputTesting = {};
if (!config.inputTesting[path]) config.inputTesting[path] = {};
if (!config.inputTesting[path][operation]) config.inputTesting[path][operation] = {};
if (!config.inputTesting[path][operation]["200"]) config.inputTesting[path][operation]["200"] = [];
if (!config.inputTesting[path][operation]["400"]) config.inputTesting[path][operation]["400"] = [];
for (var j = 0; j < jsonData.length; j++) {
if (jsonData[j].valid == true) {
config.inputTesting[path][operation]["200"].push({"body": jsonData[j]});
} else {
config.inputTesting[path][operation]["400"].push({"body": jsonData[j]});
}
}
}
}
}
}
setTimeout(function(){
looper(x+1, completeFunc);
}, 0);
} else {
completeFunc();
}
}
looper(0, function(){
// Generates an array of JavaScript test files following specified configuration
var tests = stt.testGen(swagger, config);
console.log("Creating "+tests.length+" tests...")
for (var i = 0; i < tests.length; i++){
fs.writeFileSync("./tests/"+tests[i].name, tests[i].test, 'utf8');
}
}) |
Just another thought, would be cool to use the example property ( if present ) to fill the mock data. This snippet is derived from the pet-schema: Carefull documentation of your API would then lead to comprehensive mocks! |
This issue is open to discuss the viability of generating mock data for STT generated unit tests. The discussion is being being continued here from #107, so refer to there if you are catching up.
The text was updated successfully, but these errors were encountered: