Pytest + Allure + Jenkins

目录

pytest测试框架

安装allure+pytest(pip)

Unittest VS pytest

 pytest安装&规则&约束

pytest断言

  常用断言方法:​

pytest参数化

pytest常用运行参数

 pytest生成测试报告

pytest控制测试用例执行

 多进程运行用例

通过标记表达式执行用例

 重新运行失败用例

 pytest的类外  setup & teardown函数

 类内的setup和teardown函数:

 =========pytest 核心组件fixture========

fixture调用fixture

 使用多个fixture

使用装饰器@pytest.mark.usefixtures()修饰需要运行的用例

 叠加使用usefixtures

 conftest.py-------测试配置文件

 fixture自动使用autouse=True与作用域scope

fixture之params参数化

 跳过测试函数

跳过测试类

跳过模块

单个/多个参数

使用函数返回值形式传入参数值

​ parametrize参数中ids用法

parametrize叠加使用

 修改python traceback的输出

 执行失败跳转pdb

​ Allure测试报告

Jenkins的CI和CD

​ 


 

 

1,pytest测试框架

(1)单元测试:对软件中最小的单元(如函数‘,模块)进行测试

(2)框架:规范,帮助进行用例管理

(3)自动化框架:app自动化------appium

                               web自动化--------selenium

                                接口自动化--------request

具体使用

import pytest
class Testcase:
    def setup(self):
        print('start:  --------loading-------- ')

    def teardown(self):
        print('end:------------close--------')

    def test_01(self):
        print("the first test")
        assert 1 == 3

# 用例都是从上往下依次执行,若想先执行这个,可以用mark设置
    @pytest.mark.run(order=1)
    def test_02(self):
        print("the second test")
        assert 1 == 1

if __name__ == '__main__':
    # ['-s','-v','test_py.py']
    # -s 显示结果    -v显示详细用例
    pytest.main()

 配置pytest.ini文件可以改变命名规则:

[pytest]
python_files=test_*.py *_test.py   #文件都是以test_开头 或者 _test结尾命名,不符合则检测不到
python_classes=Test*
python_functions=test_*

安装allure+pytest(pip)

Unittest VS pytest

 

 

 pytest安装&规则&约束

   

 testpath在ini文件里面定义的测试路径。norecursedirs定义不想找的目录

在pytest.ini中定义:(pytest,ini中不能再注释中写中文,会报错!!)

[pytest]
; 只运行指定路径testpaths下文件(该行中文不可以写)
testpaths = testing doc
; 不运行该指定目录
norecursedirs = doc*

pytest断言

电商网站登录

path里路径 r 表示:后面的r"C:\Program Files\Google\Chrome\Application\chromedriver.exe"中的反斜杠“\”不存在转义,就是原始的路径

from _pytest.monkeypatch import derive_importpath
import pytest
from selenium import webdriver
from time import sleep

def test_login_success():
    driver_path = r"C:\Program Files\Google\Chrome\Application\chromedriver.exe"
    driver = webdriver.Chrome(executable_path=driver_path)
    driver.get("http://39.98.138.157/shopxo")
    driver.find_element_by_link_text("登录").click()
    driver.find_element_by_xpath("//*[@name='accounts']").send_keys("1111111111")
    driver.find_element_by_xpath('//*[@name="pwd"]').send_keys("1111111111")
    driver.find_element_by_xpath("//button[text()='登录']").click()
    sleep(2)
    # test equall?
    #
    welcome = driver.find_element_by_xpath("//*[contains(text(),'欢迎来到')]").text

    # assert '1111111111, 欢迎来到' == welcome

    driver.quit()

if __name__ == '__main__':
    pytest.main(["-s","test_cema_assert.py"])

断言成功:

  常用断言方法:

pytest参数化

import pytest
from selenium import webdriver
from time import sleep

@pytest.mark.parametrize(
    "user,pw,excepted",
    [("1111111111","1111111111","1111111111,欢迎来到"),
    ("dsgfas","1","1111111111,欢迎来到")],
    ids=["case1","case2"])
#以上是两组用例:case1和case2
def test_login(user,pw,excepted):
    driver_path = r"C:\Program Files\Google\Chrome\Application\chromedriver.exe"
    driver = webdriver.Chrome(executable_path=driver_path)
    driver.get("http://39.98.138.157/shopxo")
    driver.find_element_by_link_text("登录").click()
    driver.find_element_by_xpath("//*[@name='accounts']").send_keys(user)
    driver.find_element_by_xpath('//*[@name="pwd"]').send_keys(pw)
    driver.find_element_by_xpath("//button[text()='登录']").click()
    sleep(2)
    # test equall?
    #
    welcome = driver.find_element_by_xpath("//*[contains(text(),'欢迎来到')]").text

    assert excepted == welcome

    driver.quit()

if __name__ == '__main__':
    pytest.main(["-s","test_param.py"])

pytest常用运行参数

 

 

 

 pytest生成测试报告

