使用 pytest --collect-only 仅返回单个测试名称 [英] using pytest --collect-only to only return individual test names

查看:107
本文介绍了使用 pytest --collect-only 仅返回单个测试名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试整理如何使用 pytest 从测试文件中获取仅包含测试名称的列表,到目前为止我已经做到了:

I am trying to sort out how to get a list of just the test names from a test file using pytest I have made it this far:

$ pytest --collect-only my_test_file.py
============================================================================================= test session starts ==============================================================================================
platform darwin -- Python 3.6.4, pytest-3.2.5, py-1.5.2, pluggy-0.4.0
rootdir: /Users/.../automation, inifile:
plugins: xdist-1.20.1, metadata-1.7.0, html-1.17.0, forked-0.2, cloud-2.0.0
collected 6 items
<Module 'my_test_file.py'>
  <UnitTestCase 'TestFile'>
    <TestCaseFunction 'test_general'>
    <TestCaseFunction 'test_search_0'>
    <TestCaseFunction 'test_search_1'>
    <TestCaseFunction 'test_search_2'>
    <TestCaseFunction 'test_search_3'>
    <TestCaseFunction 'test_search_4'>

虽然这是一个好的开始,但信息太多.我应该传递什么来获取测试名称本身的列表?

While this is a good start it is too much information. What should I be passing to just get back a list of the test names themselves?

推荐答案

使用 --quiet 更改集合输出.

Use --quiet to change the collection output.

$ pytest --collect-only -q
test_eggs.py::test_bacon[1]
test_eggs.py::test_bacon[2]
test_eggs.py::test_bacon[3]
test_spam.py::test_foo
test_spam.py::test_bar

no tests ran in 0.03 seconds

你可以用

$ pytest --collect-only -q | head -n -2

当使用两次(或更多)时,--quiet 将打印每个模块的测试数量:

When used twice (or more), --quiet will print the amount of tests per module:

$ pytest --collect-only -qq
test_eggs.py: 3
test_spam.py: 2

这篇关于使用 pytest --collect-only 仅返回单个测试名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