diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..2a06027 --- /dev/null +++ b/.github/workflows/tests.yml @@ -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: . diff --git a/README.md b/README.md index 339ce55..8c09a21 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ +Pytest Report: +https://bradevansqa.github.io/pytest-api-example/report.html + # Sample API Test Using Pytest and SwaggerUI ## System Requirements @@ -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 \ No newline at end of file +- [ ] Take note of any bugs you may have found diff --git a/schemas.py b/schemas.py index 946cb6c..760be60 100644 --- a/schemas.py +++ b/schemas.py @@ -6,7 +6,7 @@ "type": "integer" }, "name": { - "type": "integer" + "type": "string" }, "type": { "type": "string", diff --git a/test_pet.py b/test_pet.py index e215678..b690c8a 100644 --- a/test_pet.py +++ b/test_pet.py @@ -26,7 +26,7 @@ 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 = { @@ -34,6 +34,15 @@ def test_find_by_status_200(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... ''' @@ -41,6 +50,18 @@ def test_find_by_status_200(status): 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 \ No newline at end of file +@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")) + + \ No newline at end of file diff --git a/test_store.py b/test_store.py index 186bd79..5ea1c26 100644 --- a/test_store.py +++ b/test_store.py @@ -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) + + + + + + +