A python library to test services like RESTful APIs
Project description
Welcome to the PyClinic
Pyclinic is a library to make it easier and faster to get your Service Testing up and running!
Currently, PyClinic can integrate with Postman users so you can export a Postman Collection and use it to automatically generate python functions!
You can also genereate Pydantic Models by using the CLI:
pyclinic generate-models --input <postman_collection_path>
💡 This allows you to quickly write automation to work with many endpoints or even write automated tests against those endpoints!
Simple Example
-
Export your Postman Collection (as
example.postman_collection.json
, for example) -
Make an instance of
Postman
and pass in the file path to your JSON filefrom pyclinic.postman import Postman runner = Postman("example.postman_collection.json")
-
Then call the endpoint function!
runner.Pets.list_all_pets()
In-depth Example
When you instantiate Postman()
, it converts the Postman Collection JSON and turns each request to an executable function!
Take this Deck of Cards API Collection example. 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 Piles
-
Make an instance of Postman
from pyclinic.postman import Postman runner = Postman("deckofcards.postman_collection.json")
-
To call the
Create shuffle deck
endpoint at the Collection Root, you would useresponse = runner.Root.create_shuffled_deck()
-
Then do what you need with the Response!
💡 pyclinic uses the
requests
library to make requests and to work with responses!assert response.ok print(response.json()) """ Output: { "success": true, "deck_id": "3p40paa87x90", "shuffled": true, "remaining": 52 } """
-
If you want to call the
Draw Cards
item 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 do
runner.Folder1.Folder11.draw_cards()
Normalizing Folder Names and Function Names
Observe how, in the last example with runner.Folder11.draw_cards()
, each Postman item name is turned into Pythonic syntax:
- Folders are turned into classes, so
Folder 1
turns intoFolder1
- Requests are turned into functions, so
Draw Cards
turns intodraw_cards
Work with them like normal functions
def test_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})
assert response.ok
assert len(response.json()["cards"]) == 2, "Should draw two cards from deck"
Setup and Contribute
💡 Use Poetry
as the package manager to take advantage of the pyproject.toml
at the Workspace Root
⚠️ Python version 3.9 or higher is required
-
Clone/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 automatically poetry install
-
Configure your IDE
- Select Interpreter - Gives you autocomplete, intellisense, etc
- Configure Tests - We use
pytest
instead of the defaultunittest
library - Any other settings. This project uses a Formatter (
black
) and Linter (flake8
)
-
That's it! Run the tests to see it all work
poetry run poe test
-
Make 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, a Maintainer will review your PR! 😄
Shoutout to @sudomaze from Twitch 💪🏽🐍
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pyclinic-0.1.3.tar.gz
.
File metadata
- Download URL: pyclinic-0.1.3.tar.gz
- Upload date:
- Size: 11.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.5 CPython/3.9.1 Darwin/20.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2425b787449ff3dd4340dfbf11298036734ce0168b29e26cf693d97fa64062f3 |
|
MD5 | d699ac7d5c2419ecad2ced0c1ac6f20a |
|
BLAKE2b-256 | 4b4fed3b2c2b2dc2a1eb7947fcc07aeb96dc2f01878b5866d482098bee685500 |
File details
Details for the file pyclinic-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: pyclinic-0.1.3-py3-none-any.whl
- Upload date:
- Size: 11.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.5 CPython/3.9.1 Darwin/20.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 16788070cb233653461a5f7b9ef3a9fe1aa896d69220978c45d94a8ed9f193de |
|
MD5 | a788c29cd4a233114eec6fd3a5fd48d8 |
|
BLAKE2b-256 | db064b00cf777446b0b3c61e46e54263ea3ffc57036300396c21cfb0e5e97591 |