Pytest----Pytest自动化测试脚本的执行方法

【原文链接】

一、指定文件名执行模块中的所有用例

目录结构如下:

demo01-----
  |--------test_demo01.py
  |--------test_demo02.py

test_demo01.py内容

def test_func1():
    print("\nin test_demo01.test_func1......")
    assert 1 == 1

def test_func2():
    print("\nin test_demo01.test_func2......")
    assert 1 == 1

test_demo02.py内容

def test_func1():
    print("\nin test_demo02.test_func1......")
    assert 1 == 1

def test_func2():
    print("\nin test_demo02.test_func2......")
    assert 1 == 1

在demo01的目录下执行命令如下:只执行test_demo01.py文件内的测试脚本

$ pytest -s test_demo01.py
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests\demo01
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 2 items                                                                                                                                                       

test_demo01.py
in test_demo01.test_func1......
.
in test_demo01.test_func2......
.

========================================================================== 2 passed in 0.02s ===========================================================================

$

二、指定文件夹执行其中的所有用例

目录结构如下:

tests
  |--------demo01
             |--------test_demo01.py
             |--------test_demo02.py
  |--------test_case01.py

test_demo01.py内容

def test_func1():
    print("\nin test_demo01.test_func1......")
    assert 1 == 1

def test_func2():
    print("\nin test_demo01.test_func2......")
    assert 1 == 1

test_demo02.py内容

def test_func1():
    print("\nin test_demo02.test_func1......")
    assert 1 == 1

def test_func2():
    print("\nin test_demo02.test_func2......")
    assert 1 == 1

test_case01.py的内容

def test_func1():
    print("\nin test_case01.test_func1......")
    assert 1 == 1

def test_func2():
    print("\nin test_case01.test_func2......")
    assert 1 == 1

在tests目录下得cmd窗口中执行如下命令,即只执行demo01目录下的测试用例

$ pytest -s demo01
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 4 items                                                                                                                                                       

demo01\test_demo01.py
in test_demo01.test_func1......
.
in test_demo01.test_func2......
.
demo01\test_demo02.py
in test_demo02.test_func1......
.
in test_demo02.test_func2......
.

========================================================================== 4 passed in 0.03s ===========================================================================

$

三、指定节点id执行指定的用例

test_demo01.py内容如下:

def test_func1():
    print("\nin test_demo01.test_func1......")
    assert 1 == 1

def test_func2():
    print("\nin test_demo01.test_func2......")
    assert 1 == 1

class TestDemo(object):
    def test_func1(self):
        print("\nin test_demo01.TestDemo.test_func1......")
        assert 1 == 1

    def test_func2(self):
        print("\nin test_demo01.TestDemo.test_func2......")
        assert 1 == 1

如下指定测试函数执行具体的用例

$ pytest -s test_demo01.py::test_func1
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests\demo01
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 1 item                                                                                                                                                        

test_demo01.py
in test_demo01.test_func1......
.

========================================================================== 1 passed in 0.02s ===========================================================================

$

如下指定测试类中的具体方法执行具体的用例

$ pytest -s test_demo01.py::TestDemo::test_func1
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests\demo01
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 1 item                                                                                                                                                        

test_demo01.py
in test_demo01.TestDemo.test_func1......
.

========================================================================== 1 passed in 0.01s ===========================================================================

$

如下指定测试类执行整个测试类中的所有测试用例

$ pytest -s test_demo01.py::TestDemo
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests\demo01
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 2 items                                                                                                                                                       

test_demo01.py
in test_demo01.TestDemo.test_func1......
.
in test_demo01.TestDemo.test_func2......
.

========================================================================== 2 passed in 0.02s ===========================================================================

$

四、通过对文件名类名函数名模糊匹配执行指定用例

使用 pytest -k 参数

目录结构如下:

demo01-----
  |--------test_demo01.py
  |--------test_demo02.py

test_demo01.py内容如下:

def test_func1():
    print("\nin test_demo01.test_func1......")
    assert 1 == 1

