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

发布于 2024-11-10 13:34:38 字数 3071 浏览 1 评论 0原文

我不确定我是否正确组织了我的包结构或在 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='[email protected]',
      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'

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

I'm not sure I'm organizing my package structure correctly or am using the right options in setup.py because I'm getting errors when I try to run unit tests.

I have a structure like this:

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

My setup.py looks like this:

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

setup(version='0.1',
      description='Trend following library',
      author='Nate Reed',
      author_email='[email protected]',
      packages=find_packages(),
      install_requires=['numpy'],
      test_suite="tests",                          
)

When I run 'python setup.py test' I get:

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'

Do the test names need to match module names? Are there other conventions I need to follow in my package structure?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清旖 2024-11-17 13:34:38

通过一些尝试和错误,我找到了这个问题的原因。测试名称应与模块名称匹配。如果有“foo_test.py”测试,则需要有相应的模块foo.py。

我找到了一些组织包结构的指南,这帮助我将包重新组织成我想要的结构是有信心的。

Through some trial and error, I found the cause of this problem. Test names should match module names. If there is a "foo_test.py" test, there needs to be a corresponding module foo.py.

I found some guidelines on organizing package structure, which helped me reorganize my package into a structure I was confident in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文