在主包外运行Pyramid测试
我刚开始学Python,所以我觉得我缺少一些理解,这导致我的代码无法正常工作。
我创建了一个新的Pyramid项目,所以我的文件夹结构看起来是这样的:
MyProject / - myproject / - static - templates - tests.py - views.py - development.ini - production.ini - setup.py - setup.cfg
不过,我把很多业务逻辑和服务逻辑放在了根目录下的myprojectlib包里,还创建了一个tests包,结构如下:
MyProject / - myproject / - tests.py - views.py - myprojectlib / - user_service.py - tests / - user_tests.py - setup.py
我在'myprojectlib'和'tests'文件夹里添加了init.py文件,这样它们就变成了包,但我似乎无法让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目录里找测试呢?
我看过这篇文章,似乎我做的都符合建议:在多个文件中进行测试
提前谢谢你的帮助 :)
祝好,
Justin
1 个回答
1
在 setup.py
文件中的 setup
调用里,试着加上这个:
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
模块。