我遇到奇怪的错误,调用 ./manage.py test 会找到我的测试,但提示无法导入它们。
python 3.4
Django 1.7b4
看起来像这样(只是相关的部分):
inkasso
├── db.sqlite3
├── functional_tests
│ ├── base.py
│ ├── base.pyc
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── __pycache__
│ ├── test_login.py
│ └── test_login.pyc
├── __init__.py
├── inkasso
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── migrations
│ ├── models.py
│ ├── settings.py
│ ├── settings.pyc
│ ├── urls.py
│ └── wsgi.py
├── manage.py
├── static
│ ├── ...
├── templates
│ ├── ...
└── web
├── admins.py
├── tests
│ ├── __init__.py
│ ├── test_forms.py
│ ├── test_models.py
│ └── test_views.py
├── urls.py
└── views.py
所以当我运行 ./manage.py test 时,我得到以下 stak-trace:
$ ./manage.py test
Creating test database for alias 'default'...
EEEE
======================================================================
ERROR: inkasso.functional_tests.test_login (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python3.4/unittest/case.py", line 57, in testPartExecutor
yield
File "/usr/lib/python3.4/unittest/case.py", line 574, in run
testMethod()
File "/usr/lib/python3.4/unittest/loader.py", line 32, in testFailure
raise exception
ImportError: Failed to import test module: inkasso.functional_tests.test_login
Traceback (most recent call last):
File "/usr/lib/python3.4/unittest/loader.py", line 312, in _find_tests
module = self._get_module_from_name(name)
File "/usr/lib/python3.4/unittest/loader.py", line 290, in _get_module_from_name
__import__(name)
ImportError: No module named 'inkasso.functional_tests'
======================================================================
ERROR: inkasso.web.tests.test_forms (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python3.4/unittest/case.py", line 57, in testPartExecutor
yield
File "/usr/lib/python3.4/unittest/case.py", line 574, in run
testMethod()
File "/usr/lib/python3.4/unittest/loader.py", line 32, in testFailure
raise exception
ImportError: Failed to import test module: inkasso.web.tests.test_forms
Traceback (most recent call last):
File "/usr/lib/python3.4/unittest/loader.py", line 312, in _find_tests
module = self._get_module_from_name(name)
File "/usr/lib/python3.4/unittest/loader.py", line 290, in _get_module_from_name
__import__(name)
ImportError: No module named 'inkasso.web'
======================================================================
ERROR: inkasso.web.tests.test_models (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python3.4/unittest/case.py", line 57, in testPartExecutor
yield
File "/usr/lib/python3.4/unittest/case.py", line 574, in run
testMethod()
File "/usr/lib/python3.4/unittest/loader.py", line 32, in testFailure
raise exception
ImportError: Failed to import test module: inkasso.web.tests.test_models
Traceback (most recent call last):
File "/usr/lib/python3.4/unittest/loader.py", line 312, in _find_tests
module = self._get_module_from_name(name)
File "/usr/lib/python3.4/unittest/loader.py", line 290, in _get_module_from_name
__import__(name)
ImportError: No module named 'inkasso.web'
======================================================================
ERROR: inkasso.web.tests.test_views (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python3.4/unittest/case.py", line 57, in testPartExecutor
yield
File "/usr/lib/python3.4/unittest/case.py", line 574, in run
testMethod()
File "/usr/lib/python3.4/unittest/loader.py", line 32, in testFailure
raise exception
ImportError: Failed to import test module: inkasso.web.tests.test_views
Traceback (most recent call last):
File "/usr/lib/python3.4/unittest/loader.py", line 312, in _find_tests
module = self._get_module_from_name(name)
File "/usr/lib/python3.4/unittest/loader.py", line 290, in _get_module_from_name
__import__(name)
ImportError: No module named 'inkasso.web'
----------------------------------------------------------------------
Ran 4 tests in 0.001s
FAILED (errors=4)
Destroying test database for alias 'default'...
所以测试运行器找到了我的测试,但由于某种原因,它们没有被导入。我不知道这是怎么回事。堆栈跟踪对我不是很有帮助:(
由于根文件夹名为 inkasso 并且它有一个同名模块,我尝试将 print(os.getcwd) 和 print(sys .path) 到 manage.py 中,它显示的只是 CWD 和路径都设置为指向根文件夹,所以应该一切正常,不是吗?该应用程序本身按预期运行。只有测试不起作用。
我试过在 inkasso.inkasso 中创建一个空模块“web”,结果不是提示 inkasso.web 不存在,而是提示 inkasso.web.tests 不存在。所以这表明它不是在根“inkasso”文件夹中查找,而是在“inkasso.inkasso”中查找。这就是问题所在。我该如何解决?
最佳答案
是的...运行 ./manage.py 时出现问题,因为它将当前目录添加到 PYTHONPATH。
这个问题发生在你把 __init__.py在根文件夹中。
在这种情况下,一种解决方案是永远不要使用 manage.py , 但只有 django-admin.py <commands> --settings=inkasso.inkasso.settings - 当然,这假设在运行此命令时,您处于上一级,在根文件夹之外 inkasso ,或者您的主包安装在 site-packages 中.
例如,如果您的 settings.py 的完整路径文件是 /home/user/projects/inkasso/inkasso/settings.py , 你需要在 /home/user/projects运行此命令时。
但是,如果您已将软件包安装在站点软件包中,则上述限制会发生变化:您可以在除 /home/user/projects/inkasso 之外的任何位置。或其任何子文件夹等。
另一个解决方案是编辑您的 manage.py文件添加这一行:
if __name__ =='__main__': #line already present
#this will make the python interpreter see your packages as inkasso.inkasso.whatever
os.chdir('..') # <<<---This is what you want to add
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mvod.dev_settings")
....
关于python - Django 找到测试但无法导入它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23854677/
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
我正在编写一个包含C扩展的gem。通常当我写一个gem时,我会遵循TDD的过程,我会写一个失败的规范,然后处理代码直到它通过,等等......在“ext/mygem/mygem.c”中我的C扩展和在gemspec的“扩展”中配置的有效extconf.rb,如何运行我的规范并仍然加载我的C扩展?当我更改C代码时,我需要采取哪些步骤来重新编译代码?这可能是个愚蠢的问题,但是从我的gem的开发源代码树中输入“bundleinstall”不会构建任何native扩展。当我手动运行rubyext/mygem/extconf.rb时,我确实得到了一个Makefile(在整个项目的根目录中),然后当
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
我有一个围绕一些对象的包装类,我想将这些对象用作散列中的键。包装对象和解包装对象应映射到相同的键。一个简单的例子是这样的:classAattr_reader:xdefinitialize(inner)@inner=innerenddefx;@inner.x;enddef==(other)@inner.x==other.xendenda=A.new(o)#oisjustanyobjectthatallowso.xb=A.new(o)h={a=>5}ph[a]#5ph[b]#nil,shouldbe5ph[o]#nil,shouldbe5我试过==、===、eq?并散列所有无济于事。
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e