Whilst it's good practice to type out your own code, sometimes it's useful to copy and paste from the Project Exercise PDFs. Unfortunately line breaks and formatting do not always copy correctly, so we replicate the larger code samples here for easy copy-pasting.
class ViewModel:
def __init__(self, items):
self._items = items
@property
def items(self):
return self._items
import os
import pytest
import requests
from todo_app import app
from dotenv import load_dotenv, find_dotenv
@pytest.fixture
def client():
# Use our test integration config instead of the 'real' version
file_path = find_dotenv('.env.test')
load_dotenv(file_path, override=True)
# Create the new app.
test_app = app.create_app()
# Use the app to create a test_client that can be used in our tests.
with test_app.test_client() as client:
yield client
class StubResponse():
def __init__(self, fake_response_data):
self.fake_response_data = fake_response_data
def json(self):
return self.fake_response_data
# Stub replacement for requests.get(url)
def stub(url, params={}):
test_board_id = os.environ.get('TRELLO_BOARD_ID')
fake_response_data = None
if url == f'https://api.trello.com/1/boards/{test_board_id}/lists':
fake_response_data = [{
'id': '123abc',
'name': 'To Do',
'cards': [{'id': '456', 'name': 'Test card'}]
}]
return StubResponse(fake_response_data)
raise Exception(f'Integration test did not expect URL "{url}"')
def test_index_page(monkeypatch, client):
# Replace requests.get(url) with our own function
monkeypatch.setattr(requests, 'get', stub)
# Make a request to our app's index page
response = client.get('/')
assert response.status_code == 200
assert 'Test card' in response.data.decode()
import os
import pytest
from threading import Thread
from time import sleep
from selenium import webdriver
from dotenv import load_dotenv
from todo_app import app
@pytest.fixture(scope='module')
def app_with_temp_board():
# Load our real environment variables
load_dotenv(override=True)
# Create the new board & update the board id environment variable
board_id = create_trello_board()
os.environ['TRELLO_BOARD_ID'] = board_id
# Construct the new application
application = app.create_app()
# Start the app in its own thread.
thread = Thread(target=lambda: application.run(use_reloader=False))
thread.daemon = True
thread.start()
# Give the app a moment to start
sleep(1)
# Return the application object as the result of the fixture
yield application
# Tear down
thread.join(1)
delete_trello_board(board_id)
def create_trello_board():
# TODO Create a new board in Trello and return the id
pass
def delete_trello_board(board_id):
# TODO Delete the Trello board with id board_id
pass
@pytest.fixture(scope="module")
def driver():
with webdriver.Firefox() as driver:
yield driver
def test_task_journey(driver, app_with_temp_board):
driver.get('http://localhost:5000/')
assert driver.title == 'To-Do App'