Package: pyclinic
Welcome to the PyClinicPyClinic is a library to make it easier and faster to get your Service Testing up and running!PyClinic currently supports and auto-generates functions for:Postman CollectionsSwagger2 Spec FilesOpenAPI3 Spec Files💡 This allows you to quickly write automation to work with many endpoints or even write automated tests against those endpoints!Table of ContentsQuickstartOpenApi3 Demo (Swagger 2 is identical)In-Depth Postman ExampleAutomated Test ExampleWorking with Postman VariablesSetup and ContributeQuickstartExport your Collection from Postman (asexample.postman_collection.json, for example)Install PyClinic with your preferred Package ManagerpipinstallpyclinicMake an instance ofPostmanand pass in the file path to your JSON file.💡 You will see the instance commonly referred to asrunnerfrompyclinic.postmanimportPostmanrunner=Postman("example.postman_collection.json")Then call the endpoint function and do something with the response!response=runner.Pets.list_all_pets()assertresponse.okprint(response.json())The process is the same for a Swagger2 or OpenApi3 Spec File, but you use theSwaggerorOpenApiclass insteadfrompyclinic.openapiimportOpenApirunner=OpenApi("path-to-spec-file.yml")response=runner.Pets.list_all_pets()assertresponse.okprint(response.json())OpenApi3 Demo Notebook (Swagger2 experience is identical)Open ourdemo_openapi3_runner.ipynbfor a Jupyter Notebook experience of how to use PyClinic.In-Depth Postman ExampleOpen ourdemo_postman_runner.ipynbfor a Jupyter Notebook experience of how to use PyClinic.When you instantiatePostman(), it converts the Postman Collection JSON and turns each request into an executable function.Take thisDeck of Cards API Collectionexample. Here is what the folder structure looks like in Postman:Root↪️ Create shuffled deck📂 Folder 1↪ Reshuffle Deck📂 Folder 1.1↪️ Draw Cards📂 Folder 2↪️ List Cards in PilesMake an instance of Postmanfrompyclinic.postmanimportPostmanrunner=Postman("deckofcards.postman_collection.json")To call theCreate shuffle deckfunction at the Collection Root, you would useresponse=runner.Root.create_shuffled_deck()Then do what you need with the Response!💡 pyclinic uses therequestslibrary to make requests and to work with responses!assertresponse.okprint(response.json())""" Output:{"success": true,"deck_id": "3p40paa87x90","shuffled": true,"remaining": 52}"""If you want to call theDraw Cardsitem underFolder 1 > Folder 1.1, then use:response=runner.Folder11.draw_cards()💡 All folders in the Postman Collection are flattened, so you don't have to dorunner.Folder1.Folder11.draw_cards()You can see all folders and functions that can be used with theshow_foldersfunctionrunner.show_folders()# or use .help() to see which functions belong to a folderrunner.Folder1.help()Folder Names and Function Names are normalizedObserve how, in the last example withrunner.Folder11.draw_cards(), each Postman item name is turned into Pythonic syntax:Folders are turned into classes, soFolder 1turns intoFolder1Requests are turned into functions, soDraw Cardsturns intodraw_cardsAutomated Test Exampledeftest_deckofcards_multiple_calls():runner=Postman("deckofcards.postman_collection.json")create_response=runner.Root.create_shuffled_deck()deck_id=create_response.json().get("deck_id")response=runner.Folder11.draw_cards({"deck_id":deck_id})assertresponse.okassertlen(response.json()["cards"])==2,"Should draw two cards from deck"Working with Postman VariablesPostman has 3 layers of Variables, but we've added a 4th:GlobalEnvironmentCollectionUserCollection Variablescome as part of your collection when you export it. However,GlobalandEnvironmentvariables must be exported separately.When instantiating a Postman runner, you can pass in the paths to these exported Variables files to include them.deftest_runner_show_variables():user_variables={"USERNAME":"Carlos Kidman","SHOW":"ME THE MONEY"}runner=Postman(COLLECTION_PATH,ENV_PATH,GLOBAL_PATH,user_variables)runner.show_variables()""" Output:{'NAME': {'value': 'CARLOS KIDMAN', 'enabled': True},'BASE_URL': {'value': 'https://demoqa.com', 'enabled': True},'USER_ID': {'value': '', 'enabled': True},'USERNAME': {'value': 'Carlos Kidman', 'enabled': True},'PASSWORD': {'value': '', 'enabled': True},'TOKEN': {'value': '', 'enabled': True},'SHOW': {'value': 'ME THE MONEY', 'enabled': True}}"""NOTE: User Variables are defined as a flat dictionary with the key-value pairs you want. These will override values if they already exist, or add them if they don't.You can use the.show_variables()method to display the variables that the Postman runner has been instantiated with.Finally, you can use the.help()method on the executable function to see the raw request dictionary that is being used. This is extremely helpful when needing more details about the request function you're dealing with.runner=Postman("bookstore.postman_collection.json")runner.Account.create_user.help()""" Output:{'method': 'POST','url': 'https://demoqa.com/Account/v1/User','data': {'userName': '{{$randomFullName}}', 'password': 'P@$$w0rd'},'headers': {}}"""TIPSInstantiate the Postman runner at the top of the file and use the show methods to display info. This way you can see the folders and the variables that you have at the beginning of the program or test run:frompyclinic.postmanimportPostmanrunner=Postman(BOOKSTORE_PATH,BOOKSTORE_ENV_VARIABLES_PATH)runner.show_folders()runner.show_variables()defcreate_user(credentials:Dict)->Dict:response=runner.Account.create_user(data=credentials)returnresponse.json()...Setup and Contribute💡 UsePoetryas the package manager to take advantage of thepyproject.tomlat the Workspace Root⚠️ Python version 3.7 or higher is requiredClone/Fork this repo and open it in your favorite editor (VS Code, Pycharm, etc)Open the Integrated Terminal and use Poetry to install all dependencies# this also creates the virtual environment automaticallypoetryinstallConfigure your IDESelect Interpreter - Gives you autocomplete, intellisense, etcConfigure Tests - We usepytestinstead of the defaultunittestlibraryAny other settings. This project uses a Formatter (black) and Linter (flake8)That's it! Run the tests to see it all workpoetryrunpoetestMake your changes, then submit a Pull Request (PR) for review. This automatically triggers a pipeline that lints and runs tests. Once the pipeline is green, aMaintainerwill review your PR! 😄Twitch Shoutouts 💪🏽🐍I have been building this entirely while streaming on Twitch!
Come check it out every weekday at 12:00pm MST athttps://twitch.tv/carloskidman@sudomaze- amazing feedback, humor, and searching skills@vernkofford- OG subscriber and friend