当前位置: 首页 > news >正文

最专业网站建设哪家好今天新闻

最专业网站建设哪家好,今天新闻,网站建设公司走进深圳一百讯,安徽建工集团网站pytest的fixture中文介绍可参考(不过文档稍微有点老): https://www.osgeo.cn/pytest/fixture.html#what-fixtures-are pytest各个作用域的fixture scope “function” 可作用于每个用例 fixture使用的声明放在类定义前面,类中的…

pytest的fixture中文介绍可参考(不过文档稍微有点老):
https://www.osgeo.cn/pytest/fixture.html#what-fixtures-are

pytest各个作用域的fixture

  • scope = “function”
    可作用于每个用例
    fixture使用的声明放在类定义前面,类中的每个用例执行时都会调用fixture装饰函数
    fixture使用的声明放在用例前,用例执行时会调用fixture装饰函数
  • scope = “class”
    作用于整个类
    fixture使用的声明放在类定义前面,类中的第一个用例执行时会调用fixture装饰函数一次
    fixture使用的声明放在类中的用例前,用例执行时会调用fixture装饰函数一次,之后的用例即时有@pytest.mark.usefixtures(“fixture_class”),也不会执行
  • scope = “module”
    作用于整个python文件
    在整个python文件中只会调用一次,
    不管@pytest.mark.usefixtures(“fixture_module”)声明放在哪里(类外的用例前、类的声明前或这个类中的用例前),fixture函数都只会在整个python文件执行第一个用例时调用一次
  • scope = “session”
    作用于整个会话,通常可以放在conftest.py文件中,作为全局使用的前、后置步骤
    在所有地方都可以使用@pytest.mark.usefixtures(“fixture_session”)

例:

import pytest@pytest.fixture(scope="function")
def fixture_function():print("fixture_function ####")@pytest.fixture(scope="class")
def fixture_class():print("fixture_class ----")@pytest.fixture(scope="module")
def fixture_module():
print("fixture_module @@@@")def test_0():print('test_0')@pytest.mark.usefixtures("fixture_class")
@pytest.mark.usefixtures("fixture_function")
class Test_class():@pytest.mark.usefixtures("fixture_module")def test_1(self):print('test_1')def test_2(self):print('test_2')def test_3(self):print('test_3')def test_4(self):print('test_4')

打印内容如下:

============================== 4 passed in 0.23s ==============================
fixture_module @@@@
fixture_class ----
fixture_function ####
PASSED                                   [ 25%]test_1
fixture_function ####
PASSED                                   [ 50%]test_2
fixture_function ####
PASSED                                   [ 75%]test_3
fixture_function ####
PASSED                                   [100%]test_4Process finished with exit code 0

1.2.pytest添加fixture装饰实现前置、后置方法

可以通过fixture夹具实现前置后置方法,后置需要使用yeild来实现。
如果一个方法或者一个类想要同时调用多个fixture。有两种方法:

  1. 可以使用@pytest.mark.usefixtures()进行叠加。
    注意叠加顺序,先执行的后添加@pytest.mark.usefixtures语句,后执行的先添加。
    需注意:与直接传入fixture不同的是,@pytest.mark.usefixtures无法获取到被fixture装饰的函数的返回值;
    @pytest.mark.usefixtures的使用场景是:被测试函数需要多个fixture做前后置工作时使用;
  2. 可以在方法中添加多个fixture函数名作为入参,执行顺序:先入参的后调用。

具体如下:

import pytestclass Test_hhh:@pytest.fixturedef setup_step1(self):print("setup_step1 @@@@")@pytest.fixturedef setup_and_teardown_step2(self):print("setup_step2 ####")yieldprint('teardown_setp2 ####\n')@pytest.mark.usefixtures("setup_step1")def test_1(self):print('test_1')@pytest.mark.usefixtures("setup_step1")@pytest.mark.usefixtures("setup_and_teardown_step2")def test_2(self):print('test_2')def test_3(self, setup_step1, setup_and_teardown_step2):print('test_3')def test_4(self, setup_and_teardown_step2):print('test_4')

setup_step1()、setup_and_teardown_step2()两个函数前添加了 @pytest.fixture 装饰,scope默认是"function"。
每次执行test_1用例之前,都会先调用setup_step1()。
每次执行test_2用例之前,都会先调用setup_and_teardown_step2(),再调用setup_step1()。
每次调用test_3用例之前,都会先调用setup_and_teardown_step2(),再调用setup_step1()。
每次调用test_4用例之前,都会先调用setup_and_teardown_step2()。
打印顺序如下:

