Skip to content
Merged
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
20 changes: 0 additions & 20 deletions .github/workflows/enforce-develop-pr.yml

This file was deleted.

20 changes: 0 additions & 20 deletions .github/workflows/enforce-staging-pr.yml

This file was deleted.

9 changes: 9 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@
"group": "test",
"detail": "Runs all Behave tests and saves JUnit results to the host machine"
},
{
"label": "Docker: Run Smoke Tests",
"type": "shell",
"command": "docker run --rm --env-file .env -v ${workspaceFolder}/reports:/app/reports behave-test test/features -t @smoke --junit --junit-directory /app/reports",
"dependsOn": ["Docker: Build Behave Image"],
"problemMatcher": [],
"group": "test",
"detail": "Runs only Behave tests tagged with @smoke and saves JUnit results to the host machine"
},
{
"label": "Docker: Run Finnhub REST API Test",
"type": "shell",
Expand Down
133 changes: 132 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,132 @@
# testjeff-course-python-behave-api
# 🧪 TestJeff Course: Python Behave API Testing

Welcome to the **TestJeff Course Repository** for mastering **API Testing with Python and Behave**. This repo contains hands-on examples, Docker integration, and complete test automation pipelines for REST, SOAP, GraphQL, WebSocket, and RPC APIs.

---

## 🚀 Course Overview

This repository supports the **TestJeff API Testing Course**. You'll learn how to:
- Structure and write Gherkin feature files
- Create step definitions using Behave
- Test various API protocols: REST, SOAP, GraphQL, WebSocket, and JSON-RPC
- Run tests in Dockerized environments
- Integrate with CI and generate test reports

---

## 📂 Project Structure

```
test/
└── features/
├── environment/ # Global setup and teardown logic
├── steps/ # Step definitions (.py)
├── support/ # Utility modules
└── *.feature # Gherkin feature files
.env # API keys and configuration variables
docker-compose.yml # Optional Docker services
reports/ # Test output (e.g., JUnit, HTML)
```

---

## 🛠️ Prerequisites

- [Docker](https://docs.docker.com/get-docker/)
- [VS Code](https://code.visualstudio.com/)
- Python 3.10+ (if running outside Docker)
- API keys for third-party APIs (Finnhub, etc.)

---

## ⚙️ Setup

### Clone the repo

```bash
git clone https://github.com/testjeff/testjeff-course-python-behave-api.git
cd testjeff-course-python-behave-api
```

### Create a `.env` file

```env
FINNHUB_API_KEY=your_api_key_here
NEWS_API_KEY=your_api_key_here
WEATHER_API_KEY=your_api_key_here
```

### Build Docker image

```bash
docker build -t behave-test .
```

---

## ▶️ Run Tests

### VS Code Tasks

Use the preconfigured VS Code tasks:

- `Docker: Run Welcome Feature Only`
- `Docker: Run Finnhub REST API Test`
- `Docker: Run All Behave Tests`

Or run manually:

```bash
docker run --rm --env-file .env -v ${PWD}/reports:/app/reports behave-test behave test/features
```

---

## 🧪 Sample Feature

```gherkin
Feature: Get stock quote from Finnhub API

Scenario: Successful stock quote fetch for AAPL
Given I have a valid API key
When I request a stock quote for "AAPL"
Then the response status should be 200
And the current price should be a positive number
```

---

## 📘 Course Notes

Refer to the **Module Guide** included with the course to follow along lesson by lesson and get the most out of the provided examples.

---

## 🤖 GitHub Copilot

Try using Copilot to:
- Auto-generate step definitions from feature files
- Extend test coverage for edge cases
- Refactor and improve test logic

---

## 🧑‍💻 Contributing

Pull requests are welcome! If you have ideas for extending this project or submitting test cases, feel free to fork and contribute.

---

## 📜 License

MIT License — see `LICENSE` file for details.

---

## 🔗 Stay Connected

Follow **TestJeff** for more tools, templates, and automation strategies:
- 📺 [YouTube Channel](https://www.youtube.com/@testjeff)
- 🧑‍🏫 [Udemy Courses](https://www.udemy.com/user/testjeff/)
- 🧠 [ChatGPT Plugin (Coming Soon!)]()
26 changes: 24 additions & 2 deletions test/features/ankreth_rpc_api_test.feature
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
Feature: Ethereum RPC Block Number Retrieval

@smoke
Scenario: Validate latest block number response
Given I set the Ethereum RPC endpoint
When I request the latest block number
Then the response status should be 200
Then the response status should be "200"
Then I should get a valid hex response
And the block number should be a positive integer
And the block number should match expected range
And no error should occur during the request
And no error should occur during the request

Scenario: Validate block number retrieval with invalid endpoint
Given I set an invalid Ethereum RPC endpoint
When I request the latest block number
Then the response status should not be "200"
And I should receive an error message indicating the failure
And no valid block number should be returned

Scenario: Validate block number retrieval with malformed response
Given I set the Ethereum RPC endpoint with a malformed response
When I request the latest block number
Then the response status should not be "200"
And I should receive an error message indicating the malformed response
And no valid block number should be returned

Scenario: Validate block number retrieval with network timeout
Given I set the Ethereum RPC endpoint with a network timeout
When I request the latest block number
Then the response status should not be "200"
And I should receive an error message indicating a network timeout
And no valid block number should be returned
20 changes: 20 additions & 0 deletions test/features/bitstamp_websocket_api_test.feature
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
Feature: Bitstamp WebSocket BTC price feed

@smoke
Scenario: Receive real-time BTC/USD trade price
Given the WebSocket connection to Bitstamp is established
When I subscribe to BTC/USD trades
Then I should receive a trade event
And the price should be a valid number

Scenario: Receive real-time BTC/USD order book updates
Given the WebSocket connection to Bitstamp is established
When I subscribe to BTC/USD order book updates
Then I should receive an order book update event
And the order book should contain valid bids and asks

Scenario: Handle WebSocket reconnection
Given the WebSocket connection to Bitstamp is established
When the WebSocket connection is lost
And I wait for reconnection
Then the WebSocket connection should be re-established
And I should still receive BTC/USD trade events

Scenario: Handle invalid messages
Given the WebSocket connection to Bitstamp is established
When I receive an invalid message format
Then I should handle the error gracefully
And no unhandled exceptions should occur
13 changes: 12 additions & 1 deletion test/features/environment.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import os
import glob
from behave import fixture, use_fixture

def before_all(context):
pass
reports_dir = os.path.join(os.getcwd(), "reports")
if os.path.exists(reports_dir):
files = glob.glob(os.path.join(reports_dir, "*"))
for f in files:
try:
os.remove(f)
print(f"🗑️ Removed report file: {os.path.basename(f)}")
except Exception as e:
print(f"⚠️ Could not remove {f}: {e}")
print("📂 Reports directory cleared.")

def after_all(context):
pass
Expand Down
22 changes: 21 additions & 1 deletion test/features/finnhub_rest_api_test.feature
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
Feature: Get stock quote from Finnhub API

@smoke
Scenario: Successful stock quote fetch for AAPL
Given I have a valid API key
When I request a stock quote for "AAPL"
Then the response status should be 200
Then the response status should be "200"
And the current price should be a positive number
And the response should contain required quote fields

Scenario: Unsuccessful stock quote fetch for invalid symbol
Given I have a valid API key
When I request a stock quote for "INVALID"
Then the response status should be "200"

Scenario: Successful stock quote fetch for MSFT
Given I have a valid API key
When I request a stock quote for "MSFT"
Then the response status should be "200"
And the current price should be a positive number
And the response should contain required quote fields

Scenario: Successful stock quote fetch for GOOGL
Given I have a valid API key
When I request a stock quote for "GOOGL"
Then the response status should be "200"
And the current price should be a positive number
And the response should contain required quote fields
26 changes: 25 additions & 1 deletion test/features/rickandmorty_graphql_api_test.feature
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
Feature: Rick and Morty GraphQL character lookup

@smoke
Scenario: Query character by ID and validate fields
Given I prepare a GraphQL query for character ID 1
When I send the GraphQL request
Then the response status should be 200
Then the response status should be "200"
Then the response should return character "Rick Sanchez"
And the character should belong to the "Human" species
And the response should contain a valid ID and status

Scenario: Query character by name and validate fields
Given I prepare a GraphQL query for character name "Morty Smith"
When I send the GraphQL request
Then the response status should be "200"
Then the response should return character "Morty Smith"
And the character should belong to the "Human" species
And the response should contain a valid ID and status

Scenario: Query character by status and validate fields
Given I prepare a GraphQL query for character status "Alive"
When I send the GraphQL request
Then the response status should be "200"
Then the response should return characters with status "Alive"
And the response should contain valid IDs and statuses

Scenario: Query character by species and validate fields
Given I prepare a GraphQL query for character species "Alien"
When I send the GraphQL request
Then the response status should be "200"
Then the response should return characters of species "Alien"
And each character should have a valid ID and status
And the response should not contain any characters of species "Human"
Loading