gpt4 book ai didi

Python 包结构,setup.py 用于运行单元测试

转载 作者:IT老高 更新时间:2023-10-28 20:23:24 26 4
gpt4 key购买 nike

我不确定我是否正确地组织了我的包结构或在 setup.py 中使用了正确的选项,因为我在尝试运行单元测试时遇到了错误。

我有这样的结构:

/project
/bin
/src
/pkgname
__init__.py
module1.py
module2.py
/tests
__init__.py
test1.py
test2.py

我的 setup.py 看起来像这样:

#!/usr/bin/env python                                                                                                                                        
from setuptools import setup, find_packages

setup(version='0.1',
description='Trend following library',
author='Nate Reed',
author_email='nate@natereed.com',
packages=find_packages(),
install_requires=['numpy'],
test_suite="tests",
)

当我运行“python setup.py test”时,我得到:

nate@nate-desktop:~/PycharmProjects/trendfollowing$ sudo python setup.py test
running test
running egg_info
writing requirements to UNKNOWN.egg-info/requires.txt
writing UNKNOWN.egg-info/PKG-INFO
writing top-level names to UNKNOWN.egg-info/top_level.txt
writing dependency_links to UNKNOWN.egg-info/dependency_links.txt
reading manifest file 'UNKNOWN.egg-info/SOURCES.txt'
writing manifest file 'UNKNOWN.egg-info/SOURCES.txt'
running build_ext
Traceback (most recent call last):
File "setup.py", line 11, in <module>
test_suite="tests",
File "/usr/lib/python2.6/distutils/core.py", line 152, in setup
dist.run_commands()
File "/usr/lib/python2.6/distutils/dist.py", line 975, in run_commands
self.run_command(cmd)
File "/usr/lib/python2.6/distutils/dist.py", line 995, in run_command
cmd_obj.run()
File "/usr/lib/python2.6/dist-packages/setuptools/command/test.py", line 137, in run
self.with_project_on_sys_path(self.run_tests)
File "/usr/lib/python2.6/dist-packages/setuptools/command/test.py", line 117, in with_project_on_sys_path
func()
File "/usr/lib/python2.6/dist-packages/setuptools/command/test.py", line 146, in run_tests
testLoader = loader_class()
File "/usr/lib/python2.6/unittest.py", line 816, in __init__
self.parseArgs(argv)
File "/usr/lib/python2.6/unittest.py", line 843, in parseArgs
self.createTests()
File "/usr/lib/python2.6/unittest.py", line 849, in createTests
self.module)
File "/usr/lib/python2.6/unittest.py", line 613, in loadTestsFromNames
suites = [self.loadTestsFromName(name, module) for name in names]
File "/usr/lib/python2.6/unittest.py", line 587, in loadTestsFromName
return self.loadTestsFromModule(obj)
File "/usr/lib/python2.6/dist-packages/setuptools/command/test.py", line 34, in loadTestsFromModule
tests.append(self.loadTestsFromName(submodule))
File "/usr/lib/python2.6/unittest.py", line 584, in loadTestsFromName
parent, obj = obj, getattr(obj, part)
AttributeError: 'module' object has no attribute 'test1'

测试名称是否需要与模块名称匹配?我的包结构中是否需要遵循其他约定?

最佳答案

通过反复试验,我找到了这个问题的原因。测试名称应与模块名称匹配。如果有“foo_test.py”测试,则需要有对应的模块foo.py。

我找到了一些 guidelines on organizing package structure ,这帮助我将我的包裹重新组织成一个我有信心的结构。

关于Python 包结构,setup.py 用于运行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6164004/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com