pytest命令执行测试用例

测试代码

from collections import namedtuple
Task = namedtuple('Task', ['summary','owner','done','id'])
# __new__.__defaults__创建默认的Task对象
Task.__new__.__defaults__ = (None, None, False, None)


def test_default():
    """
    如果不传任何参数,则默认调用缺省对象Task.__new__.__defaults__ = (None, None, False, None)
    """
    t1 = Task()
    t2 = Task(None, None, False, None)
    assert t1 == t2


def test_member_access():
    """
    利用属性名来访问对象成员
    :return: 
    """
    t = Task('buy milk', 'brian')
    assert t.summary == 'buy milk'
    assert t.owner == 'brian'
    assert(t.done,  t.id) == (False, None)


def test_asdict():
    """
    _asdict()返回一个字典
    """
    t_task = Task('do something','okken',True, 21)
    t_dict = t_task._asdict()
    expected_dict = {'summary': 'do something',
                     'owner': 'okken',
                     'done': True,
                     'id': 21}
    assert t_dict == expected_dict


def test_replace():
    """
    _replace()替换对象成员
    """
    t_before_replace = Task('finish book', 'brian', False)
    t_after_replace = t_before_replace._replace(id=10, done=True)
    t_expected = Task('finish book', 'brian', True, 10)
    assert t_after_replace == t_expected

Pytest执行规则

pytest命令的执行可以加options和file or directory,如果不提供这些参数,pytest会在当前目录及其子目录下寻找测试文件,然后运行搜索到的测试代码;如果提供了一个或者多个文件名、目录,pytest会逐一查找并运行所有测试,为了搜索到所有的测试代码,pytest会递归遍历每个目录及其子目录,但也只是执行以test_开头或者_test开头的测试函数

pytest搜索测试文件和测试用例的过程称为测试搜索,只要遵循如下几条原则便能够被它搜索到

测试文件应命名为test_(something).py或者(something)_test.py
测试函数、测试类方法应命名为test_(something)
测试类应命名为Test(something)---以驼峰形式命名
如果执行上述代码文件,执行结果为:
D:\PythonPrograms\PythonPytest\TestScripts>pytest test_two.py
============================= test session starts =============================
platform win32 -- Python 3.7.2, pytest-4.0.2, py-1.8.0, pluggy-0.12.0
rootdir: D:\PythonPrograms\PythonPytest\TestScripts, inifile:
plugins: allure-adaptor-1.7.10
collected 4 items

test_two.py ....                                                         [100%]

========================== 4 passed in 0.04 seconds ===========================

在执行结果的第一行显示执行代码的操作系统、python版本以及pytest的版本:platform win32 – Python 3.7.2, pytest-4.0.2, py-1.8.0, pluggy-0.12.0
第二行显示搜索代码的启示目录以及配置文件,在本例中没有配置文件,因此inifile为空:rootdir: D:\PythonPrograms\PythonPytest\TestScripts, inifile:
第三行显示pytest插件
第四行 collected 4 items表示找到4个测试函数。
test_two.py … 显示的是测试文件名,后边的点表示测试通过,除了点以外,还可能遇到Failure、error(测试异常)、skip、xfail(预期失败并确实失败)、xpass(预期失败但实际通过,不符合预期)分别会显示F、E、s、x、X
4 passed in 0.04 seconds 则表示测试结果和执行时间
执行单个测试用例

使用命令 pytest -v 路径/文件名::测试用例函数名 执行结果如下:

D:\PythonPrograms\Python_Pytest\TestScripts>pytest -v test_two.py::test_replace
============================= test session starts =============================
platform win32 -- Python 3.7.2, pytest-4.0.2, py-1.8.0, pluggy-0.12.0 -- c:\python37\python.exe
cachedir: .pytest_cache
rootdir: D:\PythonPrograms\Python_Pytest\TestScripts, inifile:
plugins: allure-adaptor-1.7.10
collected 1 item

test_two.py::test_replace PASSED                                         [100%]

========================== 1 passed in 0.04 seconds ===========================

命令行规则如下:
运行某个模块内的某个测试函数:

