“没有代码来源” Coverage.py 中的消息

发布于 2024-08-23 18:07:06 字数 565 浏览 8 评论 0原文

我昨晚成功运行了一次构建。我今天早上起床并运行了另一个,没有更改任何配置或修改任何源代码。现在,在使用覆盖率运行我的 nosetests 时,我的构建失败,并显示消息“无代码源”

NoSource: No source for code: '/home/matthew/.hudson/jobs/myproject/workspace/tests/unit/util.py'
. . . 
No source for code: '/home/matthew/.hudson/jobs/myproject/workspace/__init__.py'

我唯一的线索是它说找不到的文件不存在,但它们从来不存在,也不应该存在。例如,在后者中,Hudson 的工作区不是 Python 模块,因此 __init__.py 不会在那里。

更新:我已确认这不是 Hudson 问题。当我运行 nostests 并覆盖目录本身时,我看到类似的消息。同样,报道所寻找的文件从一开始就不存在,这使得这非常令人费解。

I ran a build last night, successfully. I got up this morning and ran another without changing any configuration or modifying any source code. Now my build is failing with the message "No source for code" when running my nosetests with coverage.

NoSource: No source for code: '/home/matthew/.hudson/jobs/myproject/workspace/tests/unit/util.py'
. . . 
No source for code: '/home/matthew/.hudson/jobs/myproject/workspace/__init__.py'

The only clue I have is that the files it says it can't find aren't there, but they never were and they're not supposed to be. For example, in the latter, Hudson's workspace isn't a Python module, so __init__.py wouldn't be there.

Update: I've confirmed that this isn't a Hudson issue. When I run nostests with coverage in the directory itself, I see similar messages. Again, the files that coverage is looking for were never there to begin with, making this very puzzling.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

月下凄凉 2024-08-30 18:07:07

摘要:运行 nosetests --with-coverage 时,现有的 .coverage 数据会保留下来,因此请先将其删除。

详细信息:我也刚刚通过Hudson和nosetests遇到了这个问题。此错误来自 coverage/results.py:18(coverage 3.3.1 - 有 3 个地方引发此错误,但这是相关的)。它试图打开与实际跟踪的模块相对应的 .py 文件。一个小演示:

$ echo print > hello.py
$ echo import hello > main.py
$ coverage run main.py

$ rm hello.py
$ coverage xml
No source for code: '/tmp/aoeu/hello.py'

显然我有一个被执行/跟踪的文件 stopwords.pyc,但没有 stopwords.py。然而,我的代码中没有任何地方导入停用词,甚至删除 .pyc 我仍然收到错误。

然后,一个简单的 strings.coverage 显示对 stopwords.py 的引用仍然存在。 nosetests --with-coverage 正在使用覆盖率的附加或合并功能,这意味着旧的 .coverage 数据仍然存在。事实上,删除 .coverage 解决了这个问题。

Summary: Existing .coverage data is kept around when running nosetests --with-coverage, so remove it first.

Details: I too just encountered this via Hudson and nosetests. This error was coming from coverage/results.py:18 (coverage 3.3.1 - there were 3 places raising this error, but this was the relevant one). It's trying to open the .py file corresponding to the module that was actually traced. A small demo:

$ echo print > hello.py
$ echo import hello > main.py
$ coverage run main.py

$ rm hello.py
$ coverage xml
No source for code: '/tmp/aoeu/hello.py'

Apparently I had a file stopwords.pyc that was executed/traced, but no stopwords.py. Yet nowhere in my code was I importing stopwords, and even removing the .pyc I still got the error.

A simple strings .coverage then revealed that the reference to stopwords.py still existed. nosetests --with-coverage is using coverage's append or merge functionality, meaning the old .coverage data still lingers around. Indeed, removing .coverage addressed the issue.

孤寂小茶 2024-08-30 18:07:07

只需使用“--cover-erase”参数即可。它修复了此错误,您不必手动删除覆盖文件

nosetests --with-coverage --cover-erase

我强烈建议您查看帮助以查看您还缺少哪些其他参数,也不要忘记这些插件

Just use the '--cover-erase' argument. It fixes this error and you don't have to manually delete coverage files

nosetests --with-coverage --cover-erase

I'd strongly recommend checking out the help to see what other args you're missing too and don't forget those plugins either

护你周全 2024-08-30 18:07:07

问题是 .pyc 文件仍然存在。

一个快速但肮脏的解决方案是删除该目录中的所有 .pyc 文件:

