pytest 测试框架

安装

pip安装

pip install pytest

测试使用

官方文档

注意点:

  1. 测试文件要以test_*.py或者*_test.py
  2. Test*类中不能包含__init__方法
  3. pytest解释器,执行命令
  4. 以python执行文件需要有入口函数
pycharm中设置

 pytest运行
 python运行
 简单的参数化

简单的fixture操作

参数化,数据驱动

@pytest.mark.parametrize('a,b', [(1, 1), (1, 2), (2, 3)])

data.yaml文件

-
  - 10
  - 20
-
  - 30
  - 40
-
  - 40
  - 50
参数化导入文件

测试报告定制

工具Allure,文档

python安装

pip install allure-pytest

Windows安装

下载allure-2.20.0.zip压缩包,解压后,双击bat文件,安装成功

运行,测试代码

import pytest


def test_success():
    """this test succeeds"""
    assert True


def test_failure():
    """this test fails"""
    assert False


def test_skip():
    """this test is skipped"""
    pytest.skip('for a reason!')


def test_broken():
    raise Exception('oops')
方式一:直接打开测试报告

现在pycharm的terminal中输入

pytest .\test_allure.py --alluredir=./result/allure

打开Windows的命令行输入

allure serve ./result/allure

方式二:先生成测试报告,然后打开报告
 allure generate ./result/allure -o ./report/ --clean

allure open -h 127.0.0.1 -p 8883 ./report/

 Allure常用特性

在测试报告中看到测试功能,子功能或场景,测试步骤,包括测试的附加信息

@Feature,@story,@step,@attach

测试代码

import pytest
import allure


@allure.feature("登录模块")
class TestLogin:
    @allure.story("登录成功")
    def test_login_success(self):
        print("这是登录:测试用例,登录成功")
        pass

    @allure.story("登录失败")
    def test_login_success_a(self):
        print("这是登录:测试用例,登录成功")
        pass

    @allure.story("用户名缺失")
    def test_login_success_b(self):
        print("用户名缺失")
        pass

    @allure.story("密码缺失")
    def test_login_failure(self):
        with allure.step("点击用户名"):
            print("输入用户名")
        with allure.step("点击密码"):
            print("输入密码")
        print("点击登录")
        with allure.step("点击登录之后登录失败"):
            assert '1' == 1
            print("登录失败")
        pass

    @allure.story("登录失败")
    def test_login_failure_a(self):
        print("这是登录:测试用例,登录失败")
        pass

执行

pytest .\test_al.py --alluredir=./result/test_al
allure serve ./result/test_al

只运行登录模块的测试用例

pytest .\test_al.py --allure-features '登录模块'

只运行登录模块里的登录成功

pytest .\test_al.py  --allure-stories '登录成功' 

@allure.link,@allure.testcase,@allure.issue

给测试用例添加一个连接

测试代码

import allure


@allure.link("https://www.baidu.com/", name='链接')
def test_with_link():
    print("测试链接")
    pass


TEST_CASE_LINK = 'https://docs.qameta.io/allure/#_python'
@allure.testcase(TEST_CASE_LINK, '登录用例')
def test_with_testcase():
    print("测试链接")
    pass


# --allure--link--pattern=issue:http://www.mytesttracker.com/issue/{}
@allure.issue('36', '这是一个issue')
def test_with_issue():
    print("测试链接")
    pass

结果

按照重要级别进行一定范围的测试

 @allure.severity

测试代码

import allure


def test_with_no_severity():
    pass


@allure.severity(allure.severity_level.TRIVIAL)
def test_with_trivial_severity():
    pass


@allure.severity(allure.severity_level.NORMAL)
def test_with_normal_severity():
    pass


@allure.severity(allure.severity_level.NORMAL)
class TestClassWithNormalSeverity:
    def test_inside_the_normal_severity_test_class(self):
        pass

    @allure.severity(allure.severity_level.CRITICAL)
    def test_inside_nsc_with_overriding_critical_severity(self):
        pass

 结果

 在pycharm中配置执行

 前端自动化测试,添加截图

@allure.attach

综合生成报告

pytest+allure+selenium

测试代码

import allure
import pytest
from selenium import webdriver
import time
from selenium.webdriver.common.by import By


@allure.feature("百度搜索")
@pytest.mark.parametrize('test_data', ['allure', 'pytest', 'unittest'])
def test_steps_demo(test_data):
    with allure.step("打开百度页面"):
        driver = webdriver.Chrome()
        driver.get('https://www.baidu.com/')
        driver.maximize_window()

    with allure.step(f"输入搜索词:{test_data}"):
        driver.find_element(By.ID, "kw").send_keys(test_data)
        time.sleep(2)
        driver.find_element(By.ID, "su").click()
        time.sleep(2)

    with allure.step("保存图片"):
        driver.save_screenshot("./result/a.png")
        allure.attach.file("./result/a.png", attachment_type=allure.attachment_type.PNG)

    with allure.step("关闭浏览器"):
        driver.quit()

结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值