pytest test_mod.py::test_func
1
运行某个模块内某个类的某个测试方法:文件名::类名::测试用例名

pytest test_mod.py::TestClass::test_method
1
执行单个测试模块

pytest test_module.py
1
执行某个目录下的所有测试

pytest test/
1
–collect-only选项

在批量执行测试用例之前,我们往往会想知道哪些用例将被执行是否符合我们的预期等等,这种场景下可以使用–collect-only选项,如下执行结果所示:

D:\PythonPrograms\Python_Pytest\TestScripts>pytest --collect-only
============================= test session starts =============================
platform win32 -- Python 3.7.2, pytest-4.0.2, py-1.8.0, pluggy-0.12.0
rootdir: D:\PythonPrograms\Python_Pytest\TestScripts, inifile:
plugins: allure-adaptor-1.7.10
collected 17 items
<Package 'D:\\PythonPrograms\\Python_Pytest\\TestScripts'>
  <Module 'test_asserts.py'>
    <Function 'test_add'>
    <Function 'test_add2'>
    <Function 'test_add3'>
    <Function 'test_add4'>
    <Function 'test_in'>
    <Function 'test_not_in'>
    <Function 'test_true'>
  <Module 'test_fixture1.py'>
    <Function 'test_numbers_3_4'>
    <Function 'test_strings_a_3'>
  <Module 'test_fixture2.py'>
    <Class 'TestUM'>
        <Function 'test_numbers_5_6'>
        <Function 'test_strings_b_2'>
  <Module 'test_one.py'>
    <Function 'test_equal'>
    <Function 'test_not_equal'>
  <Module 'test_two.py'>
    <Function 'test_default'>
    <Function 'test_member_access'>
    <Function 'test_asdict'>
    <Function 'test_replace'>

======================== no tests ran in 0.09 seconds =========================

-k选项

该选项允许我们使用表达式指定希望运行的测试用例,如果某测试名是唯一的或者多个测试名的前缀或后缀相同,则可以使用这个选项来执行,如下执行结果所示:

D:\PythonPrograms\Python_Pytest\TestScripts>pytest -k "asdict or default" --collect-only
============================= test session starts =============================
platform win32 -- Python 3.7.2, pytest-4.0.2, py-1.8.0, pluggy-0.12.0
rootdir: D:\PythonPrograms\Python_Pytest\TestScripts, inifile:
plugins: allure-adaptor-1.7.10
collected 17 items / 15 deselected
<Package 'D:\\PythonPrograms\\Python_Pytest\\TestScripts'>
  <Module 'test_two.py'>
    <Function 'test_default'>
    <Function 'test_asdict'>

======================== 15 deselected in 0.06 seconds ========================

从执行结果中我们能看到使用-k和–collect-only组合能够查询到我们设置的参数所能执行的测试方法。
然后我们将–collect-only从命令行移出,只使用-k便可执行test_default和test_asdict了

D:\PythonPrograms\Python_Pytest\TestScripts>pytest -k "asdict or default"
============================= test session starts =============================
platform win32 -- Python 3.7.2, pytest-4.0.2, py-1.8.0, pluggy-0.12.0
rootdir: D:\PythonPrograms\Python_Pytest\TestScripts, inifile:
plugins: allure-adaptor-1.7.10
collected 17 items / 15 deselected

test_two.py ..                                                           [100%]

=================== 2 passed, 15 deselected in 0.07 seconds ===================

如果我们在定义用例名的时候特别注意一下便可以使用-k的方式执行一系列测试用例了

-m选项

用于标记并分组,然后仅执行带有标记的用例,如此便实现了执行某个测试集合的场景,如下代码所示,给我们之前的两个测试方法添加标记


@pytest.mark.run_these_cases
def test_member_access():
    """
    利用属性名来访问对象成员
    :return:
    """
    t = Task('buy milk', 'brian')
    assert t.summary == 'buy milk'
    assert t.owner == 'brian'
    assert(t.done,  t.id) == (False, None)

@pytest.mark.run_these_cases
def test_asdict():
    """
    _asdict()返回一个字典
    """
    t_task = Task('do something','okken',True, 21)
    t_dict = t_task._asdict()
    expected_dict = {'summary': 'do something',
                     'owner': 'okken',
                     'done': True,
                     'id': 21}
    assert t_dict == expected_dict

