python - 使用 init_model 中的模型对 Pylons 应用程序进行 Nose 测试?

标签 python sqlalchemy pylons nose nosetests

我有一个使用 paster create -t​​ pylons 创建的库存 Pylons 应用程序,其中包含一个 Controller 和匹配的功能测试,使用 paster Controller 添加,以及一个 SQLAlchemy 表和映射的 ORM类(class)。 SQLAlchemy 的内容是在 init_model() 函数中定义的,而不是在模块范围内(并且需要在那里)。

运行python setup.py test会引发异常,因为nose以某种方式导致init_model()在同一进程中被调用两次,所以它试图创建一个已经存在的模型。

我可以通过设置和检查 init_model() 内部的全局变量来解决这个问题,但是 (a) 我不想这样做,(b) 第三方库,例如 AuthKit 动态地定义模型也会破坏测试,并且不能轻易更改。

有没有办法修复 Pylons 的 nose 测试,或者我应该编写自己的测试脚本并仅使用 unittestloadapp、直接和webtest?有这方面的工作示例吗?

最佳答案

我会尝试调试你的nosetest运行。为什么不放:

import pdb;pdb.set_trace()

init_model() 函数中查看它是如何被多次调用的。

运行 PDB 后,您可以使用 where 命令查看堆栈跟踪:

w(here)
Print a stack trace, with the most recent frame at the bottom.
An arrow indicates the "current frame", which determines the
context of most commands.  'bt' is an alias for this command.

关于python - 使用 init_model 中的模型对 Pylons 应用程序进行 Nose 测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1342232/

相关文章:

python - 如何用星号 ("*") 替换 csv 文件列中的非重复值?

python - 如何减少 Pandas DataFrame 的内存?

python - 使用 % 和//求除数

python - 使用语句和 SQLalchemy 引擎

python - Turbomail 与 Pyramid 集成

python - 关闭单元测试的翻译

python - 在执行任务之前禁用 QPushButton

flask - 在 Flask 中分配组权限

python - sqlalchemy.exc.InvalidRequestError : SQL expression, 列,或预期的映射实体

python - Pylons 用户身份验证是我们自己的还是 openid 或替代方案?