def test_func2():
    print("\nin test_demo01.test_func2......")
    assert 1 == 1

class TestDemo(object):
    def test_func1(self):
        print("\nin test_demo01.TestDemo.test_func1......")
        assert 1 == 1

    def test_func2(self):
        print("\nin test_demo01.TestDemo.test_func2......")
        assert 1 == 1

test_demo02.py内容如下:

def test_func1():
    print("\nin test_demo02.test_func1......")
    assert 1 == 1

def test_func2():
    print("\nin test_demo02.test_func2......")
    assert 1 == 1

这里进行正则匹配的包括文件名、类名、函数名,如这里只想匹配1的用例,结果如下,可以发现这里首先test_demo01.py文件已经匹配上了,因此这个文件里的所有用例都会跑,而test_demo02.py文件中的test_func1函数名中有1,因此也匹配上了

$ pytest -s -k "1"
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests\demo01
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 6 items / 1 deselected / 5 selected                                                                                                                           

test_demo01.py
in test_demo01.test_func1......
.
in test_demo01.test_func2......
.
in test_demo01.TestDemo.test_func1......
.
in test_demo01.TestDemo.test_func2......
.
test_demo02.py
in test_demo02.test_func1......
.

=================================================================== 5 passed, 1 deselected in 0.02s ====================================================================

$

这里可以像linux命令里的管道一样进行多次过滤,如下,可以理解为首先在文件名类名函数名中匹配1,然后再匹配到的用例中再继续匹配没有func2的,结果如下

$ pytest -s -k "1 and not func2"
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests\demo01
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 6 items / 3 deselected / 3 selected                                                                                                                           

test_demo01.py
in test_demo01.test_func1......
.
in test_demo01.TestDemo.test_func1......
.
test_demo02.py
in test_demo02.test_func1......
.

=================================================================== 3 passed, 3 deselected in 0.02s ====================================================================

$

这里和linux的管道非常类似,可以进行多次过滤,如下即表示首先在文件名类名函数名中匹配1,在匹配到的结果中再继续匹配文件名类名函数名中没有func2字段的,再在匹配结果继续匹配文件名类名函数名中有testdemo的,这里需要特别注意的是,这里的匹配是不区分大小写的,也就是这里最后匹配testdemo的时候实际上是匹配上了TestDemo类名

$ pytest -s -k "1 and not func2 and testdemo"
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests\demo01
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 6 items / 5 deselected / 1 selected                                                                                                                           

test_demo01.py
in test_demo01.TestDemo.test_func1......
.

=================================================================== 1 passed, 5 deselected in 0.02s ====================================================================

$

五、通过指定标签来执行指定的用例

这里需要结合pytest.mark 打标签一起使用,如下test_demo01.脚本,脚本中打了smoke,func,两个标签,然后执行用例的时候可以通过 pytest -m参数指定标签执行

import pytest

@pytest.mark.smoke
def test_func1():
    print("\nin test_demo01.test_func1......")
    assert 1 == 1

@pytest.mark.smoke
@pytest.mark.func
def test_func2():
    print("\nin test_demo01.test_func2......")
    assert 1 == 1

class TestDemo(object):
    @pytest.mark.smoke
    def test_func1(self):
        print("\nin test_demo01.TestDemo.test_func1......")
        assert 1 == 1

    @pytest.mark.func
    def test_func2(self):
        print("\nin test_demo01.TestDemo.test_func2......")
        assert 1 == 1

如下,执行 打smoke标签的用例

$ pytest -s -m smoke
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests\demo01
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 6 items / 3 deselected / 3 selected                                                                                                                           

test_demo01.py
in test_demo01.test_func1......
.
in test_demo01.test_func2......
.
in test_demo01.TestDemo.test_func1......
.

=========================================================================== warnings summary ===========================================================================
test_demo01.py:3
  D:\src\blog\tests\demo01\test_demo01.py:3: PytestUnknownMarkWarning: Unknown pytest.mark.smoke - is this a typo?  You can register custom marks to avoid this warning -
 for details, see https://docs.pytest.org/en/stable/mark.html
    @pytest.mark.smoke

