2017-09-27 138 views
1

要掌握一些練習和編程技巧,我決定在Python中構建一個應用程序來解決Project Euler出現的問題。我沒有單元測試的經驗,所以我決定在TDD的幫助下構建我的項目,並獲得一些經驗。不幸的是,在建立基礎時,我用Python3運行我的程序,但是用Python2做了所有的單元測試。現在我嘗試使用Python3進行測試,但失敗了。單元測試在Python 2中不能在Python 3中工作

在此之前,我從來沒有用Python編寫過多的小腳本。我希望這個項目做大,也將包含多個文件,所以經過一番研究,我決定使用以下項目結構:

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 

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) 

下面的命令做的工作:
蟒蛇-m單元測試發現
蟒蛇-m單元測試tests.test_problems

以下不。
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' 

python3 -m單元測試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之間的區別。

回答

0

問題在文件problems.py在行import euler。嘗試替換它from .euler import Euler

+0

當我運行該程序時會給出以下消息:ModuleNotFoundError:沒有名爲'__main __。euler'的模塊; '__main__'不是包 – Matthieu

+0

嘗試在主 – AndMar

+0

中添加'__init__'無論如何,問題是n這一行,這取決於結構和設置 – AndMar