Closed trieuH closed 4 months ago
Great tests. Thanks for the setup.
Possible to make some functions reusable across different test classes / .py files by creating a base file to define a base test class with the common setup, teardown, and helper methods? For example:
# base_test.py
import unittest
from flask_testing import TestCase
from app import create_app, db
from app.models import User
class BaseTestCase(TestCase):
def create_app(self):
return create_app("TestConfig")
def setUp(self):
self.app = self.create_app()
self.app_context = self.app.app_context()
self.app_context.push()
db.create_all()
self.add_test_data_to_db()
def tearDown(self):
db.session.remove()
db.drop_all()
self.app_context.pop()
def add_test_data_to_db(self):
user = User(username="user1", email="user1@example.com")
user.set_password("password")
db.session.add(user)
db.session.commit()
def login_test_user(self):
self.client.post(
"/login",
json={
"action": "Login",
"login-username": "user1",
"login-password": "password",
"remember_me": False,
},
headers={"X-Requested-With": "XMLHttpRequest"},
)
Then we may inherit from the base test class for further testing like this:
from base_test.py import BaseTestCase
class CreateDrawingsTest(BaseTestCase):
This way we can minimise the redundancy across the test cases.
Implemented base test class as suggested above. Wrote unit tests for other pages.
Confirmed all unit tests work as expected:
Change Summary
/submit-drawing
and/get-random-word
endpoints used by the Create Drawing page.Change Form
Other Information