test_demo01.py:8
  D:\src\blog\tests\demo01\test_demo01.py:8: PytestUnknownMarkWarning: Unknown pytest.mark.smoke - is this a typo?  You can register custom marks to avoid this warning -
 for details, see https://docs.pytest.org/en/stable/mark.html
    @pytest.mark.smoke

test_demo01.py:9
  D:\src\blog\tests\demo01\test_demo01.py:9: PytestUnknownMarkWarning: Unknown pytest.mark.func - is this a typo?  You can register custom marks to avoid this warning -
for details, see https://docs.pytest.org/en/stable/mark.html
    @pytest.mark.func

test_demo01.py:15
  D:\src\blog\tests\demo01\test_demo01.py:15: PytestUnknownMarkWarning: Unknown pytest.mark.smoke - is this a typo?  You can register custom marks to avoid this warning
- for details, see https://docs.pytest.org/en/stable/mark.html
    @pytest.mark.smoke

test_demo01.py:20
  D:\src\blog\tests\demo01\test_demo01.py:20: PytestUnknownMarkWarning: Unknown pytest.mark.func - is this a typo?  You can register custom marks to avoid this warning -
 for details, see https://docs.pytest.org/en/stable/mark.html
    @pytest.mark.func

-- Docs: https://docs.pytest.org/en/stable/warnings.html
============================================================= 3 passed, 3 deselected, 5 warnings in 0.03s ==============================================================

$ 

标签之间可以使用逻辑关系and,or,not,如下执行既打了smoke标签也打了func标签的用例

$ pytest -s -m "smoke and func"
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests\demo01
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 6 items / 5 deselected / 1 selected                                                                                                                           

test_demo01.py
in test_demo01.test_func2......
.

=========================================================================== warnings summary ===========================================================================
test_demo01.py:3
  D:\src\blog\tests\demo01\test_demo01.py:3: PytestUnknownMarkWarning: Unknown pytest.mark.smoke - is this a typo?  You can register custom marks to avoid this warning -
 for details, see https://docs.pytest.org/en/stable/mark.html
    @pytest.mark.smoke

test_demo01.py:8
  D:\src\blog\tests\demo01\test_demo01.py:8: PytestUnknownMarkWarning: Unknown pytest.mark.smoke - is this a typo?  You can register custom marks to avoid this warning -
 for details, see https://docs.pytest.org/en/stable/mark.html
    @pytest.mark.smoke

test_demo01.py:9
  D:\src\blog\tests\demo01\test_demo01.py:9: PytestUnknownMarkWarning: Unknown pytest.mark.func - is this a typo?  You can register custom marks to avoid this warning -
for details, see https://docs.pytest.org/en/stable/mark.html
    @pytest.mark.func

test_demo01.py:15
  D:\src\blog\tests\demo01\test_demo01.py:15: PytestUnknownMarkWarning: Unknown pytest.mark.smoke - is this a typo?  You can register custom marks to avoid this warning
- for details, see https://docs.pytest.org/en/stable/mark.html
    @pytest.mark.smoke

test_demo01.py:20
  D:\src\blog\tests\demo01\test_demo01.py:20: PytestUnknownMarkWarning: Unknown pytest.mark.func - is this a typo?  You can register custom marks to avoid this warning -
 for details, see https://docs.pytest.org/en/stable/mark.html
    @pytest.mark.func

-- Docs: https://docs.pytest.org/en/stable/warnings.html
============================================================= 1 passed, 5 deselected, 5 warnings in 0.02s ==============================================================

$

六、执行用例同时显示用例执行时间

test_demo.py内容如下:

import time
def test_demo01():
    time.sleep(0.1)
    assert 1==1

def test_demo02():
    time.sleep(0.2)
    assert 1==1

def test_demo03():
    time.sleep(0.3)
    assert 1==1

