Closed LouisZou closed 8 years ago
That's meaning, Now I load a json file but I don't know the real Id or path in this json. I need to get the list from our swaggerAPP.op and then test them one by one.
Hi, I'm curious that how could you test API without knowing their definition in advance? Even if I provide a way for you to iterate all APIs by giving you IDs, how could you test them after that step?
Thanks mission-liao, in my test case, the details is as the following:
so that I can iterate the APis (app.ops) and then test it with the test data.
I think I can also test them from test data, such as :
is this OK?
But in this way, I think if I lost some data of api in test data, I cannot test all of the Apis in definition. The purpose is testing all of the apis in definition. :)
I think you can write a scanner to collect all those IDs on you own, the app.op is a little bit more complicated than what you need (to be compatible with Swagger 1.2).
Here is a pseudo code(not tested) to collect all Operation objects in pyswagger, similar to what we did in pyswagger.scanner.TypeReduce:
from pyswagger import SwaggerApp
from pyswagger.scan import Scanner, Dispatcher
from pyswagger.spec.v2_0.objects import Operation
# define your scanner
class OperationCollector(object):
class Disp(Dispatcher): pass
def __init__(self):
self.cached_obj = []
@Disp.register([Operation])
def _op(self, path, obj, _):
self.cached_obj.append(obj)
# load your definition
app = SwaggerApp.create("path_to_your_definition")
s = Scanner(app)
coll = OperationCollector()
s.scan(root=app.root, route=[coll])
# access all Operation objects, no matter with IDs or not
for v in coll.cached_obj:
pass
Thanks mission-liao. I can iterate the op list by the method above. but I don't know the op is which one (related to Id or other information) I think I can also get the list by the following method: for ops in app.op.keys(): print ops print app.op[ops] the ops is "Tag!##!Id", maybe I can analyze it and get the correct Id. or I will get the Ids from test data (data-driven test) and assure I have all of the test data according to the Ids in definition.
Thanks again for you kindly reply.
Hi, did you tried the scanner way?
if you need the ID of Operation, just cache them ( a modified version of previous pesudo code ):
from pyswagger import SwaggerApp
from pyswagger.scan import Scanner, Dispatcher
from pyswagger.spec.v2_0.objects import Operation
# define your scanner
class OperationCollector(object):
class Disp(Dispatcher): pass
def __init__(self):
self.cached_obj = {} # <- a dict
@Disp.register([Operation])
def _op(self, path, obj, _):
self.cached_obj[obj.operationId] = obj # cache Operation, indexed by operationId
# load your definition
app = SwaggerApp.create("path_to_your_definition")
s = Scanner(app)
coll = OperationCollector()
s.scan(root=app.root, route=[coll])
# access all Operation objects, no matter with IDs or not
for opId, op in coll.cached_obj: # <- should be simple enough
pass
Thanks mission-liao. Yes. I have tried to use scanner just like the sample that you provided. and I can get the Ids. but for the sample code #2 , when I run it, there is a error: ValueError: too many values to unpack If I fix the line "for opId, op in coll.cached_obj:" to "for opId in coll.cached_obj:", it work well.
Glad those pseudo code works.
Close this issue for reducing monitoring effort, reopen it if anything goes wrong.
I try to get the apis with app.op.keys(), but it return "path!##!Id". I only want to get the ID.
And How can iterate all of the path and then get the supported method in it. is there a sample or guide to do this?