import pytest from app import app, init_db @pytest.fixture def client(): # POSTGRES_* environment variables select the disposable CI service database. app.config['TESTING'] = True with app.app_context(): init_db() with app.test_client() as client: yield client def test_visitor_counter(client): """Test that the visitor counter increments.""" # First request rv = client.get('/') assert rv.status_code == 200 first_count = get_count_from_response(rv.data) # Second request rv = client.get('/') assert rv.status_code == 200 second_count = get_count_from_response(rv.data) assert second_count == first_count + 1 def get_count_from_response(data): """Extract visitor count from HTML.""" # Simple string parsing content = data.decode('utf-8') start = content.find("Visitor Count: ") + len("Visitor Count: ") end = content.find("", start) return int(content[start:end])