Python: 鼻子没有接受新的测试
Python: Nose not picking up new test
我刚刚向我的 Python 模块之一添加了一个单元测试,但 nose 拒绝接收。测试看起来像这样:
class TestMargin(unittest.TestCase):
def setUp(self):
super().setUp()
def test_margin(self):
self.assertTrue(False)
我预计这个测试会失败,但它似乎甚至都没有被接受。我 运行 nose 带有 --all-modules
标志,其他模块中的测试确实正在被提取,所以它似乎不是 nose 的普遍问题。我已经检查了 nose 的文档,但看不出有任何充分的理由说明这个测试不应该工作...
编辑:我通过(有点随意地)在模块级别添加一个空 __init__.py
来解决问题。但这为什么会有帮助?
您的测试看起来不错,但您必须将它放在正确文件名的正确位置,以便 nose 发现它。
你可以通过 运行 和 -vvv
看到鼻子的内部工作
默认情况下,nose 通过检查两件事来决定是否进入您的目录结构:它是一个包吗?如果是,nose 将继续查看目录以进行更多测试。 a package in python 意味着拥有 __init__.py
个文件。这就是添加 __init__.py
解决问题的原因。
如果目录不是包,nose 可能仍想查看它,如果它看起来像测试目录,即 tests
或者它看起来像源目录,即 src
或 lib
。但我想情况并非如此。
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). In addition, all other packages found in the working directory will be examined for python source files or directories that match testMatch. Package discovery descends all the way down the tree, so package.tests and package.sub.tests and package.sub.sub2.tests will all be collected.
此外,nose 只是一堆 python 文件,因此您可以随时查看代码以了解它如何发现测试。在这种情况下,您可能需要查看 selector.py
中的 wantDirectory() 方法
如果您的命名正确但文件仍未被选中,可能的原因之一是您的测试文件对其具有可执行权限 -rwxrwxr-x。修改权限为-rw-rw-r--。 Python Nose 不挑选具有可执行权限的文件。
如果可执行标志被错误地添加到版本控制中,请按如下方式删除权限
Git
通过
查找权限
git ls-files --stage | grep file_name
删除执行权限然后提交
git update-index --chmod=-x file_path
SVN
如果存在 svn 属性,可以通过
列出
svn proplist test_file_path
属性删除
svn propdel svn:executable test_file_path
我刚刚向我的 Python 模块之一添加了一个单元测试,但 nose 拒绝接收。测试看起来像这样:
class TestMargin(unittest.TestCase):
def setUp(self):
super().setUp()
def test_margin(self):
self.assertTrue(False)
我预计这个测试会失败,但它似乎甚至都没有被接受。我 运行 nose 带有 --all-modules
标志,其他模块中的测试确实正在被提取,所以它似乎不是 nose 的普遍问题。我已经检查了 nose 的文档,但看不出有任何充分的理由说明这个测试不应该工作...
编辑:我通过(有点随意地)在模块级别添加一个空 __init__.py
来解决问题。但这为什么会有帮助?
您的测试看起来不错,但您必须将它放在正确文件名的正确位置,以便 nose 发现它。
你可以通过 运行 和 -vvv
默认情况下,nose 通过检查两件事来决定是否进入您的目录结构:它是一个包吗?如果是,nose 将继续查看目录以进行更多测试。 a package in python 意味着拥有 __init__.py
个文件。这就是添加 __init__.py
解决问题的原因。
如果目录不是包,nose 可能仍想查看它,如果它看起来像测试目录,即 tests
或者它看起来像源目录,即 src
或 lib
。但我想情况并非如此。
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). In addition, all other packages found in the working directory will be examined for python source files or directories that match testMatch. Package discovery descends all the way down the tree, so package.tests and package.sub.tests and package.sub.sub2.tests will all be collected.
此外,nose 只是一堆 python 文件,因此您可以随时查看代码以了解它如何发现测试。在这种情况下,您可能需要查看 selector.py
中的 wantDirectory() 方法如果您的命名正确但文件仍未被选中,可能的原因之一是您的测试文件对其具有可执行权限 -rwxrwxr-x。修改权限为-rw-rw-r--。 Python Nose 不挑选具有可执行权限的文件。
如果可执行标志被错误地添加到版本控制中,请按如下方式删除权限
Git
通过
查找权限git ls-files --stage | grep file_name
删除执行权限然后提交
git update-index --chmod=-x file_path
SVN
如果存在 svn 属性,可以通过
列出svn proplist test_file_path
属性删除
svn propdel svn:executable test_file_path