执行命令:pytest -v -m run_these_cases,结果如下:

D:\PythonPrograms\Python_Pytest\TestScripts>pytest -v -m run_these_cases
============================= test session starts =============================
platform win32 -- Python 3.7.2, pytest-4.0.2, py-1.8.0, pluggy-0.12.0 -- c:\python37\python.exe
cachedir: .pytest_cache
rootdir: D:\PythonPrograms\Python_Pytest\TestScripts, inifile:
plugins: allure-adaptor-1.7.10
collected 17 items / 15 deselected

test_two.py::test_member_access PASSED                                   [ 50%]
test_two.py::test_asdict PASSED                                          [100%]

=================== 2 passed, 15 deselected in 0.07 seconds ===================
-m选项也可以用表达式指定多个标记名,例如-m “mark1 and mark2” 或者-m “mark1 and not mark2” 或者-m “mark1 or mark2”

-x选项

Pytest会运行每一个搜索到的测试用例,如果某个测试函数被断言失败,或者触发了外部异常,则该测试用例的运行就会停止,pytest将其标记为失败后继续运行一下测试用例,然而在debug的时候,我们往往希望遇到失败时立刻停止整个会话,-x选项为我们提供了该场景的支持,如下执行结果所示:

E:\Programs\Python\Python_Pytest\TestScripts>pytest -x
=============================== test session starts ==============================
platform win32 -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0
rootdir: E:\Programs\Python\Python_Pytest\TestScripts
plugins: allure-pytest-2.6.3
collected 17 items                                                                                                                                                                                                                     

test_asserts.py ...F

========================FAILURES =================================================
_________________________test_add4 _____________________________________________________

    def test_add4():
>       assert add(17,22) >= 50
E       assert 39 >= 50
E        +  where 39 = add(17, 22)

test_asserts.py:34: AssertionError
======================== warnings summary ====================================
c:\python37\lib\site-packages\_pytest\mark\structures.py:324
  c:\python37\lib\site-packages\_pytest\mark\structures.py:324: PytestUnknownMarkWarning: Unknown pytest.mark.run_these_cases - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pyt
est.org/en/latest/mark.html
    PytestUnknownMarkWarning,

-- Docs: https://docs.pytest.org/en/latest/warnings.html
===================== 1 failed, 3 passed, 1 warnings in 0.41 seconds ====================

在执行结果中我们可以看到实际收集的测试用例是17条,但执行了4条,通过3条失败一条,执行便停止了。
如果不适用-x选项再执行一次结果如下:


E:\Programs\Python\Python_Pytest\TestScripts>pytest --tb=no
=============================== test session starts ================================
platform win32 -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0
rootdir: E:\Programs\Python\Python_Pytest\TestScripts
plugins: allure-pytest-2.6.3
collected 17 items                                                                                                                                                                                                                     

test_asserts.py ...F..F                                                                                                                                                                                                          [ 41%]
test_fixture1.py ..                                                                                                                                                                                                              [ 52%]
test_fixture2.py ..                                                                                                                                                                                                              [ 64%]
test_one.py .F                                                                                                                                                                                                                   [ 76%]
test_two.py ....                                                                                                                                                                                                                 [100%]

============================ warnings summary =================================
c:\python37\lib\site-packages\_pytest\mark\structures.py:324
  c:\python37\lib\site-packages\_pytest\mark\structures.py:324: PytestUnknownMarkWarning: Unknown pytest.mark.run_these_cases - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pyt
est.org/en/latest/mark.html
    PytestUnknownMarkWarning,

-- Docs: https://docs.pytest.org/en/latest/warnings.html
================= 3 failed, 14 passed, 1 warnings in 0.31 seconds ========================

从执行结果中我们看到一共收集的测试用例为17条,14条通过,3条失败,使用了选项–tb=no关闭错误信息回溯,当我们只想看执行结果而不想看那么多报错信息的时候可以使用它。

–maxfail=num选项

-x是遇到失败便全局停止,如果我们想遇到失败几次再停止呢?–maxfail选项为我们提供了这个场景的支持,如下执行结果所示:


