Python 2中的Unittest在Python 3中不起作用
为了练习和提高编程技能,我决定用Python构建一个应用程序来解决Euler项目中的问题。我没有单元测试的经验,所以我决定在TDD的帮助下构建我的项目,并从中获得一些经验。不幸的是,在构建基础时,我用Python 3运行我的程序,但是用Python 2进行了所有单元测试。现在我尝试用Python3进行测试,但失败了 在此之前,我只使用Python编写了一些小脚本。我希望这个项目会更大,也会包含更多的文件,所以经过一些研究后,我决定使用以下项目结构:Python 2中的Unittest在Python 3中不起作用,python,python-2.7,python-3.x,tdd,python-unittest,Python,Python 2.7,Python 3.x,Tdd,Python Unittest,为了练习和提高编程技能,我决定用Python构建一个应用程序来解决Euler项目中的问题。我没有单元测试的经验,所以我决定在TDD的帮助下构建我的项目,并从中获得一些经验。不幸的是,在构建基础时,我用Python 3运行我的程序,但是用Python 2进行了所有单元测试。现在我尝试用Python3进行测试,但失败了 在此之前,我只使用Python编写了一些小脚本。我希望这个项目会更大,也会包含更多的文件,所以经过一些研究后,我决定使用以下项目结构: project_euler: euler:
project_euler:
euler:
- __init__.py
- euler.py
- problems.py
tests:
- __init__.py
- test_euler.py
- test_problems.py
初始化文件为空
euler.py
#!/usr/bin/env python3
# coding: utf-8
class Euler:
def __init__(self):
pass
#!/usr/bin/env python3
# coding: utf-8
import sys
import euler
class ProjectEuler(euler.Euler):
def __init__(self):
pass
#!/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)
问题。py
#!/usr/bin/env python3
# coding: utf-8
class Euler:
def __init__(self):
pass
#!/usr/bin/env python3
# coding: utf-8
import sys
import euler
class ProjectEuler(euler.Euler):
def __init__(self):
pass
#!/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)
测试euler.py
#!/usr/bin/env python3
# coding: utf-8
class Euler:
def __init__(self):
pass
#!/usr/bin/env python3
# coding: utf-8
import sys
import euler
class ProjectEuler(euler.Euler):
def __init__(self):
pass
#!/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单元测试发现
python-m unittest.test\u问题 以下情况不适用。
python3-m单元测试发现
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'
ImportError:无法导入测试模块:tests.test\u问题
回溯(最近一次呼叫最后一次):
文件“/usr/lib64/python3.6/unittest/loader.py”,第428行,在查找测试路径中
module=self.\u从\u name(name)获取\u module\u
文件“/usr/lib64/python3.6/unittest/loader.py”,第369行,在“从”name“获取”模块中
__导入(名称)
文件“/home/me/code/project\u euler/tests/test\u problems.py”,第5行,在
将euler.problems作为问题导入
文件“/home/me/code/project\u euler/euler/problems.py”,第8行,在
类ProjectEuler(euler.euler):
AttributeError:模块“euler”没有属性“euler”
python3-m单元测试。测试问题
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'
回溯(最近一次呼叫最后一次):
文件“/usr/lib64/python3.6/runpy.py”,第193行,在“运行”模块中作为“主”
“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
文件“/usr/lib64/python3.6/runpy.py”,第85行,在运行代码中
exec(代码、运行\全局)
文件“/usr/lib64/python3.6/unittest/__main__uuu.py”,第18行,在
主(模块=无)
文件“/usr/lib64/python3.6/unittest/main.py”,第94行,在__
self.parseArgs(argv)
parseArgs中的文件“/usr/lib64/python3.6/unittest/main.py”,第141行
self.createTests()
文件“/usr/lib64/python3.6/unittest/main.py”,第148行,在createTests中
自模块)
loadTestsFromNames中的文件“/usr/lib64/python3.6/unittest/loader.py”,第219行
suites=[self.loadTestsFromName(name,module)表示名称中的名称]
文件“/usr/lib64/python3.6/unittest/loader.py”,第219行,在
suites=[self.loadTestsFromName(name,module)表示名称中的名称]
loadTestsFromName中的文件“/usr/lib64/python3.6/unittest/loader.py”,第153行
模块=\uuuu导入\uuuu(模块名称)
文件“/home/me/code/project\u euler/tests/test\u problems.py”,第5行,在
将euler.problems作为问题导入
文件“/home/mvrakking/code/project_euler/euler/problems.py”,第8行,在
类ProjectEuler(euler.euler):
AttributeError:模块“euler”没有属性“euler”
我尝试了各种选择,但它破坏了我的程序或单元测试。我找不到Python 2.7.13和3.6.2之间的差异是什么导致了这一问题。文件problems.py行
导入euler
中存在问题。尝试在.euler import euler的上替换它行中的文件problems.py中存在问题import euler
。尝试在.euler import euler的上替换它,当我运行程序时,它会给出以下消息:ModuleNotFoundError:没有名为'main.euler'的模块;'“main”不是要在main中添加\uuuu init\uuuu
的程序包无论如何,问题在于此行,它取决于结构和设置。我没有文件main。我也尝试了这一行的一些方法,但它破坏了程序或单元测试。您是否使用了某种IDE?这在我运行程序时给出了以下消息:ModuleNotFoundError:没有名为“main.euler”的模块;'“main”不是要在main中添加\uuuu init\uuuu
的程序包无论如何,问题在于此行,它取决于结构和设置。我没有文件main。我也试过这一行的一些东西,但它破坏了程序或单元测试。你使用一些IDE吗?