如何判断nosetest何时以编程方式运行

发布于 2024-08-06 22:07:35 字数 1226 浏览 9 评论 0原文

nosetest 是 Turbogeras 2.0 中的默认测试框架。该应用程序有一个用于初始化数据库的 websetup.py 模块。我在开发和生产环境中使用 mysql,并且 websetup 工作正常,但是 nosetest 在内存上使用 sqlite,当它尝试初始化数据库时会发送错误:

类型错误:SQLite 日期、时间和 DateTime 类型仅接受 Python 日期时间对象作为输入。

我已经检测到这种情况何时发生,并且处于导入阶段:

csvreader = csv.reader(open('res/products.csv'), delimiter=",", quotechar="'")
for row in csvreader:
    p = model.Product(row[1], row[2], row[3], row[4] + ".jpg")
    # Even tried to convert the date to a sqlalchemy type
    # need to put a conditional here, when testing I don't care this date
    import sqlalchemy
    dateadded = sqlalchemy.types.DateTime(row[5])
    p.dateAdded = dateadded
    p.brand_id = row[6]
    p.code = row[3]

    ccat = model.DBSession.query(model.Category)\
        .filter(model.Category.id==int(row[8]) + 3).one()

    p.categories.append(ccat)

    p.normalPrice = row[9]
    p.specialPrice = row[10]
    p.discountPrice = row[11]

    model.DBSession.add(p)

How can I告诉nosetest何时运行?我尝试过:

if globals().has_key('ModelTest'):

if vars().has_key('ModelTest'):

一个没有结果,第二个有错误

nosetest is the default test framework in Turbogeras 2.0. The application has a websetup.py module that initialise the database. I use mysql for my development and production environment and websetup works fine, but nosetest uses sqlite on memory and when it tries to initialise the DB sends an error:

TypeError: SQLite Date, Time, and
DateTime types only accept Python
datetime objects as input.

I've detected when this happens and is in the import fase:

csvreader = csv.reader(open('res/products.csv'), delimiter=",", quotechar="'")
for row in csvreader:
    p = model.Product(row[1], row[2], row[3], row[4] + ".jpg")
    # Even tried to convert the date to a sqlalchemy type
    # need to put a conditional here, when testing I don't care this date
    import sqlalchemy
    dateadded = sqlalchemy.types.DateTime(row[5])
    p.dateAdded = dateadded
    p.brand_id = row[6]
    p.code = row[3]

    ccat = model.DBSession.query(model.Category)\
        .filter(model.Category.id==int(row[8]) + 3).one()

    p.categories.append(ccat)

    p.normalPrice = row[9]
    p.specialPrice = row[10]
    p.discountPrice = row[11]

    model.DBSession.add(p)

How can I tell when nosetest is running? I've try:

if globals().has_key('ModelTest'):

and

if vars().has_key('ModelTest'):

The first one with no results and the second one with error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸩远一方 2024-08-13 22:07:35

据推测,如果nose正在运行,“nose”顶级模块将被导入。您应该能够使用“

if 'nose' in sys.modules:
    print "Nose is running, or at least has been imported!"
    #or whatever you need to do if nose is running

当然这不是一个万无一失的测试”来测试它,它假设没有其他原因导致鼻子被导入。

Presumably if nose is running, the 'nose' top-level module will have been imported. You should be able to test that with

if 'nose' in sys.modules:
    print "Nose is running, or at least has been imported!"
    #or whatever you need to do if nose is running

Of course this is not a foolproof test, which assumes that there's no other reason why nose would be imported.

绿光 2024-08-13 22:07:35

我不使用 TurboGears,但是是否没有设置或全局某处表明测试正在运行?在大型系统中,运行测试时通常需要进行一些小的更改。 SQLite 和 MySQL 之间的切换只是一个例子。

I don't use TurboGears, but is there not a setting or global somewhere that indicates that the tests are running? In large systems, there are often small changes that need to be made when running tests. Switching between SQLite and MySQL is just one example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文