python 鼻子无法在 ubuntu 中找到测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1457104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Nose unable to find tests in ubuntu
提问by Sudhir Jonathan
Is there any reason why Nose wouldn't be able to find tests in Ubuntu 9.04?
有什么理由为什么 Nose 无法在 Ubuntu 9.04 中找到测试吗?
I'm using nose 0.11.1 with python 2.5.4.
I can run tests only if I explicitly specify the filename.
If I don't specify the filename it just says, 0 tests.
我在 python 2.5.4 中使用鼻子 0.11.1。
仅当我明确指定文件名时,我才能运行测试。如果我不指定文件名,它只是说0 tests。
The same project runs tests fine on my Mac, so I'm quite stumped!
同一个项目在我的 Mac 上运行测试很好,所以我很难过!
采纳答案by MITjanitor
Some what related, if you're running tests off of a directory i.e
一些相关的,如果你在一个目录外运行测试,即
nosetests ... tests/
where tests is the name of the folder with my tests, and have separate python test functions in one of the .py modules... Your functions have to start with 'test' for nosetests to recognize that as a test you want to run.
其中,tests 是包含我的测试的文件夹的名称,并且在其中一个 .py 模块中具有单独的 python 测试函数...您的函数必须以“test”开头,以便鼻子测试将其识别为您想要运行的测试。
for example:
例如:
def test_something():
...
nosetests will run this function when executed in this directory while
在此目录中执行时,nosetests 将运行此函数,而
def somethin_to_test():
...
would not.
不会。
回答by David Wolever
The other thing which alwaysgets me with nose
is that it won't run tests in executable files. I'm not exactly sure why that would make a difference across Mac/Ubuntu, but it's worth a shot.
另一件总是让我感到困惑的事情nose
是它不会在可执行文件中运行测试。我不确定为什么这会对 Mac/Ubuntu 产生影响,但值得一试。
Make sure that the scripts didn't somehow get chmod +x
'd on the Mac… And if they did, fix them with chmod -x $(find tests/ -name '*.py')
.
确保脚本不会以某种方式chmod +x
在 Mac 上运行……如果出现了,请使用chmod -x $(find tests/ -name '*.py')
.
回答by Mark Rushakoff
This behavior is almost certainly because your files are not named in accordance with nose's test matching behavior. From the nose docs:
这种行为几乎可以肯定是因为您的文件没有根据鼻子的测试匹配行为命名。从鼻子文档:
nose collects tests automatically from python source files, directories and packages found in its working directory (which defaults to the current working directory). Any python source file, directory or package that matches the testMatch regular expression (by default: (?:^|[b_.-])[Tt]est) will be collected as a test(or source for collection of tests).
鼻子从其工作目录(默认为当前工作目录)中找到的python源文件、目录和包中自动收集测试。任何与 testMatch 正则表达式(默认情况下:(?:^|[b_.-])[Tt]est)匹配的 python 源文件、目录或包都将被收集为测试(或测试集合的源)。
Emphasis was mine.
重点是我的。
Some example names that would match:
一些匹配的示例名称:
- TestFoo.py
- Foo-Test.py
- Foo_Test.py
- Foo.Test.py (note that this one will try to import Foo, and will raise an exception if it cannot)
- 测试文件
- Foo-Test.py
- Foo_Test.py
- Foo.Test.py(注意,这个将尝试导入 Foo,如果不能,将引发异常)
A name that lookslike it would match, but actually does not:
一个看似匹配但实际上不匹配的名称:
- FooTest.py
- 测试文件
If you just rename your files you should be good to go.
如果你只是重命名你的文件,你应该很高兴。
Update更新:我无法从您发布的详细信息中得知,但也许您的测试目录丢失了它们的
__init__.py
__init__.py
文件?... make sure that your “tests” directories are actually modules (they have an empty
__init__.py
file).
...确保您的“测试”目录实际上是模块(它们有一个空
__init__.py
文件)。
回答by Rocky
I had the same problem. My tests ran just fine in Windows, but not in Ubuntu.
我有同样的问题。我的测试在 Windows 中运行得很好,但在 Ubuntu 中却没有。
In Ubuntu, if you run:
在 Ubuntu 中,如果你运行:
nosetests -vv --collect-only
You'll probably see that it's skipping your test file because it's an executable: _Tools/LintControlFiles/test_HgLint.py is executable; skipped
您可能会看到它正在跳过您的测试文件,因为它是一个可执行文件:_Tools/LintControlFiles/test_HgLint.py是可执行文件;跳过
In order to get nose to consider executables, run it like this:
为了让鼻子考虑可执行文件,请像这样运行它:
nosetests --exe
回答by zahanm
I can confirm that as @david-wolever said, they cannotbe executable on Ubuntu. Run
我可以确认,正如@david-wolever 所说,它们不能在 Ubuntu 上执行。跑
nosetests -vv --collect-only
to see full details on which files were examined.
查看有关检查哪些文件的完整详细信息。
回答by georgeok
Use the -all-modules
and it will find all the tests.
使用-all-modules
,它将找到所有测试。
nosetests --all-modules ./tests
nosetests --all-modules ./tests
回答by seaders
After looking through the source of nose, specifically the selector.py file, if you look at what's happening,
在查看了鼻子的来源,特别是 selector.py 文件之后,如果你看看发生了什么,
https://github.com/nose-devs/nose/blob/master/nose/selector.py#L129
https://github.com/nose-devs/nose/blob/master/nose/selector.py#L129
When checking if we wantFile
, self.matches
is called, which then does a regex
search against the match
, which is what you would have passed in as testMatch
.
当检查 we 时wantFile
, ,self.matches
被调用,然后对 进行regex
搜索match
,这就是您将作为 传入的内容testMatch
。
The problem occurs when you then check later down (and, throughout that file),
当您稍后检查(以及在整个文件中)时,就会出现问题,
https://github.com/nose-devs/nose/blob/master/nose/selector.py#L152
https://github.com/nose-devs/nose/blob/master/nose/selector.py#L152
It runs the very same type of checks again, against wantFunction
.
它再次针对wantFunction
.
This means, if you've got a different structure for your package, your container pyfile, and your actual test class / function, you'll have to create a crazy complicated regex to match that at every stage.
这意味着,如果你的包、你的容器 pyfile 和你的实际测试类/函数有不同的结构,你将不得不创建一个疯狂的复杂正则表达式来匹配每个阶段。
For me, when I learned this, I chose to prefix my package, container and test functions with a common bit, i.e.
对我来说,当我了解到这一点时,我选择在我的包、容器和测试函数前面加上一个共同的位,即
setests
├── __init__.py
├── setest_area1.py
└──── def setest_someblock(): ...
setests
├── __init__.py
├── setest_area1.py
└──── def setest_someblock(): ...
And then my nose
command works like,
然后我的nose
命令就像,
nose --testMatch="setest"
nose --testMatch="setest"
This then filters the way I expect it to work.
这然后过滤了我期望它的工作方式。