This is a follow up to my previous question. I have built the forms and pages and have moved onto testing it with unit testing. However when I try running my unit tests with a new SQLite database test.db it cannot find the tables within and if I use my main data.db it is unable to read the content of homepage table
In my unit_test.py I have the following for the test base
from flask_testing import TestCase
from flask_sqlalchemy import SQLAlchemy
from flask import url_for
import os
from application import app, db
from application.models import Company, Frankendama
class TestBase(TestCase):
def create_app(self):
app.config.update(
SQLALCHEMY_DATABASE_URI="sqlite:///test.db",
SECRET_KEY='TEST_SECRET_KEY',
DEBUG=True,
WTF_CSRF_ENABLED=False
)
return app
def set_up(self):
db.create_all()
frank1 = Frankendama(
title="Taps",
description="A combo of damas designed for taps",
tama="SK x Cereal STIK",
sarado="Lomond Shape",
sword="Lomond Shape",
string="72",
bearing="Yes"
)
company1 = Company(name = "CEREAL", frankendama_id = 1)
company2 = Company(name = "SK", frankendama_id = 1)
db.session.add(frank1)
db.session.add(company1)
db.session.add(company2)
db.session.commit()
def tear_down(self):
db.session.remove()
db.drop_all()
After the base I have 3 more test classes:
They look like this:
class TestRead(TestBase):
def test_home(self):
response = self.client.get(
url_for('home'),
follow_redirects= True
)
assert "Taps" in response.data.decode()
assert "Check updated task" in response.data.decode()
assert "SK x Ceral STIK" in response.data.decode()
assert "Lomond Shape" in response.data.decode()
assert "Lomond Shape" in response.data.decode()
assert "72" in response.data.decode()
assert "Yes" in response.data.decode()
assert "CEREAL" in response.data.decode()
assert "SK" in response.data.decode()
class TestUpdate(TestBase):
def test_update(self):
response = self.client.post(
url_for('update', id=1),
data={"description": "Check updated task"},
follow_redirects= True
)
assert "Taps" in response.data.decode()
assert "Check updated task" in response.data.decode()
assert "SK x Ceral STIK" in response.data.decode()
assert "Lomond Shape" in response.data.decode()
assert "Lomond Shape" in response.data.decode()
assert "72" in response.data.decode()
assert "Yes" in response.data.decode()
assert "CEREAL" in response.data.decode()
assert "SK" in response.data.decode()
assert "A combo of damas designed for taps" not in response.data.decode()
def test_update_companies(self):
response = self.client.post(
url_for('update', id=1),
data={"companies": "SWEETS"},
follow_redirects= True
)
assert "Taps" in response.data.decode()
assert "A combo of damas designed for taps" in response.data.decode()
assert "SK x Ceral STIK" in response.data.decode()
assert "Lomond Shape" in response.data.decode()
assert "Lomond Shape" in response.data.decode()
assert "72" in response.data.decode()
assert "Yes" in response.data.decode()
assert "SWEETS" in response.data.decode()
assert "SK" not in response.data.decode()
assert "CEREAL" not in response.data.decode()
class TestDelete(TestBase):
def test_delete(self):
response = self.client.get(
url_for('delete', id=1),
follow_redirects=True
)
assert "Taps" not in response.data.decode()
assert "A combo of damas designed for taps" not in response.data.decode()
assert "SK x Ceral STIK" not in response.data.decode()
assert "Lomond Shape" not in response.data.decode()
assert "Lomond Shape" not in response.data.decode()
assert "72" not in response.data.decode()
assert "Yes" not in response.data.decode()
Everytime I try run pytest --cov=app to see the coverage i get these errors:
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: frankendama
======== 6 failed, 1 passed, 21 warnings in 5.72s =========
Overall I am super stumped, any suggestions are hugely welcome!
I found out that coverage does not import the module so when i ran:
pytest tests/test_unit.py --cov=.
i got full coverage and all my tests passed.
I also removed test.db
from SQLALCHEMY_DATABASE_URI="sqlite:///test.db"
and that solved my original problems.