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

做app网站的软件叫什么鞍山网站制作云端

做app网站的软件叫什么,鞍山网站制作云端,塔城北京网站建设,附近的灯箱广告制作From: https://www.jianshu.com/p/54b0f4016300 一. fixture介绍 fixture是pytest的一个闪光点#xff0c;pytest要精通怎么能不学习fixture呢#xff1f;跟着我一起深入学习fixture吧。其实unittest和nose都支持fixture#xff0c;但是pytest做得更炫。 fixture是pytest特有…From: https://www.jianshu.com/p/54b0f4016300 一. fixture介绍 fixture是pytest的一个闪光点pytest要精通怎么能不学习fixture呢跟着我一起深入学习fixture吧。其实unittest和nose都支持fixture但是pytest做得更炫。 fixture是pytest特有的功能它用pytest.fixture标识定义在函数前面。在你编写测试函数的时候你可以将此函数名称做为传入参数pytest将会以依赖注入方式将该函数的返回值作为测试函数的传入参数。 fixture有明确的名字在其他函数模块类或整个工程调用它时会被激活。 fixture是基于模块来执行的每个fixture的名字就可以触发一个fixture的函数它自身也可以调用其他的fixture。 我们可以把fixture看做是资源在你的测试用例执行之前需要去配置这些资源执行完后需要去释放资源。比如module类型的fixture适合于那些许多测试用例都只需要执行一次的操作。 fixture还提供了参数化功能根据配置和不同组件来选择不同的参数。 fixture主要的目的是为了提供一种可靠和可重复性的手段去运行那些最基本的测试内容。比如在测试网站的功能时每个测试用例都要登录和退出利用fixture就可以只做一次否则每个测试用例都要做这两步也是冗余。 下面会反复提高Python的Module概念Python中的一个Module对应的就是一个.py文件。其中定义的所有函数或者是变量都属于这个Module。这个Module 对于所有函数而言就相当于一个全局的命名空间而每个函数又都有自己局部的命名空间。 二. Fixture基础实例入门 把一个函数定义为Fixture很简单只能在函数声明之前加上“pytest.fixture”。其他函数要来调用这个Fixture只用把它当做一个输入的参数即可。 test_fixture_basic.py import pytestpytest.fixture() def before(): print \nbefore each test def test_1(before): print test_1() def test_2(before): print test_2() assert 0 下面是运行结果test_1和test_2运行之前都调用了before也就是before执行了两次。默认情况下fixture是每个测试用例如果调用了该fixture就会执行一次的。 C:\Users\yatyang\PycharmProjects\pytest_examplepytest -v -s test_fixture_basic.pytest session starts platform win32 -- Python 2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0 -- C:\Python27\python.exe cachedir: .cache metadata: {Python: 2.7.13, Platform: Windows-7-6.1.7601-SP1, Packages: {py: 1.4.32, pytest: 3.0.6, pluggy: 0.4.0}, JAVA_HOME: C:\\Program Files (x86)\\Java\\jd k1.7.0_01, Plugins: {html: 1.14.2, metadata: 1.3.0}} rootdir: C:\Users\yatyang\PycharmProjects\pytest_example, inifile: plugins: metadata-1.3.0, html-1.14.2 collected 2 items test_fixture_basic.py::test_1 before each test test_1() PASSED test_fixture_basic.py::test_2 before each test test_2() FAILED FAILURES ___________________________________ test_2 ____________________________________ before None def test_2(before): print test_2() assert 0 E assert 0 test_fixture_basic.py:12: AssertionError 1 failed, 1 passed in 0.23 seconds 如果你的程序出现了下面的错误,就是开始忘记添加‘import pytest所以不要忘记罗。 ERRORS _________________ ERROR collecting test_fixture_decorator.py __________________ test_fixture_decorator.py:2: in module pytest.fixture() E NameError: name pytest is not defined !!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!! 1 error in 0.36 seconds 三. 调用fixture的三种方式 1. 在测试用例中直接调用它例如第二部分的基础实例。 2. 用fixture decorator调用fixture 可以用以下三种不同的方式来写我只变化了函数名字和类名字内容没有变。第一种是每个函数前声明第二种是封装在类里类里的每个成员函数声明第三种是封装在类里在前声明。在可以看到3中不同方式的运行结果都是一样。 test_fixture_decorator.py import pytestpytest.fixture() def before(): print(\nbefore each test) pytest.mark.usefixtures(before) def test_1(): print(test_1()) pytest.mark.usefixtures(before) def test_2(): print(test_2()) class Test1: pytest.mark.usefixtures(before) def test_3(self): print(test_1()) pytest.mark.usefixtures(before) def test_4(self): print(test_2()) pytest.mark.usefixtures(before) class Test2: def test_5(self): print(test_1()) def test_6(self): print(test_2()) 运行结果如下和上面的基础实例的运行效果一样。 C:\Users\yatyang\PycharmProjects\pytest_examplepytest -v -s test_fixture_decorator.pytest session starts platform win32 -- Python 2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0 -- C:\Python27\python.exe cachedir: .cache metadata: {Python: 2.7.13, Platform: Windows-7-6.1.7601-SP1, Packages: {py: 1.4.32, pytest: 3.0.6, pluggy: 0.4.0}, JAVA_HOME: C:\\Program Files (x86)\\Java\\jd k1.7.0_01, Plugins: {html: 1.14.2, metadata: 1.3.0}} rootdir: C:\Users\yatyang\PycharmProjects\pytest_example, inifile: plugins: metadata-1.3.0, html-1.14.2 collected 6 items test_fixture_decorator.py::test_1 before each test test_1() PASSED test_fixture_decorator.py::test_2 before each test test_2() PASSED test_fixture_decorator.py::Test1::test_3 before each test test_1() PASSED test_fixture_decorator.py::Test1::test_4 before each test test_2() PASSED test_fixture_decorator.py::Test2::test_5 before each test test_1() PASSED test_fixture_decorator.py::Test2::test_6 before each test test_2() PASSED 6 passed in 0.10 seconds 3. 用autos调用fixture fixture decorator一个optional的参数是autouse, 默认设置为False。 当默认为False就可以选择用上面两种方式来试用fixture。 当设置为True时在一个session内的所有的test都会自动调用这个fixture。 权限大责任也大所以用该功能时也要谨慎小心。 import time import pytestpytest.fixture(scopemodule, autouseTrue) def mod_header(request): print(\n-----------------) print(module : %s % request.module.__name__) print(-----------------) pytest.fixture(scopefunction, autouseTrue) def func_header(request): print(\n-----------------) print(function : %s % request.function.__name__) print(time : %s % time.asctime()) print(-----------------) def test_one(): print(in test_one()) def test_two(): print(in test_two()) 从下面的运行结果可以看到mod_header在该module内运行了一次而func_header对于每个test都运行了一次总共两次。该方式如果用得好还是可以使代码更为简洁。 但是对于不熟悉自己组的测试框架的人来说在pytest里面去新写测试用例需要去了解是否已有一些fixture是module或者class级别的需要注意。 C:\Users\yatyang\PycharmProjects\pytest_examplepytest -v -s test_fixture_auto.pytest session starts platform win32 -- Python 2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0 -- C:\Python27\python.exe cachedir: .cache metadata: {Python: 2.7.13, Platform: Windows-7-6.1.7601-SP1, Packages: {py: 1.4.32, pytest: 3.0.6, pluggy: 0.4.0}, JAVA_HOME: C:\\Program Files (x86)\\Java\\jd k1.7.0_01, Plugins: {html: 1.14.2, metadata: 1.3.0}} rootdir: C:\Users\yatyang\PycharmProjects\pytest_example, inifile: plugins: metadata-1.3.0, html-1.14.2 collected 2 items test_fixture_auto.py::test_one ----------------- module : test_fixture_auto ----------------- ----------------- function : test_one time : Sat Mar 18 06:56:54 2017 ----------------- in test_one() PASSED test_fixture_auto.py::test_two ----------------- function : test_two time : Sat Mar 18 06:56:54 2017 ----------------- in test_two() PASSED 2 passed in 0.03 seconds 四. fixture scope function每个test都运行默认是function的scope class每个class的所有test只运行一次 module每个module的所有test只运行一次 session每个session只运行一次 比如你的所有test都需要连接同一个数据库那可以设置为module只需要连接一次数据库对于module内的所有test这样可以极大的提高运行效率。 五. fixture 返回值 在上面的例子中fixture返回值都是默认None我们可以选择让fixture返回我们需要的东西。如果你的fixture需要配置一些数据读个文件或者连接一个数据库那么你可以让fixture返回这些数据或资源。 如何带参数 fixture还可以带参数可以把参数赋值给params默认是None。对于param里面的每个值fixture都会去调用执行一次就像执行for循环一样把params里的值遍历一次。 test_fixture_param.py import pytestpytest.fixture(params[1, 2, 3]) def test_data(request): return request.param def test_not_2(test_data): print(test_data: %s % test_data) assert test_data ! 2 可以看到test_not_2里面把用test_data里面定义的3个参数运行里三次。 C:\Users\yatyang\PycharmProjects\pytest_examplepytest -v -s test_fixture_param.pytest session starts platform win32 -- Python 2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0 -- C:\Python27\python.exe cachedir: .cache metadata: {Python: 2.7.13, Platform: Windows-7-6.1.7601-SP1, Packages: {py: 1.4.32, pytest: 3.0.6, pluggy: 0.4.0}, JAVA_HOME: C:\\Program Files (x86)\\Java\\jd k1.7.0_01, Plugins: {html: 1.14.2, metadata: 1.3.0}} rootdir: C:\Users\yatyang\PycharmProjects\pytest_example, inifile: plugins: metadata-1.3.0, html-1.14.2 collected 3 items test_fixture_param.py::test_not_2[1] test_data: 1 PASSED test_fixture_param.py::test_not_2[2] test_data: 2 FAILED test_fixture_param.py::test_not_2[3] test_data: 3 PASSED FAILURES ________________________________ test_not_2[2] ________________________________ test_data 2 def test_not_2(test_data): print(test_data: %s % test_data) assert test_data ! 2 E assert 2 ! 2 test_fixture_param.py:9: AssertionError 1 failed, 2 passed in 0.24 seconds 转载于:https://www.cnblogs.com/Raul2018/p/10340452.html
http://www.pierceye.com/news/578753/