E:\Programs\Python\Python_Pytest\TestScripts>pytest --maxfail=2 --tb=no
================================ test session starts ====================================
platform win32 -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0
rootdir: E:\Programs\Python\Python_Pytest\TestScripts
plugins: allure-pytest-2.6.3
collected 17 items                                                                                                                                                                                                                     

test_asserts.py ...F..F

============================= warnings summary ====================================
c:\python37\lib\site-packages\_pytest\mark\structures.py:324
  c:\python37\lib\site-packages\_pytest\mark\structures.py:324: PytestUnknownMarkWarning: Unknown pytest.mark.run_these_cases - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pyt
est.org/en/latest/mark.html
    PytestUnknownMarkWarning,

-- Docs: https://docs.pytest.org/en/latest/warnings.html
=========================== 2 failed, 5 passed, 1 warnings in 0.22 seconds ===================

从执行结果中我们看到收集了17条用例,执行了7条,当错误数量达到2的时候便停止了执行。

–tb=

命令及参数    描述
pytest --showlocals    # show local variables in tracebacks
pytest -l    # show local variables (shortcut)
pytest --tb=auto    # (default) ‘long’ tracebacks for the first and last entry, but ‘short’ style for the other entries
pytest --tb=long    # exhaustive, informative traceback formatting
pytest --tb=short    # shorter traceback format
pytest --tb=line    # only one line per failure
pytest --tb=native    # Python standard library formatting
pytest --tb=no    # no traceback at all
pytest --full-trace    #causes very long traces to be printed on error (longer than --tb=long).
-v(–verbose)

-v, --verbose:increase verbosity.

-q(–quiet)

-q, --quiet:decrease verbosity.

–lf(–last-failed)

–lf, --last-failed:rerun only the tests that failed at the last run (or all if none failed)

E:\Programs\Python\Python_Pytest\TestScripts>pytest --lf --tb=no
========================== test session starts ========================================
platform win32 -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0
rootdir: E:\Programs\Python\Python_Pytest\TestScripts
plugins: allure-pytest-2.6.3
collected 9 items / 6 deselected / 3 selected                                                                                                                                                                                          
run-last-failure: rerun previous 3 failures (skipped 7 files)
test_asserts.py FF                                                                                                                                                                                                               [ 66%]
test_one.py F                                                                                                                                                                                                                    [100%]

======================== 3 failed, 6 deselected in 0.15 seconds ============================

–ff(–failed-first)

–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

E:\Programs\Python\Python_Pytest\TestScripts>pytest --ff --tb=no
================================= test session starts ==================================
platform win32 -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0
rootdir: E:\Programs\Python\Python_Pytest\TestScripts
plugins: allure-pytest-2.6.3
collected 17 items                                                                                                                                                                                                                     
run-last-failure: rerun previous 3 failures first
test_asserts.py FF                                                                                                                                                                                                               [ 11%]
test_one.py F                                                                                                                                                                                                                    [ 17%]
test_asserts.py .....                                                                                                                                                                                                            [ 47%]
test_fixture1.py ..                                                                                                                                                                                                              [ 58%]
test_fixture2.py ..                                                                                                                                                                                                              [ 70%]
test_one.py .                                                                                                                                                                                                                    [ 76%]
test_two.py ....                                                                                                                                                                                                                 [100%]
======================== warnings summary ==========================================
c:\python37\lib\site-packages\_pytest\mark\structures.py:324
  c:\python37\lib\site-packages\_pytest\mark\structures.py:324: PytestUnknownMarkWarning: Unknown pytest.mark.run_these_cases - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pyt
est.org/en/latest/mark.html
    PytestUnknownMarkWarning,

-- Docs: https://docs.pytest.org/en/latest/warnings.html
================= 3 failed, 14 passed, 1 warnings in 0.25 seconds ==========================

-s与–capture=method

-s等同于–capture=no,执行结果如下:

command="-s 文件名::类名::测试用例名 --html=%s test.html" % report_path

pytest.main(command)