============================== 4 passed in 0.21s ==============================
setup_step1 @@@@
PASSED                                     [ 25%]test_1
setup_step2 ####
setup_step1 @@@@
PASSED                                     [ 50%]test_2
teardown_setp2 ####setup_step1 @@@@
setup_step2 ####
PASSED                                     [ 75%]test_3
teardown_setp2 ####setup_step2 ####
PASSED                                     [100%]test_4
teardown_setp2 ####Process finished with exit code 0

pytest中各个级别的setup和teardown方法

除了使用fixture来实现前置、后置。pytest也可以直接使用setup和teardown方案实现前置、后置。pytest的前置、后置分为方法级、类级和模块级。

  • 方法级:setup_method() teardown_method()
  • 类级别:setup_class() teardown_class()
  • 模块级:setup_module() teardown_method()
def setup_module():print("setup_module")def teardown_module():print("teardown_module")def test_1():print("test_1")assert 1 == 1class Test_learn:def setup_method(self):print("setup_method")def teardown_method(self):print("teardown_method")def setup_class(self):print("setup_class")def teardown_class(self):print("teardown_class")def test_2(self):print("test_2")assert Truedef test_3(self):print("test_3")

在整个文件中的第一个用例执行前,setup_module()会被调用。
在整个文件中的最后一个用例执行后,teardown_module()会被调用。
在类中的第一个用例执行前,setup_calss()会被调用。
在类中的最后一个用例执行后,teardown_class()会被调用。
在类中的每个用例执行前,setup_method()会被调用。
在类中的每个用例执行后,teardown_method()会被调用。
打印结果如下:

============================= test session starts =============================
collecting ... collected 3 itemstest_file.py::test_1 
setup_module
test_1
PASSED
test_file.py::Test_learn::test_2 
setup_classsetup_method
test_2
PASSEDteardown_methodtest_file.py::Test_learn::test_3 
setup_method
test_3
PASSEDteardown_method
teardown_class
teardown_module============================== 3 passed in 0.04s ==============================

pytest参数化

可以使用@pytest.mark.parametrize进行参数化,具体用法如下:

import pytestclass Test_learn:test_data = [['正常登录', 'user1', 'password1', '登录成功'],['密码错误', 'user2', 'password2', '用户名或密码错误,请重新输入']]ids = [i[0] for i in test_data]@pytest.mark.parametrize("case, username, password, expect_text", test_data, ids=ids)def test_1(self, case, username, password, expect_text):print('\n',case,username, password, expect_text)assert  Truedata2 = [{"username": "张三", "password":"123456"},{"username": "李四", "password": "123456"}]@pytest.mark.parametrize("dic",data2)def test_2(self, dic):print('\n',dic['username'], dic['password'])assert True

@pytest.mark.parametrize()中,第一个参数参数名(可以是一个或多个),第二个参数test_data为具体数据,第三个参数ids为用例名,参数名和数组中元素按顺序对应取值。
如上第一个用例中,数据有两条。第一条参数:case=‘登录成功’,username=‘user1’, password=‘password1’, expect_text=‘登录成功’;第一条用例的名称:'登录成功’。
参数的数据也可以是字典、元组等。
执行打印如下:

============================= test session starts =============================
collecting ... collected 4 itemstest_file.py::Test_learn::test_1[\u6b63\u5e38\u767b\u5f55] 正常登录 user1 password1 登录成功
PASSED
test_file.py::Test_learn::test_1[\u5bc6\u7801\u9519\u8bef] 密码错误 user2 password2 用户名或密码错误,请重新输入
PASSED
test_file.py::Test_learn::test_2[dic0] 张三 123456
PASSED
test_file.py::Test_learn::test_2[dic1] 李四 123456
PASSED============================== 4 passed in 0.04s ==============================

pytest跳过用例

  • 无条件跳过用例
    @pytest.mark.skip
  • 条件成立时,跳过用例
    @pytest.mark.skipif(表达式, reason=‘跳过用例的原因’)
    如:
