forked from PemaCheki19/demoapp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
53 lines (48 loc) · 1.39 KB
/
conftest.py
File metadata and controls
53 lines (48 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
Test configuration and fixtures for pytest
"""
import pytest
from app import app, db
from models import User, Task
from werkzeug.security import generate_password_hash
@pytest.fixture(scope='session', autouse=True)
def setup_database():
"""Create database tables once per test session"""
with app.app_context():
db.create_all()
yield
# Clean up after all tests
db.drop_all()
@pytest.fixture
def client():
"""Create a test client"""
app.config['TESTING'] = True
app.config['SECRET_KEY'] = 'test-secret-key'
app.config['WTF_CSRF_ENABLED'] = False
with app.test_client() as client:
with app.app_context():
# Clean up any existing data
db.session.remove()
# Truncate all tables
db.session.execute(db.text('TRUNCATE TABLE users, tasks RESTART IDENTITY CASCADE'))
db.session.commit()
yield client
@pytest.fixture
def test_user():
"""Create a test user"""
user = User(
username='testuser',
email='test@example.com',
password_hash=generate_password_hash('testpass123')
)
db.session.add(user)
db.session.commit()
return user
@pytest.fixture
def authenticated_user(client, test_user):
"""Log in a test user"""
client.post('/login', data={
'username': 'testuser',
'password': 'testpass123'
})
return test_user