def test_demo04():
    time.sleep(0.4)
    assert 1==1

def test_demo05():
    time.sleep(0.5)
    assert 1==1

def test_demo06():
    time.sleep(0.6)
    assert 1==1

def test_demo07():
    time.sleep(0.7)
    assert 1==1

def test_demo08():
    time.sleep(0.8)
    assert 1==1

def test_demo09():
    time.sleep(0.9)
    assert 1==1

def test_demo10():
    time.sleep(1.0)
    assert 1==1

def test_demo11():
    assert 1==1

pytest --durations=n 显示执行时间最慢的n个用例及执行时间,如下显示执行最慢的5个用例

$ pytest --durations=5
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests\demo02
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 11 items                                                                                                                                                      

test_demo01.py ...........                                                                                                                                        [100%]

========================================================================= slowest 5 durations ==========================================================================
1.00s call     test_demo01.py::test_demo10
0.92s call     test_demo01.py::test_demo09
0.81s call     test_demo01.py::test_demo08
0.71s call     test_demo01.py::test_demo07
0.60s call     test_demo01.py::test_demo06
========================================================================== 11 passed in 5.61s ==========================================================================

$

pytest --durations=0 显示所有用例的执行时间

$ pytest --durations=0
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests\demo02
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 11 items                                                                                                                                                      

test_demo01.py ...........                                                                                                                                        [100%]

========================================================================== slowest durations ===========================================================================
1.01s call     test_demo01.py::test_demo10
0.92s call     test_demo01.py::test_demo09
0.80s call     test_demo01.py::test_demo08
0.71s call     test_demo01.py::test_demo07
0.60s call     test_demo01.py::test_demo06
0.51s call     test_demo01.py::test_demo05
0.40s call     test_demo01.py::test_demo04
0.31s call     test_demo01.py::test_demo03
0.20s call     test_demo01.py::test_demo02
0.12s call     test_demo01.py::test_demo01

(23 durations < 0.005s hidden.  Use -vv to show these durations.)
========================================================================== 11 passed in 5.63s ==========================================================================

$

细心的发现上面并没有显示所有的,test_demo11用例没有显示,这是因为pytest默认显示的耗时大于0.005秒的用例,小于0.005秒的用例默认是不显示的,如果要显示耗时小于0.005秒的用例,需要加上 -vv的参数,如下:

$ pytest --durations=0 -vv
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- D:\Python39\python.exe
cachedir: .pytest_cache
rootdir: D:\src\blog\tests\demo02
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 11 items                                                                                                                                                      

test_demo01.py::test_demo01 PASSED                                                                                                                                [  9%]
test_demo01.py::test_demo02 PASSED                                                                                                                                [ 18%]
test_demo01.py::test_demo03 PASSED                                                                                                                                [ 27%]
test_demo01.py::test_demo04 PASSED                                                                                                                                [ 36%]
test_demo01.py::test_demo05 PASSED                                                                                                                                [ 45%]
test_demo01.py::test_demo06 PASSED                                                                                                                                [ 54%]
test_demo01.py::test_demo07 PASSED                                                                                                                                [ 63%]
test_demo01.py::test_demo08 PASSED                                                                                                                                [ 72%]
test_demo01.py::test_demo09 PASSED                                                                                                                                [ 81%]
test_demo01.py::test_demo10 PASSED                                                                                                                                [ 90%]
test_demo01.py::test_demo11 PASSED                                                                                                                                [100%]