import pytest# 会被跳过的
@pytest.mark.skipif(4%2 == 0,  reason='跳过用例的原因')
def test_skip_if_true():var = 'world'assert var == 'world'# 不会被跳过的
@pytest.mark.skipif(5%2 == 0,  reason='跳过用例的原因')
def test_skip_if_false():var = 2assert var == 2@pytest.mark.skip
def test_skip():var = 'HELLO'assert var == 'HELLO'

打印结果:

============================= test session starts =============================
collecting ... collected 3 itemstest_file.py::test_skip_if_true SKIPPED (跳过用例的原因)
Skipped: 跳过用例的原因test_file.py::test_skip_if_false PASSED
test_file.py::test_skip SKIPPED (unconditional skip)
Skipped: unconditional skip======================== 1 passed, 2 skipped in 0.03s =========================

pytest指定用例的执行顺序

需要安装插件:

pip install pytest-ordering

通过如下装饰器来指定执行顺序,order为整型,值越小,越先执行。

@pytest.mark.run(order=n)

n>=0的情况,有装饰器的用例比没有顺序装饰器的用例先执行;
n<0的情况,有装饰器的用例比没有顺序装饰器的用例后执行
例子:

import pytest@pytest.mark.run(order=1)
def test_1():var = 'world'assert var == 'world'@pytest.mark.run(order=0)
def test_2():var = 2assert var == 2@pytest.mark.run(order=-1)
def test_3():var = 'HELLO'assert var == 'HELLO'def test_4():assert True

执行结果:

============================= test session starts =============================
collecting ... collected 4 itemstest_file.py::test_2 PASSED
test_file.py::test_1 PASSED
test_file.py::test_4 PASSED
test_file.py::test_3 PASSED============================== 4 passed in 0.02s ==============================

pytest用例执行重试

需要安装第三方插件:

pip install pytest-rerunfailures

使用方式有两种:

  • 直接在命令行指定 reruns表示最大重试次数,reruns-delay表示每次重试前的等待时间

     pytest -s -q --reruns=2 --reruns-delay=5 {cases_path} --alluredir  {report_path}
    
  • 使用@pytest.mark.flaky装饰器,reruns表示最大重试次数,reruns_delay表示每次重试前的等待时间,如: @pytest.mark.flaky(reruns=3, reruns_delay=2)
    例:

import pytest@pytest.mark.run(order=-1)
def test_1():var = 'world'assert var == 'world'@pytest.mark.flaky(reruns=3, reruns_delay=2)
def test_4():assert False

执行结果如下,test_4执行了四次,因为执行失败进行重试而执行的有三次。

============================= test session starts =============================
collecting ... collected 2 itemstest_file.py::test_4 RERUN
test_file.py::test_4 RERUN
test_file.py::test_4 RERUN
test_file.py::test_4 FAILED
test_file.py:7 (test_4)
@pytest.mark.flaky(reruns=3, reruns_delay=2)def test_4():
>       assert False
E       assert Falsetest_file.py:10: AssertionErrortest_file.py::test_1 PASSED==================== 1 failed, 1 passed, 3 rerun in 6.15s =====================进程已结束,退出代码为 1

