Package: python-apitist
DEPRECATEDPlease useapitistpackagepython-apitistBrand new way to test your APIInstallationRun the following command in your command line::pipinstallpython-apitistDefault hooksRequestDebugLoggingHookRequestInfoLoggingHookPrepRequestDebugLoggingHookPrepRequestInfoLoggingHookResponseDebugLoggingHookResponseInfoLoggingHookExample usagefromapitist.hooksimportPrepRequestInfoLoggingHook,ResponseInfoLoggingHookfromapitist.requestsimportsessions=session()PrepRequestInfoLoggingHook.formatter="Best formatter{req.method}{req.url}"s.add_prep_request_hook(PrepRequestInfoLoggingHook)s.add_response_hook(ResponseInfoLoggingHook)s.post("https://httpbin.org/post",params={"q":"test"})Custom HooksfromrequestsimportRequest,PreparedRequest,Responsefromapitist.requestsimportsession,RequestHook,PreparedRequestHook,ResponseHooks=session()classReqHook(RequestHook):defrun(self,request:Request)->Request:print(request.url)returnrequestclassPrepReqHook(PreparedRequestHook):defrun(self,request:PreparedRequest)->PreparedRequest:print(request.url)returnrequestclassRespHook(ResponseHook):defrun(self,response:Response)->Response:print(response.url)returnresponses.add_request_hook(ReqHook)s.add_prep_request_hook(PrepReqHook)s.add_response_hook(RespHook)s.get("https://ya.ru",params={"q":"test"})Working with constructorimportattrimporttypingfromapitist.constructorimportconverterfromapitist.hooksimportRequestConverterHook,ResponseConverterHookfromapitist.requestsimportsessionclassExampleType:test=None@attr.sclassExampleStructure:test:ExampleType=attr.ib()@attr.sclassTestResponse:args:typing.Dict=attr.ib()data:str=attr.ib()files:typing.Dict=attr.ib()form:typing.Dict=attr.ib()headers:typing.Dict=attr.ib()json:ExampleStructure=attr.ib()origin:str=attr.ib()url:str=attr.ib()s=session()s.add_hook(RequestConverterHook)s.add_hook(ResponseConverterHook)defstructure_example_type(data,type_):example=ExampleType()example.test=datareturnexampledefunstructure_example_type(data):returndata.testconverter.register_hooks(ExampleType,structure_example_type,unstructure_example_type)t=ExampleType()t.test="test"struc=ExampleStructure(t)res=s.post("https://httpbin.org/post",data=struc).structure(TestResponse)print(res.structured.json.test.test)# testUsing random data generatorFirst of all create an instance of random class:fromapitist.randomimportRandomerrand=Randomer()Now, you can add custom hooks for different types:rand.add_type(str,lambda:str(random.random()))rand.add_type(float,lambda:random.random())Or usingadd_types:types={str:lambda:str(random.random()),float:lambda:random.random()}rand.add_types(types)Now you can create random object for given type or any attrs class with
defined types:importattrimporttypingrand.object(str)# '0.6147789314561384'rand.object(float)# 0.4664297665239271@attr.sclassData:value1:str=attr.ib()value2:typing.List[str]=attr.ib()value3:typing.Tuple[float]=attr.ib()print(rand.object(Data))# Data(#   value1='0.491058956716827',#   value2=['0.6568036485871975'],#   value3=(0.8603579349502298,)# )It is better to use it withFaker.
Just define different subclasses forstrand add different hooks for them.
By this you could create different data for differentstrfields.Also, using withRequestConverterHookandResponseConverterHookyou could easily create random json objects which would be send to server.