鼻子不运行测试
Nose does not run tests
假设您有一个名为 A 的 python 包,其目录结构如下
A
├── B.py
└── __init__.py
其中__init__.py
为空,B.py
的内容由
给出
def test_B():
assert False
运行 nose on the simple package above the test
$ nosetests A
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
必须运行
$ nosetests A/B.py
为了赶上考试,但是那
如果 A 中有一个复杂的子模块结构,很快就会变得笨拙。
如何在包 A 中创建 运行 所有以 'test' 开头的函数,而不必指定它们出现的每个文件?
正如@sobolevn 指出的那样,鼻子遵循 convention about filenames:
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).
例如,您可以将 B.py
重命名为 test_B.py
。
使用--all-modules
标志:
$ nosetests A --all-modules
今天 python 3.8 遇到了同样的问题。
事实证明,在测试文件夹的根目录中添加文件 setup.cfg
可以解决问题。
示例:
|code
| | __init__.py
| | A.py
|tests
| | setup.cfg
| | test_A.py
setup.cfg
的内容应该是:
[nosetests]
traverse-namespace=1
现在 运行 nosetests tests
可以工作了。
假设您有一个名为 A 的 python 包,其目录结构如下
A
├── B.py
└── __init__.py
其中__init__.py
为空,B.py
的内容由
def test_B():
assert False
运行 nose on the simple package above the test
$ nosetests A
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
必须运行
$ nosetests A/B.py
为了赶上考试,但是那 如果 A 中有一个复杂的子模块结构,很快就会变得笨拙。
如何在包 A 中创建 运行 所有以 'test' 开头的函数,而不必指定它们出现的每个文件?
正如@sobolevn 指出的那样,鼻子遵循 convention about filenames:
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).
例如,您可以将 B.py
重命名为 test_B.py
。
使用--all-modules
标志:
$ nosetests A --all-modules
今天 python 3.8 遇到了同样的问题。
事实证明,在测试文件夹的根目录中添加文件 setup.cfg
可以解决问题。
示例:
|code
| | __init__.py
| | A.py
|tests
| | setup.cfg
| | test_A.py
setup.cfg
的内容应该是:
[nosetests]
traverse-namespace=1
现在 运行 nosetests tests
可以工作了。