我需要一个带有鼻子的python单元测试sqlalchemy模型的样本 [英] I need a sample of python unit testing sqlalchemy model with nose

查看:72
本文介绍了我需要一个带有鼻子的python单元测试sqlalchemy模型的样本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我如何为使用鼻子创建的sqlalchemy模型编写单元测试.

Can someone show me how to write unit tests for sqlalchemy model I created using nose.

我只需要一个简单的例子.

I just need one simple example.

谢谢.

推荐答案

您可以简单地创建一个内存中的SQLite数据库并将您的会话绑定到该数据库.

You can simply create an in-memory SQLite database and bind your session to that.

示例:


from db import session # probably a contextbound sessionmaker
from db import model

from sqlalchemy import create_engine

def setup():
    engine = create_engine('sqlite:///:memory:')
    session.configure(bind=engine)
    # You probably need to create some tables and 
    # load some test data, do so here.

    # To create tables, you typically do:
    model.metadata.create_all(engine)

def teardown():
    session.remove()


def test_something():
    instances = session.query(model.SomeObj).all()
    eq_(0, len(instances))
    session.add(model.SomeObj())
    session.flush()
    # ...

这篇关于我需要一个带有鼻子的python单元测试sqlalchemy模型的样本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