我确实有几个小模块,其中的测试位于其中,并且 py.test
或 nose
不会查找它们,因为它们不包含 test
在他们的文件名中。
我怎样才能说服py.test
或nose
在所有python文件中递归地寻找测试 - '''包括那些没有test 在他们的文件名'''?
在源文件中,我确实保留了标准命名约定:class testSomeName
和方法def test_some_name
。
如果这是不可能的,我可以使用什么其他解决方案来获得相同的结果。
我不想手动创建包含测试的所有文件的列表,我想要一个支持发现的解决方案。
I do have several small modules where the tests are inside them and py.test
or nose
does not look for them because they do not contain test
in their filename.
How can I convince py.test
or nose
to look for tests inside all python files, recursively - '''including the ones that do not have test
in their filenames'''?
Inside the source files I do keep the standard naming convention: class testSomeName
with methods def test_some_name
.
If this is not possible, what other solution can I use to obtain the same result.
I do not want to manually create a list of all files containing the test, I want a solution that supports discovery.
发布评论
评论(5)
对于 pytest-6,我必须更改 @hpk42 答案以直接返回给定源/测试代码布局的模块实例。最初的答案对这个主要版本的 pytest 提出了例外,因为 API 同时发生了变化。
For pytest-6 I had to change @hpk42 answer to directly return a Module instance for the given source/test code layout. The original answer raised an exception for this major version of pytest, as the API has changed in the meantime.
文档 说
您能确保您的代码就是这种情况吗?
更新
(买者自负:我还没有尝试/测试过这个)如何使用hooks 提供用于收集目录和文件吗?
The documentation says that
Can you ensure that this is the case with your code?
Update
(Caveat Emptor: I haven't tried/tested this) How about using the hooks provided for collecting directories and files?
使用 py.test 就很简单了。创建一个包含以下内容的
conftest.py
文件:这将扩展收集过程,为每个“.py”文件创建一个测试“模块”节点。将其放入
conftest.py
文件中使其成为特定于项目的扩展,如果您键入以下内容,则会自动加载该扩展:出于信息目的,您还可以键入:
以查看收集了哪些测试和文件,示例输出:
如果需要,您还可以将上述
conftest.py
文件打包为 可安装插件 并通过安装插件使扩展可用。在这种情况下,您根本不需要任何conftest.py
文件。With py.test it's simple. Create a
conftest.py
file with this content:This will extend the collection process to create a test "Module" node for each ".py" file. Putting this into a
conftest.py
file makes it a project-specific extension which is automatically loaded if you type:For informational purposes you can also type:
to see what tests and files are collected, example output:
If needed you can also package the above
conftest.py
file as an installable plugin and make the extension available by installing the plugin. In this case you do not need anyconftest.py
file at all.您还可以查看 Nose ,它会发现测试,而无需使用固定的文件名约定。
您可以使用以下代码绕过鼻子中用于过滤文件的正则表达式。
创建一个 python 模块(即
my_nosetests.py
)请注意,您实际上正在加载所有模块并在其中搜索测试。注意模块加载的任何副作用。
You can also have a look at Nose which will discover tests without having to use a fixed file name convention.
You can bypass the regexp used to filter files in nose with the following code.
Create a python module (i.e.
my_nosetests.py
)Now run
my_nosetests.py
as if you were runningnosetests
and you should have your tests running. Be aware that you are in fact loading all modules and searching for tests in them. Beware of any side effect of module loading.将文件“setup.cfg”放在项目的根目录下,其中包含以下两行:
然后 py.test 从所有
*.py
文件中选择测试。这里有解释:pytest docswith Nose:
Put a file "setup.cfg" at the root of project, and it contains this two lines:
then py.test select tests from all
*.py
files. Here it's explained: pytest docswith Nose: