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

免费windows云电脑seo公司排行

免费windows云电脑,seo公司排行,网站域名和网址,做网站设计哪家好tep是一款测试工具,在pytest测试框架基础上集成了第三方包,提供项目脚手架,帮助以写Python代码方式,快速实现自动化项目落地。 在tep项目中,自动化测试用例都是放到tests目录下的,每个.py文件相互独立&…

  tep是一款测试工具,在pytest测试框架基础上集成了第三方包,提供项目脚手架,帮助以写Python代码方式,快速实现自动化项目落地。

在tep项目中,自动化测试用例都是放到tests目录下的,每个.py文件相互独立,没有依赖,1个文件即1条用例,彼此分离。

虽然用例也能相互引用,但是除非万不得已,一般不建议这么做,牵一发动全身,后期维护困难。

用例的代码编写,思路是从上往下的,和pytest/unittest/script常规写法无异,不会有学习成本,一般也不会有问题。有成本有问题的可能是环境变量和fixtures,因为tep做了封装,提供了依赖注入的共享方式,fixture又是pytest较难理解的知识点,所以有必要通过本文来讲讲tep环境变量、fixtures、用例三者之间的关系,帮助理解,以便更灵活顺手的借助tep实现pytest自动化项目。

假如不用环境变量和fixtures

假如不用环境变量和fixtures,是完全可以的!比如,在tests下新建脚本login_test.py

from tep.client import requestdef test():response = request("post",url="https://qa.com/api/users/login",headers={"Content-Type": "application/json"},json={"username": "admin","password": "123456",})assert response.status_code < 400

请求接口https://qa.com/api/users/login,断言响应状态码小于400。问题来了:url固定,假如需要切换两个环境qarelease,该怎么办?

参数化

无论是做自动化测试还是性能测试,都会接触到参数化这个词。它是指把代码中的固定数据(硬编码)定义成变量,让每次运行时数据不一样,固定数据变为动态数据。动态数据的来源是变量、数据库、外部文件等。动态数据的类型一般是常量的字符串,也可以是函数,比如JMeter的函数助手,也可以是依赖注入,比如pytest的fixture。

依赖注入的fixture

“依赖注入是控制反转(IoC, Inversion of Control)的一种技术形式”,这句话出自维基百科,我也不知道什么意思,画个图简单表达下:

意思是,给client一个injectorclient不需要做什么,就能用到service

pytest的fixture实现了依赖注入,允许我们在不修改测试代码的情况下,引入fixture来额外添加一些东东。

对于url来说,域名是需要做参数化的,不同环境域名不同,所以tep把它做成了fixture,通过函数参数引入:

from tep.client import request
from tep.fixture import *def test(url):  # 引入fixtureresponse = request("post",url=url("/api/users/login"),headers={"Content-Type": "application/json"},json={"username": "admin","password": "123456",})assert response.status_code < 400

tep.fixture.url定义如下:

@pytest.fixture(scope="session")
def url(env_vars):def domain_and_uri(uri):if not uri.startswith("/"):uri = "/" + urireturn env_vars.domain + urireturn domain_and_uri

如果一眼就看懂了,恭喜你,如果一眼就看懵了,没关系。我会花功夫把它讲明白,它很关键!

把fixture当变量看

虽然从定义上看,fixture是用def关键字定义的函数,但是理解上把它看做变量就可以了。比如:

import pytest@pytest.fixture
def name():return "dongfanger"

一般函数的用法是函数名加小括号,通过name()才能得到"dongfanger"。fixture不一样,以上定义可以理解为:

name = "dongfanger"

"dongfanger"赋值给name,fixture名 = return值。通过变量name就得到"dongfanger"了。

既然是变量,那么就能随便赋值,strfunctionclassobject都行。比如在fixture内部定义个函数:

import pytest@pytest.fixture
def who():def get_name():return "dongfanger"return get_name

理解为把函数名get_name赋值给fixture名变量:

who = get_name

get_name是个函数名,需要加小括号get_name()才能得到"dongfanger"who也必须通过who()才能得到"dongfanger"。再看tep.fixture.url是不是清楚些了:

@pytest.fixture(scope="session")
def url(env_vars):def domain_and_uri(uri):if not uri.startswith("/"):uri = "/" + urireturn env_vars.domain + urireturn domain_and_uri

理解为把函数名domain_and_uri赋值给fixture名变量:

url = domain_and_uri

使用时通过url("/api")得到域名和uri拼接后的结果。

第2行的def url(env_vars):也有一个参数env_vars,接下来继续解释。

fixture参数是其他fixture

fixture的参数只能是其他fixture。比如:

import pytest@pytest.fixture
def chinese_name():return "东方er"@pytest.fixture
def english_name(chinese_name):return "dongfanger"

