kylelhk / Pictionary

CITS5505 Agile Web Development - Group Project
MIT License
1 stars 0 forks source link

Issue #45 - Create unit tests for create drawing page's endpoints #67

Closed trieuH closed 4 months ago

trieuH commented 4 months ago

Change Summary

  1. Added unit tests for the endpoints /submit-drawing and /get-random-word endpoints used by the Create Drawing page.
  2. Confirmed all tests passed. image
  3. Added instructions in the README document for how to run unit tests.

Change Form

Other Information

kylelhk commented 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.

trieuH commented 4 months ago

Implemented base test class as suggested above. Wrote unit tests for other pages.

Confirmed all unit tests work as expected:

image