软件测试软件测试职业探索软件测试

pytest的一些高阶用法(二)

2019-01-10  本文已影响2人  迈阿密小白

前言

之前一篇文章pytest的一些高阶用法记录了一些高阶的用法,这边继续实践一些操作

1.报告 pytest-html

这是一版简单的html报告
安装命令

pip install pytest-html

执行脚本的时候,带上需要保存报告的地址和报告名称即可

pytest demo_fixture.py --html=./../report/report.html

这边会在脚本的上级目录,report文件夹中,生成一个html报告,报告内容如下:


report.png
report2.png

2.指定运行用例

可以看到 报告中Test这一列,出现 demo_fixture.py::Test1::()::test_1 这种格式,这其实就是pytest指定运行部分用例的方法,可以试验一下

import pytest

@pytest.fixture(scope="class",autouse=True)
def preclass():
    print("beore class ")
    yield
    print("after class")

@pytest.fixture(scope="module",autouse=True)
def premodule():
    print("beore module ")
    yield
    print("after module")

@pytest.fixture(scope="function",autouse=True)
def pretest():
    print("beore function ")
    yield
    print("after function")

class Test1(object):

    def test_1(self):
        print("this is test1")
        assert 1==1

    def test_2(self):
        print("this is test2")
        assert 1+2 ==3

class Test2(object):
    def test_3(self):
        print("this is test3")
        assert  3+1 ==4

    def test_4(self):
        print("this is test4")
        assert  3+2 ==5

如果我们想只执行Test1 的测试用例,那么执行命令:

pytest -sq demo_fixture_decorator.py::Test1

其结果如下:


Test1.png

如果只想执行Test2中的test_3,那么命令如下:

pytest -sq demo_fixture_decorator.py::Test2::test_3

其结果如下:


test_3.png

仅执行了test_3

同时也支持多个条件,如下:

pytest -sq demo_fixture_decorator.py::Test2::test_3 demo_fixture_decorator.py::Test1

3.conftest

conftest.py 定一个共享的fixture(备注:文件名固定为conftest.py 不可以修改)
一般放在testcase目录中,每个子目录下也可以有conftest.py,优先子目录中的conftest.py中fixture

定一个conftest.py如下:

import pytest

@pytest.fixture(scope="session",autouse="True")
def before():
    print("this is conftest")

当我们再次执行

pytest -sq demo_fixture_decorator.py::Test2::test_3

其结果如下:


conftest.png

对比可以发现,test_3 执行前,先执行了conftest中的代码

4.mark

pytest支持自定义一些标签,在执行脚本的时候,执行指定某些标签或者非某些标签的用例
首先查看pytest 自带的markers

pytest  --markers

@pytest.mark.no_cover: disable coverage for this test.

@pytest.mark.run: specify ordering information for when tests should run in relation to one another. Provided by pytest-ordering. See
 also: http://pytest-ordering.readthedocs.org/

@pytest.mark.flaky(reruns=1, reruns_delay=0): mark test to re-run up to 'reruns' times. Add a delay of 'reruns_delay' seconds between
 re-runs.
.
.
.
.
@pytest.mark.parametrize(argnames, argvalues): call a test function multiple times passing in different arguments in turn. 

@pytest.mark.usefixtures(fixturename1, fixturename2, ...): mark tests as needing all of the specified fixtures. see https://docs.pyte
st.org/en/latest/fixture.html#usefixtures

@pytest.mark.tryfirst: mark a hook implementation function such that the plugin machinery will try to call it first/as early as possi
ble.

@pytest.mark.trylast: mark a hook implementation function such that the plugin machinery will try to call it last/as late as possible
.

可以看到之前提到的一些,如:

参数化用的
@pytest.mark.parametrize

fixture用的
@pytest.mark.usefixtures

失败重跑用的
@pytest.mark.flaky(reruns=1, reruns_delay=0)
等等

我们可以自定义写mark
新增pytest.ini,内容如下:

[pytest]
markers=
    P0: level P0
    P1: level p1
    P2: level p2
    dong: testcase created by dong
    mandy: testcase created by mandy
    cm: testcase about cm
    pd: testcase about pd

再次执行

pytest  --markers

@pytest.mark.P0: level P0

@pytest.mark.P1: level p1

@pytest.mark.P2: level p2

@pytest.mark.dong: testcase created by dong

@pytest.mark.mandy: testcase created by mandy

@pytest.mark.cm: testcase about cm

@pytest.mark.pd: testcase about pd

@pytest.mark.no_cover: disable coverage for this test.

@pytest.mark.run: specify ordering information for when tests should run in relation to one another. Provided by pytest-ordering. See
 also: http://pytest-ordering.readthedocs.org/

@pytest.mark.flaky(reruns=1, reruns_delay=0): mark test to re-run up to 'reruns' times. Add a delay of 'reruns_delay' seconds between
 re-runs.
.
.
.

之前自定义的标签,已经加入了

那么如何使用这些标签呢?

import pytest

@pytest.mark.dong
@pytest.mark.P1
@pytest.mark.cm
class Test1(object):
    def test_1(self):
        print("this is test1")
        assert 1==1
    @pytest.mark.P0
    def test_2(selfs):
        print("this is test2")
        assert 1+2 ==3

@pytest.mark.mandy
@pytest.mark.P0
class Test2(object):
    def test_3(self):
        print("this is test3")
        assert  3+1 ==4

可以看到在脚本中加上了我们自定义的标签,如果向指定执行部分标签的用例执行方法:

pytest -sq -m "P0" demo_mark.py  ---仅执行P0级的用例

结果如下,和预期的一致,仅运行P0级的用例


P0

同时也支持一些逻辑

pytest -sq -m "P0 and cm" demo_mark.py    ---运行cm模块的P0级用例

预期结果:仅运行 test_2

P0 and cm.png

这一段,可以参考官网文档.

也可以根据用例名称进行筛选 -k

pytest -sq -k 2 demo_mark.py

预期只会执行 名字中包含2 的用例,即执行 test_2 和 Test2


image.png

示例代码,参考github

上一篇下一篇

猜你喜欢

热点阅读