使用 pytest 对 Flask-wtfoms 进行单元测试

Unit testing Flask-wtfoms with pytest

这是我的 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

的跟进

在我的unit_test.py中,我有以下测试基础

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()

在基础之后我还有 3 个测试 classes:

它们看起来像这样:

class 测试读取(测试库):

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 测试更新(测试库):

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 测试删除(测试库):

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()

每次我尝试 运行 pytest --cov=app 来查看覆盖率时,我都会收到以下错误:

  sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: frankendama
  ======== 6 failed, 1 passed, 21 warnings in 5.72s =========
  

总的来说我超级难过,任何建议都非常欢迎!

我发现 coverage 没有导入模块,所以当我 运行: pytest tests/test_unit.py --cov=. 我得到了全面覆盖并且我的所有测试都通过了。 我还从 SQLALCHEMY_DATABASE_URI="sqlite:///test.db" 中删除了 test.db,这解决了我原来的问题。