========================================================================== slowest durations ===========================================================================
1.01s call     test_demo01.py::test_demo10
0.91s call     test_demo01.py::test_demo09
0.81s call     test_demo01.py::test_demo08
0.71s call     test_demo01.py::test_demo07
0.60s call     test_demo01.py::test_demo06
0.51s call     test_demo01.py::test_demo05
0.40s call     test_demo01.py::test_demo04
0.30s call     test_demo01.py::test_demo03
0.21s call     test_demo01.py::test_demo02
0.11s call     test_demo01.py::test_demo01
0.00s setup    test_demo01.py::test_demo01
0.00s setup    test_demo01.py::test_demo06
0.00s setup    test_demo01.py::test_demo08
0.00s setup    test_demo01.py::test_demo10
0.00s teardown test_demo01.py::test_demo01
0.00s teardown test_demo01.py::test_demo11
0.00s setup    test_demo01.py::test_demo04
0.00s teardown test_demo01.py::test_demo02
0.00s teardown test_demo01.py::test_demo07
0.00s teardown test_demo01.py::test_demo10
0.00s teardown test_demo01.py::test_demo03
0.00s teardown test_demo01.py::test_demo04
0.00s teardown test_demo01.py::test_demo08
0.00s teardown test_demo01.py::test_demo06
0.00s setup    test_demo01.py::test_demo09
0.00s setup    test_demo01.py::test_demo02
0.00s teardown test_demo01.py::test_demo09
0.00s setup    test_demo01.py::test_demo05
0.00s setup    test_demo01.py::test_demo03
0.00s setup    test_demo01.py::test_demo07
0.00s setup    test_demo01.py::test_demo11
0.00s teardown test_demo01.py::test_demo05
0.00s call     test_demo01.py::test_demo11
========================================================================== 11 passed in 5.63s ==========================================================================
$

通过 --durations-min=t 参数可以设置显示耗时时间大于t的用例,如下为显示耗时大于0.5秒的用例

$ pytest --durations=0 --durations-min=0.5
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests\demo02
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 11 items                                                                                                                                                      

test_demo01.py ...........                                                                                                                                        [100%]

========================================================================== slowest durations ===========================================================================
1.01s call     test_demo01.py::test_demo10
0.91s call     test_demo01.py::test_demo09
0.81s call     test_demo01.py::test_demo08
0.71s call     test_demo01.py::test_demo07
0.61s call     test_demo01.py::test_demo06
0.51s call     test_demo01.py::test_demo05

(27 durations < 0.5s hidden.  Use -vv to show these durations.)
========================================================================== 11 passed in 5.63s ==========================================================================
$

七、执行已安装包中的测试用例

在某一些场景下,比如需要执行发布包中的测试用例或者自己开发了一个包发布安装之后想再执行已安装包的测试用例(当然发布包的时候需要将测试文件夹一起打包,否则找不到用例),此时在任意位置使用 --pyargs参数指定即可,比如如下指定执行numpy包的tests用例执行

$ pytest --pyargs numpy.tests
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests\demo02
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 49 items                                                                                                                                                      

test_ctypeslib.py .......................                                                                                                                         [ 46%]
test_matlib.py ........                                                                                                                                           [ 63%]
test_numpy_version.py ..                                                                                                                                          [ 67%]
test_public_api.py ..........                                                                                                                                     [ 87%]
test_reloading.py ...                                                                                                                                             [ 93%]
test_scripts.py X.                                                                                                                                                [ 97%]
test_warnings.py .                                                                                                                                                [100%]

=========================================================================== warnings summary ===========================================================================
..\..\..\..\Python39\lib\site-packages\numpy\tests\test_matlib.py:2
  D:\Python39\lib\site-packages\numpy\tests\test_matlib.py:2: PendingDeprecationWarning: Importing from numpy.matlib is deprecated since 1.19.0. The matrix subclass is n
ot the recommended way to represent matrices or deal with linear algebra (see https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html). Please adjust your code
 to use regular ndarray.
    import numpy.matlib

test_matlib.py::test_ones
  D:\Python39\lib\site-packages\numpy\tests\test_matlib.py:12: PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices or deal wi
