• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Pytest + Allure + Jenkins

武飞扬头像
洋气月
帮助1

目录

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

具体使用

学新通

  1.  
    import pytest
  2.  
    class Testcase:
  3.  
    def setup(self):
  4.  
    print('start: --------loading-------- ')
  5.  
     
  6.  
    def teardown(self):
  7.  
    print('end:------------close--------')
  8.  
     
  9.  
    def test_01(self):
  10.  
    print("the first test")
  11.  
    assert 1 == 3
  12.  
     
  13.  
    # 用例都是从上往下依次执行,若想先执行这个,可以用mark设置
  14.  
    @pytest.mark.run(order=1)
  15.  
    def test_02(self):
  16.  
    print("the second test")
  17.  
    assert 1 == 1
  18.  
     
  19.  
    if __name__ == '__main__':
  20.  
    # ['-s','-v','test_py.py']
  21.  
    # -s 显示结果 -v显示详细用例
  22.  
    pytest.main()

学新通学新通

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

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

安装allure pytest(pip)

Unittest VS pytest

学新通

 学新通

 学新通

学新通

 pytest安装&规则&约束

学新通

   学新通

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

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

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

pytest断言

电商网站登录

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

  1.  
    from _pytest.monkeypatch import derive_importpath
  2.  
    import pytest
  3.  
    from selenium import webdriver
  4.  
    from time import sleep
  5.  
     
  6.  
    def test_login_success():
  7.  
    driver_path = r"C:\Program Files\Google\Chrome\Application\chromedriver.exe"
  8.  
    driver = webdriver.Chrome(executable_path=driver_path)
  9.  
    driver.get("http://39.98.138.157/shopxo")
  10.  
    driver.find_element_by_link_text("登录").click()
  11.  
    driver.find_element_by_xpath("//*[@name='accounts']").send_keys("1111111111")
  12.  
    driver.find_element_by_xpath('//*[@name="pwd"]').send_keys("1111111111")
  13.  
    driver.find_element_by_xpath("//button[text()='登录']").click()
  14.  
    sleep(2)
  15.  
    # test equall?
  16.  
    #
  17.  
    welcome = driver.find_element_by_xpath("//*[contains(text(),'欢迎来到')]").text
  18.  
     
  19.  
    # assert '1111111111, 欢迎来到' == welcome
  20.  
     
  21.  
    driver.quit()
  22.  
     
  23.  
    if __name__ == '__main__':
  24.  
    pytest.main(["-s","test_cema_assert.py"])

断言成功:学新通

  常用断言方法:学新通

pytest参数化

学新通

  1.  
    import pytest
  2.  
    from selenium import webdriver
  3.  
    from time import sleep
  4.  
     
  5.  
    @pytest.mark.parametrize(
  6.  
    "user,pw,excepted",
  7.  
    [("1111111111","1111111111","1111111111,欢迎来到"),
  8.  
    ("dsgfas","1","1111111111,欢迎来到")],
  9.  
    ids=["case1","case2"])
  10.  
    #以上是两组用例:case1和case2
  11.  
    def test_login(user,pw,excepted):
  12.  
    driver_path = r"C:\Program Files\Google\Chrome\Application\chromedriver.exe"
  13.  
    driver = webdriver.Chrome(executable_path=driver_path)
  14.  
    driver.get("http://39.98.138.157/shopxo")
  15.  
    driver.find_element_by_link_text("登录").click()
  16.  
    driver.find_element_by_xpath("//*[@name='accounts']").send_keys(user)
  17.  
    driver.find_element_by_xpath('//*[@name="pwd"]').send_keys(pw)
  18.  
    driver.find_element_by_xpath("//button[text()='登录']").click()
  19.  
    sleep(2)
  20.  
    # test equall?
  21.  
    #
  22.  
    welcome = driver.find_element_by_xpath("//*[contains(text(),'欢迎来到')]").text
  23.  
     
  24.  
    assert excepted == welcome
  25.  
     
  26.  
    driver.quit()
  27.  
     
  28.  
    if __name__ == '__main__':
  29.  
    pytest.main(["-s","test_param.py"])

学新通

pytest常用运行参数

 学新通学新通

 学新通

 学新通

 pytest生成测试报告

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

学新通

 学新通

(2)生成在线报告

学新通

pytest控制测试用例执行

  1.  
    import pytest
  2.  
     
  3.  
    def test_fail01():
  4.  
    print("第一次失败")
  5.  
    assert 1==2
  6.  
     
  7.  
    def test_fail02():
  8.  
    print("第二次失败")
  9.  
    assert 1==2
  10.  
     
  11.  
    def test_fail03():
  12.  
    print("第三次成功")
  13.  
    assert 1==1
  14.  
     
  15.  
    if __name__ == '__main':
  16.  
    pytest.main(["--maxfail=2","test_control.py"])
  17.  
     
  18.  
     
  19.  
     

