Skip to content

Add pytest tests #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
Empty file added blogexample/tests/__init__.py
Empty file.
42 changes: 42 additions & 0 deletions blogexample/tests/test_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import pytest
# import blogexample
from blogexample.app import create_app,db
from blogexample.blueprints.blog.models import Post

@pytest.fixture
def client():
app = create_app('testing')
with app.test_client() as client:
with app.app_context():
db.create_all()
yield client
db.session.remove()
db.drop_all()

def test_index_page(client):
# Test the index page
response = client.get('/')
assert response.status_code == 200
assert b'Welcome to the Blog' in response.data

def test_create_post(client):
# Test creating a new post
response = client.post('/new', data=dict(
title='Test Post',
body='This is a test post.'
), follow_redirects=True)

assert response.status_code == 200
assert b'Test Post' in response.data
assert b'This is a test post.' in response.data

def test_view_post(client):
# Test viewing a post
post = Post(title='Another Test Post', body='Another test post body.')
db.session.add(post)
db.session.commit()

response = client.get(f'/post/{post.id}')
assert response.status_code == 200
assert b'Another Test Post' in response.data
assert b'Another test post body.' in response.data
3 changes: 3 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
testpaths = tests
python_paths = .