【问题标题】:Pyramid run tests outside of main packagePyramid 在主包之外运行测试
【发布时间】:2014-04-08 08:22:23
【问题描述】:

我是 python 的新手,所以我认为这更多是我所缺少的理解的东西,因为它不起作用。

我首先创建了一个新的 Pyramid 项目,所以我的目录结构如下所示:

我的项目 / - 我的项目 / - 静止的 - 模板 - 测试.py -views.py - 开发.ini -生产.ini - 设置.py - 设置.cfg

但是,我将很多业务/服务逻辑添加到根包目录下的 myprojectlib 包中,并且还创建了一个测试包,如下所示:

我的项目 / - 我的项目 / - 测试.py -views.py -我的项目库/ - 用户服务.py - 测试 / - user_tests.py - 设置.py

我已将 init.py 文件添加到 'myprojectlib' 和 'tests' 文件夹以将它们打包,但我似乎无法获取 user_tests.py 文件(以及后续测试代码)将包含在python setup.py test 命令中。仅包含 myproject/tests.py 测试。

到目前为止,我的 user_tests.py 文件包含一个测试:

导入单元测试 类用户测试(unittest.TestCase): def test_user_can_register(self): 例如,#code 已删除

我如何告诉 python 在 /tests 目录中查找测试?

我看过这篇文章,似乎我正在做它所建议的一切:Having tests in multiple files

提前感谢您的帮助:)

干杯, 贾斯汀

【问题讨论】:

    标签: python unit-testing python-unittest


    【解决方案1】:

    setup 调用setup.py 中,尝试添加:

      test_suite="tests",
    

    因为那是包含您的测试的目录。我通常像这样构建我的新样式包并使用nose 进行测试,所以我的setup.py 中没有它。添加该声明后,现在会在调用 python setup.py test 时执行测试。

    来源:https://pythonhosted.org/setuptools/setuptools.html#test-build-package-and-run-a-unittest-suite

    再次,确保您在测试目录中有一个__init__.py,否则 setuptools 测试运行程序将抱怨缺少 tests 模块。

    【讨论】:

    • 非常感谢,它已排序:)。我已经有那行了,但它指向“myproject”包。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-02
    • 2019-09-21
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多