python - 金字塔在主包之外运行测试

标签 python unit-testing python-unittest

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

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

MyProject /
    - myproject /
        - static
        - templates
        - tests.py
        - views.py
    - development.ini
    - production.ini
    - setup.py
    - setup.cfg

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

MyProject /
    - myproject /
        - tests.py
        - views.py
    - myprojectlib /
        - user_service.py
    - tests /
        - user_tests.py
    - setup.py

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

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

import unittest

class UserTests(unittest.TestCase):

    def test_user_can_register(self):

        #code removed for example

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

我看过这篇文章,看来我正在做它建议的所有事情:Having tests in multiple files

在此先感谢您的帮助:)

干杯, 贾斯汀

最佳答案

setup.pysetup 调用中,尝试添加以下内容:

  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 模块。

关于python - 金字塔在主包之外运行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22211948/

相关文章:

python - python 中的多处理并不比顺序处理快——我尝试使用 Pool、Queue 和 Pipe。为什么?

c# - 紧凑框架: Unit tests won't hit breakpoints

c# - 从 dotnet 测试中排除命名空间

python - 为什么unittest.TestCase中的assert函数必须是成员函数?

python - 在 Python 中编写 Selenium 单元测试的正确方法?

python - 如何在 pandas DataFrame 中存储行名和列索引?

python - 用 sk-learn 分类 : handle missing categorical features at prediction

unit-testing - Grails-从2.4.5升级到2.5.5导致运行单元测试时出错:null

python - 如何模拟 SMBConnection 类以返回模拟连接对象

python - 在 Beautifulsoup 的 find_all 中使用正则表达式