相关文章:

  • 有些网站为什么可以做资讯微信小程序vr全景
  • 做网站的开发工具长春百度关键词优化
  • 网站建设所需人力网站制作的必备技巧有哪些
  • 上饶网站建设推广四川城乡建设网网站
  • 网站logo怎么改服装手机商城网站建设
  • aspnet网站开发实例视频天津网站建设普斯泰
  • 玉溪网站建设设计心理医院网站优化服务商
  • 支付宝网站接口申请建湖做网站哪家最好
  • 网站的超级链接怎么做无法运行电脑wordpress
  • 网站建设企业网银e路通西宁做网站_君博相约
  • 陕西网站建设公司哪有大连网站建设
  • 东莞做网站 汇卓百度网盘官网登录入口
  • 网站建设哪谷歌浏览器安卓版下载
  • 中国建设银行上海市分行网站天津工程建设信息网站
  • 怎么做好网站开发、设计中国站长网站
  • 沈阳网站建设tlmh室内设计装修案例
  • 网站 linux 服务器配置长沙企业网站建设价格
  • 低价网站制作企业智慧团建官网登录口手机版
  • 临沂网站制作专业如何 做网站
  • 旅游景区网站开发的政策可行性天翼云电脑免费领取
  • 企业网站建设报价做网站要实名认证吗
  • 设计网站得多少钱ui设计师个人简历
  • 彩票网站建设基本流程wordpress上篇下篇代码
  • 一站式服务的优点无锡网站搜索优化
  • 怎么做地区网站烟台网站建设首推企汇互联见效付款
  • 杭州网站优化服务网站内容方案
  • 大气手机网站模板免费下载军事网站模板下载
  • 大兴德艺网站建设wordpress上传的gif图不会动
  • 三门峡住房和建设局网站房产主题wordpress
  • 网站改版 权重php做网站脑图