find . -name "*.pyc" -exec rm -rf {} \;

The problem is that the .pyc file still exists.

A quick and dirty solution is to delete all .pyc files in that directory:

find . -name "*.pyc" -exec rm -rf {} \;
殊姿 2024-08-30 18:07:07

当我尝试通过 setuptools 运行鼻子测试覆盖范围时,我也遇到了这个问题。如前所述,可以删除现有的 .pyc 文件,但这可能很麻烦。

我最终不得不使用以下内容创建一个 .coveragerc 文件

[报告]

ignore_errors = True

修复此错误。

I ran into this problem as well when trying to run nosetests coverage through setuptools. As mentioned, it is possible to delete existing .pyc files but that can be cumbersome.

I ended up having to create a .coveragerc file with the following

[report]

ignore_errors = True

to fix this error.

挽清梦 2024-08-30 18:07:07

coverage reports -m 可以这样调用,无需提供任何参数(请参阅 官方快速说明)。
但它适用于第一个脚本覆盖范围,而不适用于第二个脚本。例如,

coverage run -m f1.py
coverage report -m # works
coverage run -m f2.py
coverage report -m # fails (f2.py instead of f1.py in last coverage run)

相反,始终将脚本指示为覆盖率报告-m的参数:

file="f2.py" && coverage run $file && coverage report -m $file

覆盖率报告文档

coverage report -m can be called just so, without providing any argument (see official quick instructions).
But it works on 1st script coverage-ed, not on 2nd. E.g.

coverage run -m f1.py
coverage report -m # works
coverage run -m f2.py
coverage report -m # fails (f2.py instead of f1.py in last coverage run)

Instead, always indicate script as argument of coverage report -m:

file="f2.py" && coverage run $file && coverage report -m $file

Coverage reporting docs

素手挽清风 2024-08-30 18:07:07

这个问题是 13 年前提出的,但今天我也遇到了这个问题,对我来说,有效的方法是删除 .coverage 文件并再次运行覆盖率。

尽管接受的答案有效,但我不建议这样做,因为它可能隐藏您可能遇到的一些其他问题。

我怀疑对于某些人来说,对重命名文件的引用留在了索引中。

This question was made 13 years ago but today I faced this issue as well and to me, what worked was to delete the .coverage file and run coverage again.

Although the accepted answer works, I don't recommend to do it since it might hide some other issues you might have.

I suspect that for some the reference to a renamed file was left in the index.

尴尬癌患者 2024-08-30 18:07:07

也许这会有所帮助,但我今天遇到了类似的错误。这是一个权限错误。我的代码正在使用另一个用户的结帐(按照设计,向下询问),我需要 sudo 才能使覆盖范围正常工作。所以你的问题可能有一定的道理。

Maybe this will help, but I ran into a similar error today. And it's a permission error. My code is using a checkout from another user (by design, down ask) and I need to sudo in order for coverage to work. So your issue may have something to it.

初相遇 2024-08-30 18:07:07

我遇到了这个问题。 pytest-cov 声称现有文件中没有包含有效和覆盖代码的代码。我只是通过删除 .coverage 文件来删除这些警告。当然,它会在下次运行时重新创建。

I had this problem. pytest-cov claimed there is no code for existing files full of valid and covered code. I removed those warnings just by removing .coverage file. It is of course recreated on next runs.

双马尾 2024-08-30 18:07:07

又是一个例子,但无论如何......
不要像我一样愚蠢,只使用 coverage html,而不是 coverage report html

A bit another case, but anyway...
Don't be foolish as me, use just coverage html, not coverage report html

尐籹人 2024-08-30 18:07:07

我遇到了这个问题,解决方法是删除 .pytest_cache 文件夹和 *.pyc 文件

I had this problem, and what solved it was removing the .pytest_cache folder and the *.pyc files

画中仙 2024-08-30 18:07:06

我不确定为什么它认为该文件存在,但您可以使用 coverage xml -i 开关告诉coverage.py 忽略这些问题。

如果您想找出错误,请给我写信(ned at ned batelder com)。

I'm not sure why it thinks that file exists, but you can tell coverage.py to ignore these problems with a coverage xml -i switch.

If you want to track down the error, drop me a line (ned at ned batchelder com).

缪败 2024-08-30 18:07:06

确保那里没有过去可能存在的 .pyc 文件。

Ensure theres no .pyc file there, that may have existed in the past.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文