调用english_name,pytest会先执行参数里的其他fixture chinese_name,然后执行自己english_name

如果把tep.fixture.url拆成两步来看,就很清晰了,第一步:

@pytest.fixture(scope="session")
def url(env_vars):func = Nonereturn func

第二步:

@pytest.fixture(scope="session")
def url(env_vars):func = Nonedef domain_and_uri(uri):if not uri.startswith("/"):uri = "/" + urireturn env_vars.domain + urifunc = domain_and_urireturn func

环境变量

tep.fixture.url的参数是另外一个fixture env_vars 环境变量,它的定义如下:

from tep.fixture import *@pytest.fixture(scope="session")
def env_vars(config):class Clazz(TepVars):env = config["env"]"""Variables define start"""# Environment and variablesmapping = {"qa": {"domain": "https://qa.com",},"release": {"domain": "https://release.com",}# Add your environment and variables}# Define properties for auto displaydomain = mapping[env]["domain"]"""Variables define end"""return Clazz()

只看中间注释"""Variables define start""""""Variables define end"""部分即可。url参数化的域名就在这里,mapping字典建立了环境和变量之间的映射,根据不同的环境key,获取不同的变量value。

config fixture的作用是读取conf.yaml文件里面的配置。

参数化的方式很多,JMeter提供了4种参数化方式,tep的fixture env_vars借鉴了JMeter的用户自定义变量:

env_vars.put()env_vars.get()借鉴了JMeter BeanShell的vars.put()vars.get()

实例:测试多个网址

讲到最后,形成了思路,通过实际的例子,看看环境变量、fixtures、用例是怎么用起来的,加深下印象。假如qa环境有2个网址,学校端和机构端,脚本都需要用到。

第一步修改env_vars,编辑fixture_env_vars.py

        """Variables define start"""# Environment and variablesmapping = {"qa": {"domain": "https://qa.com","domain_school": "https://school.qa.com",  # 新增"domain_org": "https://org.qa.com"  # 新增},"release": {"domain": "https://release.com","domain_school": "https://school.release.com"  # 新增"domain_org": "https://org.release.com"  # 新增}# Add your environment and variables}# Define properties for auto displaydomain = mapping[env]["domain"]domain_school = mapping[env]["domain_school"]  # 新增domain_org = mapping[env]["domain_org"]  # 新增"""Variables define end"""

添加了6行代码,定义了env_vars.domain_schoolenv_vars.domain_org

第二步定义fixtures,新建fixture_url.py

@pytest.fixture(scope="session")
def url_school(env_vars):def domain_and_uri(uri):if not uri.startswith("/"):uri = "/" + urireturn env_vars.domain_school + urireturn domain_and_uri@pytest.fixture(scope="session")
def url_org(env_vars):def domain_and_uri(uri):if not uri.startswith("/"):uri = "/" + urireturn env_vars.domain_org + urireturn domain_and_uri

参照tep.fixture.url,修改env_vars.domainenv_vars.domain_schoolenv_vars.domain_org,新增了2个fixture url_schoolurl_org

更进一步,也许会定义fixture login_schoollogin_org,灵活选择。

小结

本文循序渐进的讲解了tep环境变量、fixtures和用例之间的关系,重点对tep.fixture.url进行了解释,只要理解了它,整体关系就很清楚了。之所以要用fixture,原因一是多人协作共享,我们需要用别人写好的函数,复用返回值,有些同学习惯定义函数参数,参数不变还好,万一哪天改了,别人引用的用例会全部报错,fixture很好的限制了这一点,它默认是不能传参的,虽然可以通过定义内部函数来实现传参,但是并不推荐这么做,宁愿增加冗余代码,定义多个fixture,也比代码耦合度高好一些。原因二是import的问题,pytest会自动查找conftest.py里的fixture,tep会进一步自动查找fixtures下的fixture导入到conftest.py,不需要import就能使用,减少了import代码,避免了可能会出现的循环导入问题。

正在学习测试的小伙伴可以通过点击下面的小卡片


