From 44a25c1cedb0729702d37055fc12f308e6d55aae Mon Sep 17 00:00:00 2001 From: Steven Date: Tue, 26 Oct 2021 20:41:29 -0400 Subject: [PATCH] added frontend integration testing --- qbay/templates/index.html | 2 +- qbay_test/conftest.py | 40 +++++++++++++++++++++++- qbay_test/frontend/test_registration.py | 41 +++++++++++++++++++++++++ requirements.txt | 3 +- 4 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 qbay_test/frontend/test_registration.py diff --git a/qbay/templates/index.html b/qbay/templates/index.html index 3db017f..a509c35 100644 --- a/qbay/templates/index.html +++ b/qbay/templates/index.html @@ -5,7 +5,7 @@

{% block title %}Profile{% endblock %}

{% endblock %} {% block content %} -

Welcome {{ user.name }} !

+

Welcome {{ user.username }} !

Here are all available products

diff --git a/qbay_test/conftest.py b/qbay_test/conftest.py index 454ad4a..96fae68 100644 --- a/qbay_test/conftest.py +++ b/qbay_test/conftest.py @@ -1,5 +1,10 @@ import os - +import pytest +import time +import tempfile +import threading +from werkzeug.serving import make_server +from qbay import app ''' This file defines what to do BEFORE running any test cases: @@ -23,3 +28,36 @@ def pytest_sessionfinish(): Do nothing for now ''' pass + + +base_url = 'http://127.0.0.1:{}'.format(8081) + + +class ServerThread(threading.Thread): + + def __init__(self): + threading.Thread.__init__(self) + # import necessary routes + from qbay import controllers + self.srv = make_server('127.0.0.1', 8081, app) + self.ctx = app.app_context() + self.ctx.push() + + def run(self): + print('running') + self.srv.serve_forever() + + def shutdown(self): + self.srv.shutdown() + + +@pytest.fixture(scope="session", autouse=True) +def server(): + # create a live server for testing + # with a temporary file as database + server = ServerThread() + server.start() + time.sleep(5) + yield + server.shutdown() + time.sleep(2) diff --git a/qbay_test/frontend/test_registration.py b/qbay_test/frontend/test_registration.py new file mode 100644 index 0000000..843b5ac --- /dev/null +++ b/qbay_test/frontend/test_registration.py @@ -0,0 +1,41 @@ +from seleniumbase import BaseCase + +from qbay_test.conftest import base_url +from unittest.mock import patch +from qbay.models import User + +""" +This file defines all integration tests for the frontend homepage. +""" + + +class FrontEndHomePageTest(BaseCase): + + def test_login_success(self, *_): + """ + This is a sample front end unit test to login to home page + and verify if the tickets are correctly listed. + """ + # open login page + self.open(base_url + '/login') + # fill email and password + self.type("#email", "test0@test.com") + self.type("#password", "123456") + # click enter button + self.click('input[type="submit"]') + + # after clicking on the browser (the line above) + # the front-end code is activated + # and tries to call get_user function. + # The get_user function is supposed to read data from database + # and return the value. However, here we only want to test the + # front-end, without running the backend logics. + # so we patch the backend to return a specific user instance, + # rather than running that program. (see @ annotations above) + + # open home page + self.open(base_url) + # test if the page loads correctly + self.assert_element("#welcome-header") + self.assert_text("Welcome u0 !", "#welcome-header") + # other available APIs diff --git a/requirements.txt b/requirements.txt index 32b6778..8866737 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ Flask Flask-SQLAlchemy pytest -flake8 \ No newline at end of file +flake8 +seleniumbase \ No newline at end of file