(venv) D:\Python_Pytest\TestScripts>pytest -s
============================= test session starts ============================================
platform win32 -- Python 3.7.3, pytest-4.0.2, py-1.8.0, pluggy-0.12.0
rootdir: D:\Python_Pytest\TestScripts, inifile:
plugins: allure-adaptor-1.7.10
collected 18 items                                                                                                                                                                                                                        

test_asserts.py ...F...F
test_fixture1.py

setup_module================>
setup_function------>
test_numbers_3_4
.teardown_function--->
setup_function------>
test_strings_a_3
.teardown_function--->
teardown_module=============>

test_fixture2.py

setup_class=========>
setup_method----->>
setup----->
test_numbers_5_6
.teardown-->
teardown_method-->>
setup_method----->>
setup----->
test_strings_b_2
.teardown-->
teardown_method-->>
teardown_class=========>

test_one.py .F
test_two.py ....

========================= FAILURES =============================================
_____________________________ test_add4 ______________________________________________

   @pytest.mark.aaaa
   def test_add4():
>       assert add(17, 22) >= 50
E       assert 39 >= 50
E        +  where 39 = add(17, 22)

test_asserts.py:36: AssertionError
_____________________________________________________________________________________________________________ test_not_true ______________________________________________________________________________________________________________

   def test_not_true():
>       assert not is_prime(7)
E       assert not True
E        +  where True = is_prime(7)

test_asserts.py:70: AssertionError
_______________________________________ test_not_equal ________________________________________________

   def test_not_equal():
>       assert (1, 2, 3) == (3, 2, 1)
E       assert (1, 2, 3) == (3, 2, 1)
E         At index 0 diff: 1 != 3
E         Use -v to get the full diff

test_one.py:9: AssertionError
================================== 3 failed, 15 passed in 0.15 seconds =================================
–capture=method per-test capturing method: one of fd|sys|no.

-l(–showlocals)

-l, --showlocals show locals in tracebacks (disabled by default).

–duration=N

–durations=N show N slowest setup/test durations (N=0 for all).
绝大多数用于调优测试代码,该选项展示最慢的N个用例,等于0则表示全部倒序

(venv) D:\Python_Pytest\TestScripts>pytest --duration=5
============================= test session starts =============================
platform win32 -- Python 3.7.3, pytest-4.0.2, py-1.8.0, pluggy-0.12.0
rootdir: D:\Python_Pytest\TestScripts, inifile:
plugins: allure-adaptor-1.7.10
collected 18 items                                                             

test_asserts.py ...F...F                                                 [ 44%]
test_fixture1.py ..                                                      [ 55%]
test_fixture2.py ..                                                      [ 66%]
test_one.py .F                                                           [ 77%]
test_two.py ....                                                         [100%]

================================== FAILURES ===================================
__________________________________ test_add4 __________________________________

    @pytest.mark.aaaa
    def test_add4():
>       assert add(17, 22) >= 50
E       assert 39 >= 50
E        +  where 39 = add(17, 22)

test_asserts.py:36: AssertionError
________________________________ test_not_true ________________________________

    def test_not_true():
>       assert not is_prime(7)
E       assert not True
E        +  where True = is_prime(7)

test_asserts.py:70: AssertionError
_______________________________ test_not_equal ________________________________

    def test_not_equal():
>       assert (1, 2, 3) == (3, 2, 1)
E       assert (1, 2, 3) == (3, 2, 1)
E         At index 0 diff: 1 != 3
E         Use -v to get the full diff

test_one.py:9: AssertionError
========================== slowest 5 test durations ===========================
0.01s call     test_asserts.py::test_add4

(0.00 durations hidden.  Use -vv to show these durations.)
===================== 3 failed, 15 passed in 0.27 seconds =====================
在执行结果中我们看到提示(0.00 durations hidden. Use -vv to show these durations.),如果加上-vv,执行结果如下:

(venv) D:\Python_Pytest\TestScripts>pytest --duration=5 -vv
============================= test session starts =============================
platform win32 -- Python 3.7.3, pytest-4.0.2, py-1.8.0, pluggy-0.12.0 -- c:\python37\python.exe
cachedir: .pytest_cache
rootdir: D:\Python_Pytest\TestScripts, inifile:
plugins: allure-adaptor-1.7.10
collected 18 items                                                             