(1)生成junitXML文件(在指定文件夹report中)

 

(2)生成在线报告

pytest控制测试用例执行

import pytest

def test_fail01():
    print("第一次失败")
    assert 1==2

def test_fail02():
    print("第二次失败")
    assert 1==2

def test_fail03():
    print("第三次成功")
    assert 1==1

if __name__ == '__main':
    pytest.main(["--maxfail=2","test_control.py"])



 多进程运行用例

安装pytest-xdist(用pip)

将测试发送到2个CPU

pytest.main(["-n","2","test_many.py"])

使用与计算机具有的CPU内核一样多的进程

pytest.main(["-n","auto","test_many.py"])

import py
import pytest

def test_case01():
    assert 1==1

def test_case02():
    assert 1==1

def test_case03():
    assert 1==12

def test_case04():
    assert 1==3

def test_case05():
    assert 1==12

def test_case06():
    assert 1==1

if __name__ == '__main__':
    # 将测试发送到2个CPU
    # pytest.main(["-n","2","test_many.py"])
    # 使用与计算机具有的CPU内核一样多的进程
    pytest.main(["-n","auto","test_many.py"])

 

通过标记表达式执行用例

pytest -m slow

这条命令会执行被装饰器@pytest.mark.slow装饰的所有测试用例

配置pytest.ini文件:

[pytest]
markers = 
    slow: marks tests as slow(deselect with '-m "not slow"')
    serial

 前面表示会执行mark的用例,括号内是不选中标记的用例:则只会执行后两条

import pytest

def test_fail01():
    print("第一次失败")
    assert 1==2

@pytest.mark.slow
def test_fail02():
    print("第二次失败")
    assert 1==2

@pytest.mark.slow
def test_fail03():
    print("第三次成功")
    assert 1==1

if __name__ == '__main':
    # pytest.main(["--maxfail=2","test_control.py"])
    # 通过标记表达式执行
    pytest.main(["-m","slow","test_mark.py"])

 重新运行失败用例

 

 

 

 pytest的类外  setup & teardown函数

import pytest

def multiply(a,b):
    return a * b

def setup_module(module):
    print("setup_module=========================>")

def teardown_module(module):
    print("teardown_module========================>")

def setup_function(function):
    print("setup_function--------------------->")

def teardown_function(function):
    print("teardown_dunction----------------->")

def setup():
    print("setup_module====================>")

def setup():
    print("setup----->")

def teardown():
    print("teardown--->")

def test_mul_01():
    print('test_3_4')
    assert multiply(3,4) == 12

def test_mul_02():
    print('test_6_8')
    assert multiply(6,8) == 48

if __name__ == '__main__':
    pytest.main(["-s","test_setup01.py"])

 类之外的执行顺序:module  ---> function ---> setup

 类内的setup和teardown函数:

 =========pytest 核心组件fixture========

import pytest

@pytest.fixture
def first_fix():
    return ["a"]

def test_str(first_fix):
    # 测试执行
    first_fix.append("b")
    # 断言
    assert first_fix == ["a", "b"]
    print(first_fix)

if __name__ == '__main__':
    pytest.main(["-s","-v"])

 

 原生实现(不使用fixture):

def first_var():
    return ["a"]

def test_str(varA):
    varA.append("b")
    assert varA == ["a", "b"]
    print(varA)

entry = first_var()
test_str(entry)

fixture调用fixture

import pytest

@pytest.fixture
def first_entry():
    return "a"

@pytest.fixture
def order(first_entry):
    return [first_entry]

def test_string(order):
    order.append("b")

    assert order == ["a","b"]
    print(order)

if __name__ == '__main__':
    pytest.main(["-s"])

 使用多个fixture

分开的用法:

 

使用装饰器@pytest.mark.usefixtures()修饰需要运行的用例

都指定使用fixture1,所以fixture2未用!

 叠加使用usefixtures

 

 

 conftest.py-------测试配置文件

 配置文件:

 fixture自动使用autouse=True与作用域scope

 

 

 

class也执行在类外 方法,function每一个方法都执行

fixture之params参数化

 

执行1,2,3,次

 跳过测试函数

无条件跳过测试函数

只执行用例2,其余两个跳过

 自定义skip条件:

 pytest.skip()方法跳过函数

 只执行3

跳过测试类

只执行1

跳过模块

 

 跳过所有用例

单个/多个参数

 单参数:

多参数:

使用函数返回值形式传入参数值

 parametrize参数中ids用法

 方法一:

方法二:

parametrize叠加使用

当出现叠加使用和不叠加使用同时存在,优先使用叠加使用!ids也会叠加,叠加后,ids显示顺序是下面的叠加在上面的前面,而参数执行顺序是(a=11,b=21,c=31)(a=12,b=21,c=31).......

 修改python traceback的输出

 执行失败跳转pdb

 Allure测试报告

 

Jenkins的CI和CD

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

洋气月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值