Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Run Pytest and Publish Report

on:
push:
branches: [ main ]
pull_request:

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout repo
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.11"

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install requests pytest pyhamcrest jsonschema pytest-html flask_restx flask

- name: Run tests and generate report
run: pytest -v --html=report.html

- name: Publish report to GitHub Pages
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_branch: gh-pages
publish_dir: .
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Pytest Report:
https://bradevansqa.github.io/pytest-api-example/report.html

# Sample API Test Using Pytest and SwaggerUI

## System Requirements
Expand Down Expand Up @@ -47,4 +50,4 @@ pytest -v --html=report.html
### Tasks
- [ ] Extend and fix the 3 tests from [test_pet.py](test_pet.py#1). There are TODO instructions for each test listed in the file
- [ ] Create the PATCH test for [test_store.py](test_store.py#1). There are TODO instructions for test along with optional tasks
- [ ] Take note of any bugs you may have found
- [ ] Take note of any bugs you may have found
2 changes: 1 addition & 1 deletion schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "integer"
},
"name": {
"type": "integer"
"type": "string"
},
"type": {
"type": "string",
Expand Down
29 changes: 25 additions & 4 deletions test_pet.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,42 @@ def test_pet_schema():
3) Validate the 'status' property in the response is equal to the expected status
4) Validate the schema for each object in the response
'''
@pytest.mark.parametrize("status", [("available")])
@pytest.mark.parametrize("status", ["available", "pending", "sold"])
def test_find_by_status_200(status):
test_endpoint = "/pets/findByStatus"
params = {
"status": status
}

response = api_helpers.get_api_data(test_endpoint, params)

assert response.status_code == 200

pets = response.json()

for pet in pets:
assert_that(pet['status'], is_(status))
validate(instance=pet, schema=schemas.pet)

# TODO...

'''
TODO: Finish this test by...
1) Testing and validating the appropriate 404 response for /pets/{pet_id}
2) Parameterizing the test for any edge cases
'''
def test_get_by_id_404():
# TODO...
pass
@pytest.mark.parametrize("pet_id", [895231, 99990, 123456789])
def test_get_by_id_404(pet_id):
test_endpoint = f"/pets/{pet_id}"


response = api_helpers.get_api_data(test_endpoint)

assert response.status_code == 404



body = response.json()
assert_that(body['message'], contains_string("not found"))


33 changes: 32 additions & 1 deletion test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,35 @@
4) Validate the response message "Order and pet status updated successfully"
'''
def test_patch_order_by_id():
pass
create_endpoint = "/store/order"
create_payload = {
"pet_id": 2
}

create_response = api_helpers.post_api_data(create_endpoint, create_payload)
assert create_response.status_code == 201


order_id = create_response.json()["id"]


patch_endpoint = f"/store/order/{order_id}"
patch_payload = {
"status": "sold"
}

response = api_helpers.patch_api_data(patch_endpoint, patch_payload)

assert response.status_code == 200
body = response.json()
assert "Order and pet status updated successfully" in body["message"]

reset_payload = {"status": "available"}
reset_response = api_helpers.patch_api_data(patch_endpoint, reset_payload)