test_asserts.py::test_add PASSED                                         [  5%]
test_asserts.py::test_add2 PASSED                                        [ 11%]
test_asserts.py::test_add3 PASSED                                        [ 16%]
test_asserts.py::test_add4 FAILED                                        [ 22%]
test_asserts.py::test_in PASSED                                          [ 27%]
test_asserts.py::test_not_in PASSED                                      [ 33%]
test_asserts.py::test_true PASSED                                        [ 38%]
test_asserts.py::test_not_true FAILED                                    [ 44%]
test_fixture1.py::test_numbers_3_4 PASSED                                [ 50%]
test_fixture1.py::test_strings_a_3 PASSED                                [ 55%]
test_fixture2.py::TestUM::test_numbers_5_6 PASSED                        [ 61%]
test_fixture2.py::TestUM::test_strings_b_2 PASSED                        [ 66%]
test_one.py::test_equal PASSED                                           [ 72%]
test_one.py::test_not_equal FAILED                                       [ 77%]
test_two.py::test_default PASSED                                         [ 83%]
test_two.py::test_member_access PASSED                                   [ 88%]
test_two.py::test_asdict PASSED                                          [ 94%]
test_two.py::test_replace PASSED                                         [100%]

================================== FAILURES ===================================
__________________________________ test_add4 __________________________________

    @pytest.mark.aaaa
    def test_add4():
>       assert add(17, 22) >= 50
E       assert 39 >= 50
E        +  where 39 = add(17, 22)

test_asserts.py:36: AssertionError
________________________________ test_not_true ________________________________

    def test_not_true():
>       assert not is_prime(7)
E       assert not True
E        +  where True = is_prime(7)

test_asserts.py:70: AssertionError
_______________________________ test_not_equal ________________________________

    def test_not_equal():
>       assert (1, 2, 3) == (3, 2, 1)
E       assert (1, 2, 3) == (3, 2, 1)
E         At index 0 diff: 1 != 3
E         Full diff:
E         - (1, 2, 3)
E         ?  ^     ^
E         + (3, 2, 1)
E         ?  ^     ^

test_one.py:9: AssertionError
========================== slowest 5 test durations ===========================
0.00s setup    test_one.py::test_not_equal
0.00s setup    test_fixture1.py::test_strings_a_3
0.00s setup    test_asserts.py::test_add3
0.00s call     test_fixture2.py::TestUM::test_strings_b_2
0.00s call     test_asserts.py::test_in
===================== 3 failed, 15 passed in 0.16 seconds =====================

-r 生成一个简短的概述报告,同时配合-r还可以使用

Option    Description
f    failed
E    error
s    skipped
x    xfailed
X    xpassed
p    passed
P    passed with output
a    all except pP
A    all
例如只想看失败的和跳过的测试,可以这样执行


(venv) E:\Python_Pytest\TestScripts>pytest -rfs
============================= test session starts =============================
platform win32 -- Python 3.7.3, pytest-4.0.2, py-1.8.0, pluggy-0.12.0
rootdir: E:\Python_Pytest\TestScripts, inifile:
plugins: allure-adaptor-1.7.10
collected 18 items                                                             

test_asserts.py ...F...F                                                 [ 44%]
test_fixture1.py ..                                                      [ 55%]
test_fixture2.py ..                                                      [ 66%]
test_one.py .F                                                           [ 77%]
test_two.py ....                                                         [100%]

================================== FAILURES ===================================
__________________________________ test_add4 __________________________________

    @pytest.mark.aaaa
    def test_add4():
>       assert add(17, 22) >= 50
E       assert 39 >= 50
E        +  where 39 = add(17, 22)

test_asserts.py:36: AssertionError
________________________________ test_not_true ________________________________

    def test_not_true():
>       assert not is_prime(7)
E       assert not True
E        +  where True = is_prime(7)

test_asserts.py:70: AssertionError
_______________________________ test_not_equal ________________________________

    def test_not_equal():
>       assert (1, 2, 3) == (3, 2, 1)
E       assert (1, 2, 3) == (3, 2, 1)
E         At index 0 diff: 1 != 3
E         Use -v to get the full diff

