Skip to content

Commit aab5c49

Browse files
committed
UPDATE: move the base URL to the .env file
1 parent 4ae17c9 commit aab5c49

6 files changed

Lines changed: 28 additions & 5 deletions

File tree

.github/workflows/test.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,19 @@ jobs:
3939
export PYTHONPATH=.
4040
nohup python app.py > flask.log 2>&1 &
4141
sleep 5
42+
env:
43+
APP_HOST: ${{ secrets.APP_HOST }}
44+
APP_PORT: ${{ secrets.APP_PORT }}
4245

4346
- name: Run pytest tests
4447
working-directory: .
4548
run: |
4649
source venv/bin/activate
4750
export PYTHONPATH=.
4851
python tasks.py
52+
env:
53+
APP_HOST: ${{ secrets.APP_HOST }}
54+
APP_PORT: ${{ secrets.APP_PORT }}
4955

5056
- name: Upload Report
5157
uses: actions/upload-artifact@v4

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ __pycache__/
55
# Virtual Environment
66
venv/
77
.venv/
8+
.env
89

910
# Pytest cache
1011
.pytest_cache/

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This is only backup repository. I'm starting to learn API test using Python in m
55
## Note
66
- This repository contains a custom Flask application and custom Pytest scripts for automating API testing. The test scenarios cover simple CRUD API testing. The Flask application automatically generates JSON file as the database, and the Pytest scripts automatically generate and open HTML reports. Ensure you have Python installed before attempting this repository.
77
- This repository tested on Windows 11, and WSL2. I can't test it using macOS or Linux desktop since I don't have Mac and not installing Linux desktop.
8+
- Before try this repository, you need to make '.env' file to save your 'APP_HOST' & 'APP_PORT'
89

910
## Steps to start this pytest using any terminal :
1011
1. python -m venv venv # Create your virtual environment

app.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
import os
12
from flask import Flask
2-
# Import all four method-specific blueprints
3+
from dotenv import load_dotenv
4+
5+
load_dotenv()
6+
HOST = os.getenv('APP_HOST')
7+
PORT = int(os.getenv('APP_PORT'))
8+
39
from routes.post_users import post_bp
410
from routes.get_users import get_bp
511
from routes.put_users import put_bp
@@ -16,4 +22,4 @@
1622
if __name__ == '__main__':
1723
# Flask defaults to 127.0.0.1:5000.
1824
# If you see 3000, you explicitly set it like this:
19-
app.run(debug=True, host='127.0.0.1', port=3000)
25+
app.run(debug=True, host=HOST, port=PORT)

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ pytest-order
44
requests
55
Faker
66
uuid
7-
pytest-html
7+
pytest-html
8+
python-dotenv

tests/conftest.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@
22
import requests
33
import random
44
import string
5+
import os
56
from faker import Faker
7+
from dotenv import load_dotenv
68

9+
load_dotenv()
710
fake = Faker()
811

912
@pytest.fixture(scope="session")
1013
def base_url():
11-
# Replace with your actual API's base URL
12-
return "http://localhost:3000"
14+
host = os.getenv('APP_HOST')
15+
port = os.getenv('APP_PORT')
16+
17+
if host and port:
18+
return f"http://{host}:{port}"
19+
else:
20+
raise ValueError("APP_HOST or APP_PORT not found in environment. Check .env file.")
1321

1422
@pytest.fixture(scope="session")
1523
def shared_state():

0 commit comments

Comments
 (0)