2016-02-11 69 views
1

目前我的项目的目录被组织成以下结构:单元测试pytest

. 
+-- module1 
+-- module2 
... 
+-- moduleN 
+-- test 
    +-- test_module1_item1.py 
    ... 
    +-- test_moduleN_itemM.py 

但是,当我在我的上一级目录执行py.test,它抛出一个错误,如:

ImportError: No module named 'module1'

如何声明测试文件中的导入?或者我的项目结构应该如何定义以便将测试与其他代码分开?

+3

是否都存在''__init __。py''?好像你没有找到你的模块。 – MSeifert

回答

1

我怀疑__init__.py./test

» ls * 
module1: 
__init__.py 

test: 
test_module1_item1.py 

缺失让我们试试测试

» py.test 
... 
ERROR collecting test/test_module1_item1.py  
test/test_module1_item1.py:8: in <module> 
    import module1 
E ImportError: No module named module1 

这看起来像你的错误。

» touch test/__init__.py 
» py.test 

test/test_module1_item1.py:10: in <module> 
    assert 1 == 2 
E assert 1 == 2 

这看起来像您的解决方案。我怀疑py.test是根据./test是否是一个模块来执行$ pythonpath。