test_one.py:9: AssertionError
=========================== short test summary info ===========================
FAIL test_asserts.py::test_add4
FAIL test_asserts.py::test_not_true
FAIL test_one.py::test_not_equal
===================== 3 failed, 15 passed in 0.10 seconds =====================


pytest --help 获取更多参数

在命令行输入pytest --help 然后执行结果如下,在打印出来的结果中我们能够看到pytest命令的使用方式usage: pytest [options] [file_or_dir] [file_or_dir] […]以及一系列的执行方式(options)及其描述。

C:\Users\Administrator>pytest --help
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. 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.
  --continue-on-collection-errors
                        Force test execution even if collection errors occur.
  --rootdir=ROOTDIR     Define root directory for tests. Can be relative path:
                        'root_dir', './root_dir', 'root_dir/another_dir/';
                        absolute path: '/home/user/root_dir'; path with
                        variables: '$HOME/root_dir'.
  --fixtures, --funcargs
                        show available fixtures, sorted by plugin appearance
                        (fixtures with leading '_' are only shown with '-v')
  --fixtures-per-test   show fixtures per test
  --import-mode={prepend,append}
                        prepend/append to sys.path when importing test
                        modules, default is to prepend.
  --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.
  -s                    shortcut for --capture=no.
  --runxfail            run tests even if they are marked xfail
  --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          show cache contents, don't perform collection or tests
  --cache-clear         remove all cache contents at start of test run.
  --lfnf={all,none}, --last-failed-no-failures={all,none}
                        change the behavior when no test failed in the last
                        run or no information about the last failures was
                        found in the cache
  --sw, --stepwise      exit on test fail and continue from last failing test
                        next time
  --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, minor, normal, trivial.
  --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.

reporting:
  -v, --verbose         increase verbosity.
  -q, --quiet           decrease verbosity.
  --verbosity=VERBOSE   set 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
  -l, --showlocals      show locals in tracebacks (disabled by default).
  --tb=style            traceback print mode (auto/long/short/line/native/no).
  --show-capture={no,stdout,stderr,log,all}
                        Controls how captured stdout/stderr/log is shown on
                        failed tests. Default is 'all'.
  --full-trace          don't cut any tracebacks (default is to cut).
  --color=color         color terminal output (yes/no/auto).
  --durations=N         show N slowest setup/test durations (N=0 for all).
  --pastebin=mode       send failed|all info to bpaste.net pastebin service.
  --junit-xml=path      create junit-xml style report file at given path.
  --junit-prefix=str    prepend prefix to classnames in junit-xml output
  --result-log=path     DEPRECATED path for machine-readable result log.

collection:
  --collect-only        only collect tests, don't execute them.
  --pyargs              try to interpret all arguments as python packages.
  --ignore=path         ignore path during collection (multi-allowed).
  --deselect=nodeid_prefix
                        deselect item during collection (multi-allowed).
  --confcutdir=dir      only load conftest.py's relative to specified dir.
  --noconftest          Don't load any conftest.py files.
  --keep-duplicates     Keep duplicate tests.
  --collect-in-virtualenv
                        Don't ignore tests in a local virtualenv directory
  --doctest-modules     run doctests in all .py modules
  --doctest-report={none,cdiff,ndiff,udiff,only_first_failure}
                        choose another output format for diffs on doctest
                        failure
  --doctest-glob=pat    doctests file matching pattern, default: test*.txt
  --doctest-ignore-import-errors
                        ignore doctest ImportErrors
  --doctest-continue-on-failure
                        for a given doctest, continue to run after the first
                        failure

