在Python3 virtualenv中,python setup.py测试会抛出错误,否则会发生错误

时间:2017-05-08 17:59:59

标签: python python-3.x

更新 Source is available here

使用系统python,它可以工作:

$ python setup.py test
<snip>
$ python --version
Python 2.7.13

在Python 2.7 virtualenv中,它可以工作

virtualenv venv && source venv/bin/activate && python setup.py test

在Python 3.6 virtualenv中,它失败了

virtualenv venv -p python3 && source venv/bin/activate && python setup.py test

错误是:

running test
running egg_info
writing cupcake.egg-info/PKG-INFO
writing dependency_links to cupcake.egg-info/dependency_links.txt
writing top-level names to cupcake.egg-info/top_level.txt
reading manifest file 'cupcake.egg-info/SOURCES.txt'
writing manifest file 'cupcake.egg-info/SOURCES.txt'
running build_ext
Traceback (most recent call last):
  File "setup.py", line 7, in <module>
    packages=['cupcake', 'cupcake.tests']
  File "/usr/lib64/python3.5/distutils/core.py", line 148, in setup
    dist.run_commands()
  File "/usr/lib64/python3.5/distutils/dist.py", line 955, in run_commands
    self.run_command(cmd)
  File "/usr/lib64/python3.5/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/home/me/problem_example/venv/lib/python3.5/site-packages/setuptools/command/test.py", line 211, in run
    self.run_tests()
  File "/home/me/problem_example/venv/lib/python3.5/site-packages/setuptools/command/test.py", line 234, in run_tests
    **exit_kwarg
  File "/usr/lib64/python3.5/unittest/main.py", line 94, in __init__
    self.runTests()
  File "/usr/lib64/python3.5/unittest/main.py", line 255, in runTests
    self.result = testRunner.run(self.test)
  File "/usr/lib64/python3.5/unittest/runner.py", line 176, in run
    test(result)
  File "/usr/lib64/python3.5/unittest/suite.py", line 84, in __call__
    return self.run(*args, **kwds)
  File "/usr/lib64/python3.5/unittest/suite.py", line 122, in run
    test(result)
  File "/usr/lib64/python3.5/unittest/suite.py", line 84, in __call__
    return self.run(*args, **kwds)
  File "/usr/lib64/python3.5/unittest/suite.py", line 122, in run
    test(result)
  File "/usr/lib64/python3.5/unittest/suite.py", line 84, in __call__
    return self.run(*args, **kwds)
  File "/usr/lib64/python3.5/unittest/suite.py", line 122, in run
    test(result)
  File "/usr/lib64/python3.5/unittest/suite.py", line 84, in __call__
    return self.run(*args, **kwds)
  File "/usr/lib64/python3.5/unittest/suite.py", line 122, in run
    test(result)
  File "/usr/lib64/python3.5/unittest/case.py", line 666, in __call__
    return self.run(*args, **kwds)
  File "/usr/lib64/python3.5/unittest/case.py", line 591, in run
    result.startTest(self)
  File "/usr/lib64/python3.5/unittest/runner.py", line 54, in startTest
    self.stream.write(self.getDescription(test))
  File "/usr/lib64/python3.5/unittest/runner.py", line 47, in getDescription
    return '\n'.join((str(test), doc_first_line))
  File "/usr/lib64/python3.5/unittest/case.py", line 1391, in __str__
    self._testFunc.__name__)
AttributeError: 'str' object has no attribute '__name__'

错误是:

AttributeError: 'str' object has no attribute '__name__'

示例项目非常简单:

$ tree
.
├── cupcake
│   ├── cupcake.py
│   └── __init__.py
├── setup.py
└── tests
    ├── cupcake.py
    └── __init__.py


$ more setup.py 
from setuptools import setup

setup(
    name='cupcake',
    version='1.0',
    package_dir={'cupcake': 'cupcake', 'cupcake.tests': 'tests'},
    packages=['cupcake', 'cupcake.tests']
)


$ more cupcake/cupcake.py     
def hello_world():
    print('Hello, World!')


$ more cupcake/__init__.py 
# nothing


$ more tests/cupcake.py 
from unittest import TestCase

from cupcake import cupcake

class cupcakeTest(TestCase):
    def setup(self):
        self.x = 1

    def test_nothing(self):
        self.assertEquals(True, True)

    def test_hello_world(self):
        cupcake.hello_world()
        self.assertEquals(True, True)


$ more tests/__init__.py 
from unittest import *
from .cupcake import cupcakeTest

def tests():
    suite = TestSuite()
    suite.addTest(makeSuite(cupcakeTest))
    return suite

0 个答案:

没有答案