简体   繁体   English

使用 pytest 单元测试 Flask-wtfoms

[英]Unit testing Flask-wtfoms with pytest

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但是,当我尝试使用新的 SQLite 数据库 test.db 运行单元测试时,它找不到其中的表,如果我使用主 data.db,则无法读取主页表的内容

In my unit_test.py I have the following for the test base在我的 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()

After the base I have 3 more test classes:在基础之后,我还有 3 个测试类:

  • TestRead测试读取
  • TestUpdate测试更新
  • TestDelete测试删除

They look like this:它们看起来像这样:

class TestRead(TestBase):类 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):类 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):类 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:每次我尝试运行 pytest --cov=app 以查看覆盖率时,我都会收到以下错误:

  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=.我发现覆盖率不会导入模块,所以当我运行时: 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.我还从SQLALCHEMY_DATABASE_URI="sqlite:///test.db"删除了test.db ,这解决了我原来的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM