运行pytest-命令行参数

运行pytest-命令行参数

使用pytest框架编写的测试用例是可以直接在命令行运行的,下面以简单的测试用例来说明命令行运行下的不同参数的含义。

-h | --help

显示帮助信息,以及参数列表
常用参数列表

  -k EXPRESSION         only run tests which match the given substring
                        expression. An expression is a python evaluatable
                        expression where all names are substring-matched
                        against test names and their parent classes. Example:
                        -k 'test_method or test_other' matches all test
                        functions and classes whose name contains
                        'test_method' or 'test_other'. Additionally keywords
                        are matched to classes and functions containing extra
                        names in their 'extra_keyword_matches' set, as well as
                        functions which have names assigned directly to them.
  -m MARKEXPR           only run tests matching given mark expression.
                        example: -m 'mark1 and not mark2'.
  --markers             show markers (builtin, plugin and per-project ones).
  -x, --exitfirst       exit instantly on first error or failed test.
  --maxfail=num         exit after first num failures or errors.
  --strict              marks not registered in configuration file raise
                        errors.
  -c file               load configuration from `file` instead of trying to
                        locate one of the implicit configuration files.
  -s                    shortcut for --capture=no.
  --lf, --last-failed   rerun only the tests that failed at the last run (or
                        all if none failed)
  --ff, --failed-first  run all tests but run the last failures first. This
                        may re-order tests and thus lead to repeated fixture
                        setup/teardown
  -v, --verbose         increase verbosity.
  -q, --quiet           decrease verbosity.
  -r chars              show extra test summary info as specified by chars
                        (f)ailed, (E)error, (s)skipped, (x)failed, (X)passed,
                        (p)passed, (P)passed with output, (a)all except pP.
                        Warnings are displayed at all times except when
                        --disable-warnings is set
  --disable-warnings, --disable-pytest-warnings
                        disable warnings summary

test_demo.py

“”“Tesing demo”“”

from collections import nametuple		# 命名元祖

Task = nametuple('Task', ['summary', 'owner', 'done', 'id'])
Task.__new__.__defaults__ = (None, None, False, None)

def test_defaults():
	t1 = Task()
    t2 = Task(None, None, False, None)
    assert t1 == t2

no parameters

最简单执行测试用例的命令行就是:在当前目录下直接运行 pytest``

$ cd ~/Documents/my_code/aaaTest/blog 
$ pytest
============================================================================== test session starts ===============================================================================
platform darwin -- Python 3.7.8, pytest-3.2.1, py-1.10.0, pluggy-0.4.0
rootdir: /Users/roy/Documents/my_code/aaaTest/blog, inifile:
collected 1 item

test_demo.py .

============================================================================ 1 passed in 0.01 seconds ============================================================================

当前目录下只有 test_demo.py 一个测试文件,运行 pytest 时可以指定目录和文件,如果没有指定, pytest 命令会默认搜索当前目录及其子目录中以 test_ 开头或以 _test 结尾的测试函数。

–collect-only

使用 --collect-only 选项可以真实在给定的配置下哪些测试用例会被运行。

$ cd ~/Documents/my_code/aaaTest/blog
$ pytest --collect-only
============================================================================== test session starts ===============================================================================
platform darwin -- Python 3.7.8, pytest-3.2.1, py-1.10.0, pluggy-0.4.0
rootdir: /Users/roy/Documents/my_code/aaaTest/blog, inifile:
collected 1 item
<Module 'test_demo.py'>
  <Function 'test_default'>

========================================================================== no tests ran in 0.02 seconds ==========================================================================

--collect-only 选项可以让你非常方便在测试运行之前,检查选中的测试用例是否符合预期。

-k

-k EXPRESSION
-k 选项允许你使用表达式指定希望运行的测试用例。
此选项在多个测试函数名称前缀或后缀是相同的时候,是非常适用的,首先在 test_demo.py 中添加测试用例

...
def test_defaults_add():
    assert 1+2 == 2+1

接着使用 -k 参数来结合上面的 --collect-only 参数来检查测试用例

$ pytest -k 'defaults' --collect-only
============================================================================== test session starts =================
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值