Pytest测试框架

2020-09-09  本文已影响0人  info_gu

理论:


image.png

测试用例的识别与运行:


image.png

安装:pip install pytest

https://docs.pytest.org/en/stable/

创建文件(直接运行是没有输出的因为使用的默认的unittest测试运行):


image.png

如何修改:


image.png

再次运行该文件:


image.png

如果需要再次使用python解释器则,需要添加一个python解释器


image.png

添加完后运行则使用python解释器:


image.png

pytest运行方式:

import pytest


def inc(x):
    return x + 1


def test_answer():
    assert inc(3) == 5

if __name__ == '__main__':
    pytest.main(['test_demo01.py'])
image.png

c参数:

image.png

pytest参数化:

外部文件或者外部数据

第一种外部数据:

def test_answer():
    assert inc(3) == 5
import pytest


def inc(x):
    return x + 1

@pytest.mark.parametrize('a,b',[
    (2,2),
    (3,4)
])
def test_answer(a,b):
    assert inc(a) == b

if __name__ == '__main__':
    pytest.main(['test_demo01.py'])

pytest如何setup和teardown

fixtrue:

import pytest
@pytest.fixture()
def test_login():
    print("登录成功")

def test_inc(test_login):
    print("test fuc")
import pytest
@pytest.fixture()
def test_login():
    return  "登录成功"

def test_inc(test_login):
    print(test_login)
    print("test fuc")

pytest命令参数详解:

image.png
上一篇下一篇

猜你喜欢

热点阅读