Python 2中的单元测试不适用于Python 3

时间:2017-09-27 20:06:07

标签: python python-2.7 python-3.x tdd python-unittest

为了练习和练习我的编程技巧,我决定用Python构建一个应用程序来解决Project Euler中的问题。我没有单元测试的经验所以我决定在TDD的帮助下构建我的项目并获得一些经验。不幸的是,在构建基础时,我使用Python3运行我的程序,但是使用Python2进行了所有单元测试。现在我尝试用Python3进行测试,但它失败了。

在此之前,我从未用Python编写过一些小脚本。我希望这个项目更大,并且它将包含更多文件,所以经过一些研究后我决定使用以下项目结构:

project_euler:
  euler:
    - __init__.py
    - euler.py
    - problems.py
  tests:
    - __init__.py
    - test_euler.py
    - test_problems.py

init文件为空。

euler.py

#!/usr/bin/env python3
# coding: utf-8


class Euler:
    def __init__(self):
        pass

problems.py

#!/usr/bin/env python3
# coding: utf-8

import sys
import euler


class ProjectEuler(euler.Euler):
    def __init__(self):
        pass

test_euler.py

#!/usr/bin/env python3
# coding: utf-8

import unittest
import euler.euler as euler


class UnitTest(unittest.TestCase):
    # a bunch of tests here

if __name__ == '__main__':
    unittest.main(module=None)

test_problems.py

#!/usr/bin/env python3
# coding: utf-8

import unittest
from euler import problems


class UnitTest(unittest.TestCase):
    # a bunch of tests here

if __name__ == '__main__':
    unittest.main(module=None)

以下命令有效:
python -m unittest发现
python -m unittest tests.test_problems

以下不这样做 python3 -m unittest discover

ImportError: Failed to import test module: tests.test_problems
Traceback (most recent call last):
  File "/usr/lib64/python3.6/unittest/loader.py", line 428, in _find_test_path
    module = self._get_module_from_name(name)
  File "/usr/lib64/python3.6/unittest/loader.py", line 369, in _get_module_from_name
    __import__(name)
  File "/home/me/code/project_euler/tests/test_problems.py", line 5, in <module>
    import euler.problems as problems
  File "/home/me/code/project_euler/euler/problems.py", line 8, in <module>
    class ProjectEuler(euler.Euler):
AttributeError: module 'euler' has no attribute 'Euler'

python3 -m unittest tests.test_problems

Traceback (most recent call last):
  File "/usr/lib64/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib64/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/lib64/python3.6/unittest/__main__.py", line 18, in <module>
    main(module=None)
  File "/usr/lib64/python3.6/unittest/main.py", line 94, in __init__
    self.parseArgs(argv)
  File "/usr/lib64/python3.6/unittest/main.py", line 141, in parseArgs
    self.createTests()
  File "/usr/lib64/python3.6/unittest/main.py", line 148, in createTests
    self.module)
  File "/usr/lib64/python3.6/unittest/loader.py", line 219, in loadTestsFromNames
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "/usr/lib64/python3.6/unittest/loader.py", line 219, in <listcomp>
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "/usr/lib64/python3.6/unittest/loader.py", line 153, in loadTestsFromName
    module = __import__(module_name)
  File "/home/me/code/project_euler/tests/test_problems.py", line 5, in <module>
    import euler.problems as problems
  File "/home/mvrakking/code/project_euler/euler/problems.py", line 8, in <module>
    class ProjectEuler(euler.Euler):
AttributeError: module 'euler' has no attribute 'Euler'

我尝试了各种选项,但它破坏了我的程序或我的单元测试。我找不到导致这种情况的Python 2.7.13和3.6.2之间的区别。

1 个答案:

答案 0 :(得分:0)

问题出在行import euler problems.py 文件中。尝试在from .euler import Euler

上替换它