test session debugging and configuration:
  --basetemp=dir        base temporary directory for this test run.(warning:
                        this directory is removed if it exists)
  --version             display pytest lib version and import information.
  -h, --help            show help message and configuration info
  -p name               early-load given plugin (multi-allowed). To avoid
                        loading of plugins, use the `no:` prefix, e.g.
                        `no:doctest`.
  --trace-config        trace considerations of conftest.py files.
  --debug               store internal tracing debug information in
                        'pytestdebug.log'.
  -o OVERRIDE_INI, --override-ini=OVERRIDE_INI
                        override ini option with "option=value" style, e.g.
                        `-o xfail_strict=True -o cache_dir=cache`.
  --assert=MODE         Control assertion debugging tools. 'plain' performs no
                        assertion debugging. 'rewrite' (the default) rewrites
                        assert statements in test modules on import to provide
                        assert expression information.
  --setup-only          only setup fixtures, do not execute tests.
  --setup-show          show setup of fixtures while executing tests.
  --setup-plan          show what fixtures and tests would be executed but
                        don't execute anything.

pytest-warnings:
  -W PYTHONWARNINGS, --pythonwarnings=PYTHONWARNINGS
                        set which warnings to report, see -W option of python
                        itself.

logging:
  --no-print-logs       disable printing caught logs on failed tests.
  --log-level=LOG_LEVEL
                        logging level used by the logging module
  --log-format=LOG_FORMAT
                        log format as used by the logging module.
  --log-date-format=LOG_DATE_FORMAT
                        log date format as used by the logging module.
  --log-cli-level=LOG_CLI_LEVEL
                        cli logging level.
  --log-cli-format=LOG_CLI_FORMAT
                        log format as used by the logging module.
  --log-cli-date-format=LOG_CLI_DATE_FORMAT
                        log date format as used by the logging module.
  --log-file=LOG_FILE   path to a file when logging will be written to.
  --log-file-level=LOG_FILE_LEVEL
                        log file logging level.
  --log-file-format=LOG_FILE_FORMAT
                        log format as used by the logging module.
  --log-file-date-format=LOG_FILE_DATE_FORMAT
                        log date format as used by the logging module.

reporting:
  --alluredir=DIR       Generate Allure report in the specified directory (may
                        not exist)


--------------------- 

原文:https://blog.csdn.net/dawei_yang000000/article/details/93380552 
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一、linux 1,linux常用命令 2,某个时间段日志的查询 3,linux文件的上传和下载 二、功能测试 1,工作中所遇到的错误 2,测试流程: 3,测试计划元素: 4,测试报告元素: 5,测试点: 6,测试方法: 7,bug相关问题 8,adb常用命令 9,软件测试原则 10,测试用例编写的要素 11,测试用例的设计原则 12,软件产品质量特性 13,android四大组件 14,web测试和app测试的区别 15,app的anr的根本原因 16,app的crash的原因 17,h5页面图片未加载出来问题排查 18,区分原生和h5页面 19,为什么不能用jenkins打包 三、性能测试 1,了解jmeter 2,性能指标 3,如何做性能测试 四、接口测试 1,如何设计接口测试用例 2,为什么要做接口测试 3,接口测试的关注点 4,request处理cookie的三种方式 五、自动化测试 1,自动化核心框架 2,自动化测试的好处 3,自动化的前提 4,自动化测试的场景 5,元素定位的8种方式 6,如果一个元素无法定位,一般会考虑哪些原因 7,driver.close()和driver.quit()的区别 8,自动化脚本断言 9,判断页面元素是否存在 10,js在web自动化中的作用展示 11,自动化代码优化 12,selenium对比RF 13,自动化测试脚本三种等待 14,PO模式 六、HTTP协议 1,HTTP协议特点: 2,HTTP传输原理 3,get和post的区别 4,HTTP响应代码 5,osi七层模型 6,三次握手过程 7,session和cookie的区别 8,tcp和udp的区别 9,sockect通信原理 10,post的三种请求方式 七、数据库 1,sql分类 2,数据库事务特性:ACID 3,mysql索引的类型 4,池化思想 5,redis 6,如何提高数据库运行效率 八、java 1,面向对象的三个特征 2,重写和重载 3,比较sping,sping mvc 4,进程和线程的区别 5,java三层架构 6,处理异常 九、python 1,字符串反转的7种方法 2,new 和 _init_ 3,不使用中间变量交换两个变量的值 4,python四大内置高阶函数 5,python带颜色输出 6,python *args,**kargs用法 7,python常用模块 8,python多线程 9,python发送邮件 10,python操作图像 11,python的replace()方法的使用

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值