学新通

 多进程运行用例

安装pytest-xdist(用pip)

将测试发送到2个CPU

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

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

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

  1.  
    import py
  2.  
    import pytest
  3.  
     
  4.  
    def test_case01():
  5.  
    assert 1==1
  6.  
     
  7.  
    def test_case02():
  8.  
    assert 1==1
  9.  
     
  10.  
    def test_case03():
  11.  
    assert 1==12
  12.  
     
  13.  
    def test_case04():
  14.  
    assert 1==3
  15.  
     
  16.  
    def test_case05():
  17.  
    assert 1==12
  18.  
     
  19.  
    def test_case06():
  20.  
    assert 1==1
  21.  
     
  22.  
    if __name__ == '__main__':
  23.  
    # 将测试发送到2个CPU
  24.  
    # pytest.main(["-n","2","test_many.py"])
  25.  
    # 使用与计算机具有的CPU内核一样多的进程
  26.  
    pytest.main(["-n","auto","test_many.py"])
  27.  
     

 学新通

通过标记表达式执行用例

pytest -m slow

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

配置pytest.ini文件:

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

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

  1.  
    import pytest
  2.  
     
  3.  
    def test_fail01():
  4.  
    print("第一次失败")
  5.  
    assert 1==2
  6.  
     
  7.  
    @pytest.mark.slow
  8.  
    def test_fail02():
  9.  
    print("第二次失败")
  10.  
    assert 1==2
  11.  
     
  12.  
    @pytest.mark.slow
  13.  
    def test_fail03():
  14.  
    print("第三次成功")
  15.  
    assert 1==1
  16.  
     
  17.  
    if __name__ == '__main':
  18.  
    # pytest.main(["--maxfail=2","test_control.py"])
  19.  
    # 通过标记表达式执行
  20.  
    pytest.main(["-m","slow","test_mark.py"])
  21.  
     

 重新运行失败用例

学新通

 学新通

学新通

 学新通

 学新通

 pytest的类外  setup & teardown函数

学新通

  1.  
    import pytest
  2.  
     
  3.  
    def multiply(a,b):
  4.  
    return a * b
  5.  
     
  6.  
    def setup_module(module):
  7.  
    print("setup_module=========================>")
  8.  
     
  9.  
    def teardown_module(module):
  10.  
    print("teardown_module========================>")
  11.  
     
  12.  
    def setup_function(function):
  13.  
    print("setup_function--------------------->")
  14.  
     
  15.  
    def teardown_function(function):
  16.  
    print("teardown_dunction----------------->")
  17.  
     
  18.  
    def setup():
  19.  
    print("setup_module====================>")
  20.  
     
  21.  
    def setup():
  22.  
    print("setup----->")
  23.  
     
  24.  
    def teardown():
  25.  
    print("teardown--->")
  26.  
     
  27.  
    def test_mul_01():
  28.  
    print('test_3_4')
  29.  
    assert multiply(3,4) == 12
  30.  
     
  31.  
    def test_mul_02():
  32.  
    print('test_6_8')
  33.  
    assert multiply(6,8) == 48
  34.  
     
  35.  
    if __name__ == '__main__':
  36.  
    pytest.main(["-s","test_setup01.py"])

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

学新通

 类内的setup和teardown函数:

学新通

学新通

学新通

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

学新通

  1.  
    import pytest
  2.  
     
  3.  
    @pytest.fixture
  4.  
    def first_fix():
  5.  
    return ["a"]
  6.  
     
  7.  
    def test_str(first_fix):
  8.  
    # 测试执行
  9.  
    first_fix.append("b")
  10.  
    # 断言
  11.  
    assert first_fix == ["a", "b"]
  12.  
    print(first_fix)
  13.  
     
  14.  
    if __name__ == '__main__':
  15.  
    pytest.main(["-s","-v"])

 学新通

 原生实现(不使用fixture):

  1.  
    def first_var():
  2.  
    return ["a"]
  3.  
     
  4.  
    def test_str(varA):
  5.  
    varA.append("b")
  6.  
    assert varA == ["a", "b"]
  7.  
    print(varA)
  8.  
     
  9.  
    entry = first_var()
  10.  
    test_str(entry)

学新通

fixture调用fixture

  1.  
    import pytest
  2.  
     
  3.  
    @pytest.fixture
  4.  
    def first_entry():
  5.  
    return "a"
  6.  
     
  7.  
    @pytest.fixture
  8.  
    def order(first_entry):
  9.  
    return [first_entry]
  10.  
     
  11.  
    def test_string(order):
  12.  
    order.append("b")
  13.  
     
  14.  
    assert order == ["a","b"]
  15.  
    print(order)
  16.  
     
  17.  
    if __name__ == '__main__':
  18.  
    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

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhhkbgfi
系列文章
更多 icon
同类精品
更多 icon
继续加载