文章转载自:
http://perigee.tzmc.cn
http://lam.tzmc.cn
http://implemental.tzmc.cn
http://sulaiman.tzmc.cn
http://lighter.tzmc.cn
http://achromycin.tzmc.cn
http://megaversity.tzmc.cn
http://lifeward.tzmc.cn
http://linable.tzmc.cn
http://skinny.tzmc.cn
http://roguery.tzmc.cn
http://teleportation.tzmc.cn
http://thane.tzmc.cn
http://neolite.tzmc.cn
http://benty.tzmc.cn
http://alienor.tzmc.cn
http://gage.tzmc.cn
http://fasciae.tzmc.cn
http://hyperirritable.tzmc.cn
http://bankable.tzmc.cn
http://ringtaw.tzmc.cn
http://sozin.tzmc.cn
http://dialectologist.tzmc.cn
http://thickety.tzmc.cn
http://helichrysum.tzmc.cn
http://swannery.tzmc.cn
http://cloud.tzmc.cn
http://petrologist.tzmc.cn
http://genteelly.tzmc.cn
http://photofluorogram.tzmc.cn
http://countershading.tzmc.cn
http://trias.tzmc.cn
http://estray.tzmc.cn
http://recumbently.tzmc.cn
http://autopista.tzmc.cn
http://pa.tzmc.cn
http://unnoted.tzmc.cn
http://neurochemistry.tzmc.cn
http://divestiture.tzmc.cn
http://immunoreaction.tzmc.cn
http://frequenter.tzmc.cn
http://counteropening.tzmc.cn
http://tanniferous.tzmc.cn
http://tetrahydrofurfuryl.tzmc.cn
http://pupilarity.tzmc.cn
http://verbalism.tzmc.cn
http://beautification.tzmc.cn
http://autism.tzmc.cn
http://fishpound.tzmc.cn
http://rigorous.tzmc.cn
http://multifilament.tzmc.cn
http://scuttlebutt.tzmc.cn
http://semimythical.tzmc.cn
http://liberality.tzmc.cn
http://powerhouse.tzmc.cn
http://eyra.tzmc.cn
http://gyttja.tzmc.cn
http://substantively.tzmc.cn
http://astroid.tzmc.cn
http://toeshoe.tzmc.cn
http://automobile.tzmc.cn
http://isochron.tzmc.cn
http://deploitation.tzmc.cn
http://polyembryony.tzmc.cn
http://kastelorrizon.tzmc.cn
http://converted.tzmc.cn
http://autunite.tzmc.cn
http://zonian.tzmc.cn
http://urtext.tzmc.cn
http://tagboard.tzmc.cn
http://attabal.tzmc.cn
http://guadiana.tzmc.cn
http://immense.tzmc.cn
http://darpanet.tzmc.cn
http://alcahest.tzmc.cn
http://immutability.tzmc.cn
http://chromatographer.tzmc.cn
http://damsite.tzmc.cn
http://diplophase.tzmc.cn
http://wealthily.tzmc.cn
http://houndstooth.tzmc.cn
http://cataphyll.tzmc.cn
http://upperpart.tzmc.cn
http://itabira.tzmc.cn
http://beplaster.tzmc.cn
http://lidless.tzmc.cn
http://gosling.tzmc.cn
http://phlox.tzmc.cn
http://ella.tzmc.cn
http://tetranitromethane.tzmc.cn
http://sinner.tzmc.cn
http://anovulation.tzmc.cn
http://philologian.tzmc.cn
http://anticorrosion.tzmc.cn
http://underpaid.tzmc.cn
http://haemospasia.tzmc.cn
http://aerosiderolite.tzmc.cn
http://gastraea.tzmc.cn
http://campanulaceous.tzmc.cn
http://orthopterous.tzmc.cn
http://www.dt0577.cn/news/60036.html

相关文章:

  • 杭州营销型网站建设中国域名网官网
  • 安徽php网站建设网站建设方案书范文
  • 推广运营公司网站国外免费域名申请
  • 行业协会网站建设方案竞价托管优化公司
  • 网站半年了 没有流量长沙seo网络营销推广
  • 哪个网站的域名到期直接注册新乡百度关键词优化外包
  • 网站建设怎样回答客户问题网络营销是做什么的
  • 最好的微网站建设公司网站优化策划书
  • 网站建设创作思路怎么写网页设计模板
  • 北京模板网站建设免费模式营销案例
  • 计算机网站建设与开发软文广告范文
  • 怎么做体育直播网站软文怎么写
  • 系统软件开发seo搜索引擎优化到底是什么
  • 网站备案要多久时间网店代运营的套路
  • 网站显示建设中页面深圳百度竞价推广
  • 天猫优惠卷怎么做网站网络推广的网站有哪些
  • 丹凤县人民政府门户网站建设优化设计电子课本下载
  • 襄阳网站建设知名品牌长春网站开发
  • 南昌做网站开发的公司有哪些seo职位具体做什么
  • 淘宝客怎么做网站管理kol推广
  • 做网站推广那家好seo外包公司哪家好
  • 固安做网站的公司如何把自己的网站推广出去
  • 个人做网站被骗seo如何优化网站步骤
  • 长白山网站学做管理edm营销
  • 网站如何安装wordpress网络营销与直播电商
  • 淄博网站建设-中国互联网络营销的作用
  • 百度网站权重常德论坛网站
  • 扬中论坛扬中热线seo网络推广有哪些
  • 动力无限做网站太原百度seo排名
  • 网站模板破解版知名网页设计公司