Pytest----如何执行pytest自动化测试脚本

简介: 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的执行方式的总结了,其他更高级话题待后续继续补充

目录
相关文章
|
29天前
|
Java Shell
「sh脚步模版自取」测试线排查的三个脚本:启动、停止、重启、日志保存
「sh脚步模版自取」测试线排查的三个脚本:启动、停止、重启、日志保存
36 1
|
6天前
|
运维 监控 应用服务中间件
自动化运维:如何利用Python脚本提升工作效率
【10月更文挑战第30天】在快节奏的IT行业中,自动化运维已成为提升工作效率和减少人为错误的关键技术。本文将介绍如何使用Python编写简单的自动化脚本,以实现日常运维任务的自动化。通过实际案例,我们将展示如何用Python脚本简化服务器管理、批量配置更新以及监控系统性能等任务。文章不仅提供代码示例,还将深入探讨自动化运维背后的理念,帮助读者理解并应用这一技术来优化他们的工作流程。
|
7天前
|
运维 监控 Linux
自动化运维:如何利用Python脚本优化日常任务##
【10月更文挑战第29天】在现代IT运维中,自动化已成为提升效率、减少人为错误的关键技术。本文将介绍如何通过Python脚本来简化和自动化日常的运维任务,从而让运维人员能够专注于更高层次的工作。从备份管理到系统监控,再到日志分析,我们将一步步展示如何编写实用的Python脚本来处理这些任务。 ##
|
28天前
|
测试技术
自动化测试项目学习笔记(五):Pytest结合allure生成测试报告以及重构项目
本文介绍了如何使用Pytest和Allure生成自动化测试报告。通过安装allure-pytest和配置环境,可以生成包含用例描述、步骤、等级等详细信息的美观报告。文章还提供了代码示例和运行指南,以及重构项目时的注意事项。
130 1
自动化测试项目学习笔记(五):Pytest结合allure生成测试报告以及重构项目
|
11天前
|
运维 Prometheus 监控
自动化运维之路:从脚本到DevOps
【10月更文挑战第25天】在数字化时代的浪潮中,运维不再是简单的服务器管理,而是成为了企业竞争力的核心。本文将带你走进自动化运维的世界,探索如何通过技术手段提升效率和稳定性,以及实现快速响应市场的能力。我们将一起学习如何从基础的脚本编写进化到全面的DevOps实践,包括工具的选择、流程的优化以及文化的建设。无论你是运维新手还是资深专家,这篇文章都将为你提供有价值的见解和实用的技巧。
14 3
|
13天前
|
JSON 测试技术 持续交付
自动化测试与脚本编写:Python实践指南
自动化测试与脚本编写:Python实践指南
16 1
|
29天前
|
人工智能 运维 Devops
自动化运维之路:从脚本到DevOps的转变
【10月更文挑战第7天】在这篇文章中,我们将一起探索自动化运维的演变历程,从最初的简单脚本到现代的DevOps实践。我们将深入理解自动化如何改变了运维工作的本质,并讨论实现这一转变的关键技术和策略。文章将不包含代码示例,而是聚焦于理念、工具和方法论的介绍,旨在为读者提供一个全面的自动化运维框架视图。
|
28天前
|
测试技术 Python
自动化测试项目学习笔记(四):Pytest介绍和使用
本文是关于自动化测试框架Pytest的介绍和使用。Pytest是一个功能丰富的Python测试工具,支持参数化、多种测试类型,并拥有众多第三方插件。文章讲解了Pytest的编写规则、命令行参数、执行测试、参数化处理以及如何使用fixture实现测试用例间的调用。此外,还提供了pytest.ini配置文件示例。
19 2
|
28天前
|
人工智能 运维 监控
自动化运维:从脚本到工具的演变之路
【10月更文挑战第8天】在数字化时代的浪潮中,运维不再是简单的硬件维护,它已经演变成一场关于效率、稳定性和创新的技术革命。本文将带您领略自动化运维的魅力,从最初的脚本编写到现代复杂的自动化工具,我们将一探究竟,看看这些工具如何帮助运维人员简化日常任务,提升工作效率,并最终推动业务发展。
|
23天前
|
监控 关系型数据库 MySQL
PowerShell 脚本编写 :自动化Windows 开发工作流程
PowerShell 脚本编写 :自动化Windows 开发工作流程
20 0
下一篇
无影云桌面