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

目录
相关文章
|
2月前
|
Python
自动化微信朋友圈:Python脚本实现自动发布动态
本文介绍如何使用Python脚本自动化发布微信朋友圈动态,节省手动输入的时间。主要依赖`pyautogui`、`time`、`pyperclip`等库,通过模拟鼠标和键盘操作实现自动发布。代码涵盖打开微信、定位朋友圈、准备输入框、模拟打字等功能。虽然该方法能提高效率,但需注意可能违反微信使用条款,存在风险。定期更新脚本以适应微信界面变化也很重要。
213 61
|
3月前
|
数据采集 监控 数据挖掘
Python自动化脚本:高效办公新助手###
本文将带你走进Python自动化脚本的奇妙世界,探索其在提升办公效率中的强大潜力。随着信息技术的飞速发展,重复性工作逐渐被自动化工具取代。Python作为一门简洁而强大的编程语言,凭借其丰富的库支持和易学易用的特点,成为编写自动化脚本的首选。无论是数据处理、文件管理还是网页爬虫,Python都能游刃有余地完成任务,极大地减轻了人工操作的负担。接下来,让我们一起领略Python自动化脚本的魅力,开启高效办公的新篇章。 ###
|
30天前
|
Web App开发 人工智能 JSON
AutoMouser:AI Chrome扩展程序,实时跟踪用户的浏览器操作,自动生成自动化操作脚本
AutoMouser是一款Chrome扩展程序,能够实时跟踪用户交互行为,并基于OpenAI的GPT模型自动生成Selenium测试代码,简化自动化测试流程。
139 17
AutoMouser:AI Chrome扩展程序,实时跟踪用户的浏览器操作,自动生成自动化操作脚本
|
26天前
|
前端开发 JavaScript 测试技术
使用ChatGPT生成登录产品代码的测试用例和测试脚本
使用ChatGPT生成登录产品代码的测试用例和测试脚本
82 35
|
26天前
|
前端开发 JavaScript Java
通过ChatGPT生成测试用例和测试脚本(2)
通过ChatGPT生成测试用例和测试脚本
66 21
|
1月前
|
存储 测试技术 API
pytest接口自动化测试框架搭建
通过上述步骤,我们成功搭建了一个基于 `pytest`的接口自动化测试框架。这个框架具备良好的扩展性和可维护性,能够高效地管理和执行API测试。通过封装HTTP请求逻辑、使用 `conftest.py`定义共享资源和前置条件,并利用 `pytest.ini`进行配置管理,可以大幅提高测试的自动化程度和执行效率。希望本文能为您的测试工作提供实用的指导和帮助。
114 15
|
1月前
|
Web App开发 数据采集 JavaScript
Chrome浏览器实例的TypeScript自动化脚本
Chrome浏览器实例的TypeScript自动化脚本
|
2月前
|
Android开发 开发者 Python
通过标签清理微信好友:Python自动化脚本解析
微信已成为日常生活中的重要社交工具,但随着使用时间增长,好友列表可能变得臃肿。本文介绍了一个基于 Python 的自动化脚本,利用 `uiautomator2` 库,通过模拟用户操作实现根据标签批量清理微信好友的功能。脚本包括环境准备、类定义、方法实现等部分,详细解析了如何通过标签筛选并删除好友,适合需要批量管理微信好友的用户。
108 7
|
2月前
|
运维 Kubernetes Devops
自动化运维:从脚本到工具的演进之旅
在数字化浪潮中,自动化运维成为提升效率、保障系统稳定的关键。本文将探索自动化运维的发展脉络,从基础的Shell脚本编写到复杂的自动化工具应用,揭示这一技术变革如何重塑IT运维领域。我们将通过实际案例,展示自动化运维在简化工作流程、提高响应速度和降低人为错误中的重要作用。无论你是初学者还是资深专家,这篇文章都将为你提供宝贵的洞见和实用的技巧。
|
3月前
|
SQL 测试技术 API
如何编写API接口的自动化测试脚本
本文详细介绍了编写API自动化测试脚本的方法和最佳实践,涵盖确定测试需求、选择测试框架、编写测试脚本(如使用Postman和Python Requests库)、参数化和数据驱动测试、断言和验证、集成CI/CD、生成测试报告及维护更新等内容,旨在帮助开发者构建高效可靠的API测试体系。

热门文章

最新文章