th linear algebra (see https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html). Please adjust your code to use regular ndarray.
    np.matrix([[ 1.,  1.,  1.],

test_matlib.py::test_ones
  D:\Python39\lib\site-packages\numpy\tests\test_matlib.py:15: PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices or deal wi
th linear algebra (see https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html). Please adjust your code to use regular ndarray.
    assert_array_equal(numpy.matlib.ones(2), np.matrix([[ 1.,  1.]]))

test_matlib.py::test_zeros
  D:\Python39\lib\site-packages\numpy\tests\test_matlib.py:19: PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices or deal wi
th linear algebra (see https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html). Please adjust your code to use regular ndarray.
    np.matrix([[ 0.,  0.,  0.],

test_matlib.py::test_zeros
  D:\Python39\lib\site-packages\numpy\tests\test_matlib.py:22: PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices or deal wi
th linear algebra (see https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html). Please adjust your code to use regular ndarray.
    assert_array_equal(numpy.matlib.zeros(2), np.matrix([[ 0.,  0.]]))

test_matlib.py::test_identity
  D:\Python39\lib\site-packages\numpy\tests\test_matlib.py:26: PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices or deal wi
th linear algebra (see https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html). Please adjust your code to use regular ndarray.
    assert_array_equal(x, np.matrix([[1, 0], [0, 1]]))

test_matlib.py::test_eye
test_matlib.py::test_eye
test_matlib.py::test_rand
test_matlib.py::test_randn
  D:\Python39\lib\site-packages\numpy\matrixlib\defmatrix.py:69: PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices or deal
with linear algebra (see https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html). Please adjust your code to use regular ndarray.
    return matrix(data, dtype=dtype, copy=False)

test_matlib.py::test_eye
  D:\Python39\lib\site-packages\numpy\tests\test_matlib.py:30: PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices or deal wi
th linear algebra (see https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html). Please adjust your code to use regular ndarray.
    assert_array_equal(xc, np.matrix([[ 0,  1,  0],

test_matlib.py::test_eye
  D:\Python39\lib\site-packages\numpy\tests\test_matlib.py:37: PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices or deal wi
th linear algebra (see https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html). Please adjust your code to use regular ndarray.
    assert_array_equal(xf, np.matrix([[ 1,  0,  0,  0],

-- Docs: https://docs.pytest.org/en/stable/warnings.html
============================================================= 48 passed, 1 xpassed, 12 warnings in 12.58s ==============================================================

$

八、在执行脚本之前加载自定义插件

通过 -p 参数在执行脚本之前加载自定义插件,命令如下,这里因为涉及自定义插件的看法,暂时不详细演示,待后续开发自定义插件后再继续讨论使用

pytest -p mypluginmodule

通过 -p 命令和在插件名称钱加no手动禁止默认插件加载,如下表示禁止doctest插件加载

pytest -p no:doctest

九、在IDE中通过右键执行当前文件的方式执行用例

test_case01.py文件内容如下,比如在pycharm中,按照如下编写,直接右键执行即可执行测试用例

import pytest


def test_func1():
    print("\nin test_case01.test_func1......")
    assert 1 == 1


def test_func2():
    print("\nin test_case01.test_func2......")
    assert 1 == 1


if __name__ == "__main__":
    pytest.main()

若指定pytest的参数,如下:

import pytest


def test_func1():
    print("\nin test_case01.test_func1......")
    assert 1 == 1


def test_func2():
    print("\nin test_case01.test_func2......")
    assert 1 == 1


if __name__ == "__main__":
    pytest.main(['-s','test_case01.py'])

可以在测试文件中调用自定义插件,具体使用格式如下,这里插件待后续进行插件开发时再行讨论

import sys
import pytest


def test_func1():
    print("\nin test_case01.test_func1......")
    assert 1 == 1


def test_func2():
    print("\nin test_case01.test_func2......")
    assert 1 == 1



class MyPlugin:
    def pytest_sessionfinish(self):
        print("*** test run reporting finishing")


if __name__ == "__main__":
    sys.exit(pytest.main(["-s"], plugins=[MyPlugin()]))

至此,基本完成pytest的执行方式的总结了,其他更高级话题待后续继续补充

你可能感兴趣的:(Pytest自动化测试框架,Pytest,python,pytest官方文档,pytest中文文档,pytest官方文档深入解读)