Skip to content

Instantly share code, notes, and snippets.

@4sskick
Created November 29, 2023 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 4sskick/5747e67d73c09033aeae725ad7b26a4d to your computer and use it in GitHub Desktop.
Save 4sskick/5747e67d73c09033aeae725ad7b26a4d to your computer and use it in GitHub Desktop.
from flask import Flask
# create and initialize a new Flask app
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello, World!"
if __name__ == "__main__":
app.run()
from project.app import app
def test_index():
tester = app.test_client()
response = tester.get("/", content_type="html/text")
assert response.status_code == 200
assert response.data == b"Hello, World!"
I'm using version 3.12 on virtual env with wsl2
- make sure to activate the virutal env `source venv/bin/activate`
- I'm using flask as framework `pip install flask==3.0.0` then continue with `pip install pytest==7.4.2`
- make skeleton project like this:
├── project
│ ├── __init__.py
│ ├── app.py
└── tests
├── __init__.py
└── app_test.py
- open tests/app_test.py and add the following code (see file app_test.py)
- begin the test `python -m pytest`, it goes fail with `ImportError: cannot import name 'app' from 'project.app`
- open project/app.py and add following code (see app.py)
- run the app with `FLASK_APP=project/app.py python -m flask run -p 5001` then navigate your browser to `http://localhost:5001`
- Return to the terminal. Kill the server with Ctrl+C. Run the test again `python -m pytest`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment