【pytest】用例运行规则

  • 前言:当我们使用 pytest 框架写用例的时候,一定要按它的命名规范去写用例,这样框架才能找到哪些是用例需要执行,哪些不需要执行

  • 用例设计原则

    • 文件名以 test_.py 文件呾_test.py
    • 以 test_开头的函数
    • 以 Test 开头的类
    • 以 test_开头的方法
    • 所有的包 pakege 必项要有__init__.py 文件
  • help帮助

    • pytest -h或者pytest --help查看 pytest 命令行参数
H:\pytest练习>pytest -h
usage: pytest [options] [file_or_dir] [file_or_dir] [...]

positional arguments:
  file_or_dir

general:
  -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', while -k 'not test_method'
                        matches those that don't contain 'test_method' in their names. -k 'not test_method and not test_other' will eliminate the matches. 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. The matching is case-
                        insensitive.
  -m MARKEXPR           only run tests matching given mark expression.
                        For 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.
  --fixtures, --funcargs
                        show available fixtures, sorted by plugin appearance (fixtures with leading '_' are only shown with '-v')
  --fixtures-per-test   show fixtures per test
  --pdb                 start the interactive Python debugger on errors or KeyboardInterrupt.
  --pdbcls=modulename:classname
                        start a custom interactive Python debugger on errors. For example: --pdbcls=IPython.terminal.debugger:TerminalPdb
  --trace               Immediately break when running each test.
  --capture=method      per-test capturing method: one of fd|sys|no|tee-sys.
  -s                    shortcut for --capture=no.
  --runxfail            report the results of xfail tests as if they were not marked
  --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.
  --nf, --new-first     run tests from new files first, then the rest of the tests sorted by file mtime
  --cache-show=[CACHESHOW]
                        show cache contents, don't perform collection or tests. Optional argument: glob (default: '*').
  --cache-clear         remove all cache contents at start of test run.
  --lfnf={all,none}, --last-failed-no-failures={all,none}
                        which tests to run with no previously (known) failures.
  --sw, --stepwise      exit on test failure and continue from last failing test next time
  --sw-skip, --stepwise-skip
                        ignore the first failing test but stop on the next failing test
  --allure-severities=SEVERITIES_SET
                        Comma-separated list of severity names.
                        Tests only with these severities will be run.
                        Possible values are: blocker, critical, normal, minor, trivial.
  --allure-epics=EPICS_SET
                        Comma-separated list of epic names.
                        Run tests that have at least one of the specified feature labels.
  --allure-features=FEATURES_SET
                        Comma-separated list of feature names.
                        Run tests that have at least one of the specified feature labels.
  --allure-stories=STORIES_SET
                        Comma-separated list of story names.
                        Run tests that have at least one of the specified story labels.
  --allure-link-pattern=LINK_TYPE:LINK_PATTERN
                        Url pattern for link type. Allows short links in test,
                        like 'issue-1'. Text will be formatted to full url with python
                        str.format().

#仅粘贴部分
  • 执行用例规范
#test_demo.py
import pytest
class TestClass:
    def test_one(self):
        x = "this"
        assert 'h' in x

    def test_two(self):
        x = "hello"
        assert hasattr(x, 'check')

    def test_three(self):
        a = "hello"
        b = "hello world"
        assert a in b

#test_sample.py
import pytest

def func(x):
    return x + 1

def test_answer():
    assert func(3) == 5
  • 1、执行某个目录下所有的用例
    • pytest 文件名/
  • 2、执行某一个 py 文件下用例
    • pytest XXX.py
  • 3、-k 按关键字匹配
    • pytest -k "MyC l ass and not method "
    • 行包含不给定字符串表达式匹配的名称的测试,其中包括 Python 使用文件名,类名、函数名作为变量的运算符。上面的例子将运行TestMyClass.test_something 但不会运行TestMyClass.test_method_simple
  • 4、按节点运行
    • 运行.py 模块里面的某个函数:pytest test_mod.py::test_func
    • 运行.py 模块里面,测试类里面的某个方法:pytest test_mod.py::TestC l ass::test_method
  • 5、标记表达式
    • pytest -m slow 将运行用@ pytest.mark.slow装饰器修饰的所有测试
  • 6、-X,遇到错误时停止运行
    • 本来有 3 个用例,第二个用例失败后就没继续往下执行了
  • 7、—maxfail = num 当用例错误个数达到指定数量时,停止测试
posted @ 2022-08-02 21:00  Tony_xiao  阅读(296)  评论(0编辑  收藏  举报