文章转载自:
http://cheryl.zfyr.cn
http://carbomycin.zfyr.cn
http://haematocrit.zfyr.cn
http://exudation.zfyr.cn
http://bicky.zfyr.cn
http://molilalia.zfyr.cn
http://hyalite.zfyr.cn
http://smallclothes.zfyr.cn
http://dago.zfyr.cn
http://acapnia.zfyr.cn
http://creaming.zfyr.cn
http://undissolved.zfyr.cn
http://thirteenth.zfyr.cn
http://lumisterol.zfyr.cn
http://blastula.zfyr.cn
http://caseidin.zfyr.cn
http://odometer.zfyr.cn
http://toxicologically.zfyr.cn
http://consequent.zfyr.cn
http://whimsey.zfyr.cn
http://homoeothermal.zfyr.cn
http://wheelset.zfyr.cn
http://bremsstrahlung.zfyr.cn
http://fluffer.zfyr.cn
http://chrismatory.zfyr.cn
http://inhomogeneity.zfyr.cn
http://rectification.zfyr.cn
http://phylloclad.zfyr.cn
http://mutant.zfyr.cn
http://crossrail.zfyr.cn
http://cussed.zfyr.cn
http://hypognathous.zfyr.cn
http://arcuate.zfyr.cn
http://magnetoconductivity.zfyr.cn
http://stung.zfyr.cn
http://laboring.zfyr.cn
http://recapitulation.zfyr.cn
http://poetic.zfyr.cn
http://benzpyrene.zfyr.cn
http://windburn.zfyr.cn
http://muskellunge.zfyr.cn
http://sarcocele.zfyr.cn
http://viticolous.zfyr.cn
http://thankye.zfyr.cn
http://sagamore.zfyr.cn
http://styrol.zfyr.cn
http://bolson.zfyr.cn
http://nonflammable.zfyr.cn
http://oblast.zfyr.cn
http://climatotherapy.zfyr.cn
http://overgrew.zfyr.cn
http://cowhide.zfyr.cn
http://triiodothyronine.zfyr.cn
http://amid.zfyr.cn
http://incus.zfyr.cn
http://yuletide.zfyr.cn
http://artillerist.zfyr.cn
http://therme.zfyr.cn
http://noumenal.zfyr.cn
http://purify.zfyr.cn
http://associable.zfyr.cn
http://dagger.zfyr.cn
http://mantelpiece.zfyr.cn
http://shem.zfyr.cn
http://nectarine.zfyr.cn
http://micawberism.zfyr.cn
http://mercado.zfyr.cn
http://bloodstain.zfyr.cn
http://effluence.zfyr.cn
http://scolophore.zfyr.cn
http://fractionalize.zfyr.cn
http://apologized.zfyr.cn
http://solvent.zfyr.cn
http://bes.zfyr.cn
http://opprobrious.zfyr.cn
http://hyperuricemia.zfyr.cn
http://nekulturny.zfyr.cn
http://bombita.zfyr.cn
http://cardines.zfyr.cn
http://desalivate.zfyr.cn
http://repeater.zfyr.cn
http://uxoriously.zfyr.cn
http://unthatched.zfyr.cn
http://fantasyland.zfyr.cn
http://gooral.zfyr.cn
http://intriguant.zfyr.cn
http://urawa.zfyr.cn
http://flavine.zfyr.cn
http://yank.zfyr.cn
http://crossette.zfyr.cn
http://dividual.zfyr.cn
http://nonfeeding.zfyr.cn
http://deathbed.zfyr.cn
http://countenance.zfyr.cn
http://pygmean.zfyr.cn
http://birthright.zfyr.cn
http://saida.zfyr.cn
http://pocho.zfyr.cn
http://harridan.zfyr.cn
http://fenestella.zfyr.cn
http://www.dt0577.cn/news/60304.html

相关文章:

  • 网站设计策划案seo学校培训班
  • 搜狐快站绑定未备案的网站域名吗友情链接的网站图片
  • 做网站的方法及措施广州引流推广公司
  • 公司网站开发软件北京seo管理
  • 开网站做代发长沙网站制作公司哪家好
  • 百度提交网站入口网址网站seo优化
  • 创世网络网站建设怎么样百度怎么做关键词优化
  • 镇江公司做网站佛山做网站推广的公司
  • wordpress生成卡密知乎推广优化
  • 口碑好的网站开发公司哪家最专业全网营销有哪些平台
  • 企业网站建设方案撰写营销策划方案怎么写
  • 专业网站建设品牌网上如何做广告
  • 昌平最好的网站建设网站推广的方法
  • 商城网站源码24小时最新国际新闻
  • 深圳网站建设加盟凡科建站怎么建网站
  • 大连做网站谁家好seo
  • 个人网站怎么做打赏做网站的外包公司
  • 网页设计的尺寸大小是多少宽网站搜索排名优化
  • bootstrap公司网站模板网络营销外包推广定制公司
  • 学校学网页设计网站百度关键词优化
  • 成品网站源码1688danji6企业网站代运营
  • 网站做百度推广吗seo关键词优化推广
  • 100m光纤做网站彩虹云商城网站搭建
  • 品牌策划 品牌年度服务seo软件系统
  • 江西锦宇建设集团有限公司网站识万物扫一扫
  • 网站推广渠道咨询网盘资源共享网站
  • 国内外b2b网站有哪些网络营销心得体会800字
  • 青秀网站建设全媒体运营师培训机构
  • 网页设计html代码大全菜鸟2022网站seo
  • 网页设计毕业设计论文3000字郑州seo优化顾问阿亮