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

杭州网站忧化网站应用市场设计

杭州网站忧化,网站应用市场设计,成都模板网站建设服务,金融软件网站建设公司Pytest简单介绍 #xff08;pytest是python的一个测试框架#xff0c;主要是用来进行一些小的测试#xff09; 安装#xff1a;pip install -U pytest查看是否安装成功#xff1a;pytest --version运行#xff1a;在当前文件所在目录下执行pytest#xff0c;会寻找当前目…Pytest简单介绍 pytest是python的一个测试框架主要是用来进行一些小的测试 安装pip install -U pytest查看是否安装成功pytest --version运行在当前文件所在目录下执行pytest会寻找当前目录以及子目录下以test开头的py文件或者以test结尾的py文件找到文件后在文件中找到以test开头函数并执行。或者执行pytest 文件名--这样可以指定某个文件进行pytest的测试  或者  python -m pytest xxx.py在执行pytest xxx.py时若增加参数pytest -v -s xxx.py   -v显示运行的函数-s显示内部的打印信息 编写pytest测试样例的规则测试文件以test_开头测试类以Test开头并且不能带有init方法测试函数以test_开头断言使用基本的assert即可断言--正常 assert value 0 断言--异常pytest.raise方法 1 #断言1除以0将产生一个ZeroDivisionError类型的异常。2 import pytest 3 def test_zero_division(): 4 with pytest.raises(ZeroDivisionError): 5 1 / 0 6 7 #有的时候我们可能需要在测试中用到产生的异常中的某些信息比如异常的类型type异常的值value等等。下面我们修改下上面的测试8 import pytest 9 def test_recursion_depth(): 10 with pytest.raises(ZeroDivisionError) as excinfo: 11 1/0 12 assert excinfo.type RuntimeError 13 #因为该测试断言产生的异常类型是RuntimeError而实际上产生的异常类型是ZeroDivisionError所以测试失败了。在测试结果中可以看到assert子表达式excinfo.type的值。 在test前可以通过增加skip或xfail进行跳过失败测试案例 1 import pytest 2 3 # pytest.mark.skipif() 跳过 4 # pytest.mark.skip() 跳过 5 # pytest.mark.xfail() 若出错跳过pass也跳过但会显示pass 6 def test_123(): 7 # pytest.skip() 也可以实现跳过目的 8 assert 1 0 简单例子 1 # coding:utf-8 2 import pytest 3 4 def fun(x): 5 return x 1 6 7 def test_fun(): 8 assert fun(2) 3 结果 (venvX) F:\test_jiaxinpytest test_para1.pytest session starts platform win32 -- Python 2.7.9, pytest-3.7.1, py-1.5.3, pluggy-0.7.1 rootdir: F:\test_jiaxin, inifile: plugins: allure-adaptor-1.7.10 collected 1 item test_para1.py . [100%] 1 passed in 0.09 seconds pytest之feature-scope function每个test都运行默认是function的scope. class每个class的所有test只运行一次. module每个module的所有test只运行一次. session每个session只运行一次.比如你的所有test都需要连接同一个数据库那可以设置为module只需要连接一次数据库对于module内的所有test这样可以极大的提高运行效率。 代码 1 pytest.fixture(scopemodule) 2 def hero_backup_policy(self, acmp_cfg): 3 return AcloudBackupPolicy(acmp_cfg) 4 5 pytest.fixture(scopefunction) 6 def hero_acloud_backup_policy(self, acmp_cfg): 7 return Server(acmp_cfg) pytest的参数化方式 pytest.fixture()方式进行参数化fixture装饰的函数可以作为参数传入其他函数 conftest.py 文件中存放参数化函数可作用于模块内的所有测试用例 pytest.mark.parametrize()方式进行参数化   待测试代码片段is_leap_year.py 1 # coding:utf-82 def is_leap_year(year):3 # 先判断year是不是整型4 if isinstance(year, int) is not True:5 raise TypeError(传入的参数不是整数)6 elif year 0:7 raise ValueError(公元元年是从公元一年开始)8 elif abs(year) ! year:9 raise ValueError(传入的参数不是正整数) 10 elif (year % 4 0 and year % 100 ! 0) or year % 400 0: 11 print%d年是闰年 % year 12 return True 13 else: 14 print%d年不是闰年 % year 15 return False pytest.fixture() pytest.fixture()中传入的参数为list用例执行时遍历list中的值每传入一次值则相当于执行一次用例。 pytest.fixture()装饰的函数中传入了一个参数为request。 这里的测试数据是直接存在list中的能否存入json文件或者xml文件再进行读取转换为list呢? 代码 1 # coding:utf-82 import is_leap_year3 import pytest4 5 class TestPara():6 is_leap [4, 40, 400, 800, 1996, 2996]7 is_not_leap [1, 100, 500, 1000, 1999, 3000]8 is_valueerror [0, -4, -100, -400, -1996, -2000]9 is_typeerror [-4, 4, 100, ins, **, 中文] 10 11 pytest.fixture(paramsis_leap) 12 def is_leap(self, request): 13 return request.param 14 15 pytest.fixture(paramsis_typeerror) 16 def is_typeerror(self, request): 17 return request.param 18 19 def test_is_leap(self, is_leap): 20 assert is_leap_year.is_leap_year(is_leap) True 21 22 def test_is_typeerror(self, is_typeerror): 23 with pytest.raises(TypeError): 24 is_leap_year.is_leap_year(is_typeerror) conftest.py 文件 -- 测试数据与用例分离 采用conftest.py文件存储参数化数据和函数模块下的用例执行时会自动读取conftest.py文件中的数据。代码 conftest.py文件 1 # coding:utf-82 import pytest3 4 is_leap [4, 40, 400, 800, 1996, 2996]5 is_not_leap [1, 100, 500, 1000, 1999, 3000]6 is_valueerror [0, -4, -100, -400, -1996, -2000]7 is_typeerror [-4, 4, 100, ins, **, 中文]8 9 pytest.fixture(paramsis_leap) 10 def is_leap(request): 11 return request.param 12 13 pytest.fixture(paramsis_typeerror) 14 def is_typeerror(request): 15 return request.param test_para.py文件 1 # coding:utf-82 3 import is_leap_year4 import pytest5 6 class TestPara():7 def test_is_leap(self, is_leap):8 assert is_leap_year.is_leap_year(is_leap) True9 10 def test_is_typeerror(self, is_typeerror): 11 with pytest.raises(TypeError): 12 is_leap_year.is_leap_year(is_typeerror) pytest.mark.parametrize() -- 进行参数化 采用标记函数参数化传入单个参数pytest.mark.parametrize(参数名lists。采用标记函数传入多个参数如pytest.mark.parametrize(para1, para2, [(p1_data_0, p2_data_0), (p1_data_1, p2_data_1),...]。这里测试用例中传入2个参数year和期望结果使输入数据与预期结果对应构造了2组会失败的数据在执行结果中可以看到失败原因。代码 1 # coding:utf-82 import is_leap_year3 import pytest4 5 class TestPara():6 # 参数传入year中7 pytest.mark.parametrize(year, expected, [(1, False), (4,True), (100, False), (400, True), (500, True)])8 def test_is_leap(self, year, expected):9 assert is_leap_year.is_leap_year(year) expected 10 11 pytest.mark.parametrize(year, expected, [(0, ValueError),(-4, TypeError), (-4, ValueError), (ss, TypeError), (100.0, ValueError)]) 12 def test_is_typeerror(self, year, expected): 13 if expected ValueError: 14 with pytest.raises(ValueError) as excinfo: 15 is_leap_year.is_leap_year(year) 16 assert excinfo.type expected 17 else: 18 with pytest.raises(TypeError) as excinfo: 19 is_leap_year.is_leap_year(year) 20 assert excinfo.type expected 参考链接https://blog.csdn.net/zhusongziye/article/details/79902772   pytest-fixture扩展内容 1.  把一个函数定义为Fixture很简单只能在函数声明之前加上“pytest.fixture”。其他函数要来调用这个Fixture只用把它当做一个输入的参数即可。 1 import pytest2 3 pytest.fixture()4 def before():5 print \nbefore each test6 7 def test_1(before):8 print test_1()9 10 def test_2(before): 11 print test_2() result (venvX) F:\test_jiaxinpytest -v -s test_compute.pytest session starts platform win32 -- Python 2.7.9, pytest-3.7.1, py-1.5.3, pluggy-0.7.1 -- f:\projects\sandom\venvx\scripts\python.exe cachedir: .pytest_cache rootdir: F:\test_jiaxin, inifile: plugins: allure-adaptor-1.7.10 collected 2 items test_compute.py::test_1 before each test test_1() PASSED test_compute.py::test_2 before each test test_2() PASSED 2 passed in 0.17 seconds 2.  进行封装 1 import pytest2 3 pytest.fixture()4 def before():5 print \nbefore each test6 7 # 每个函数前声明8 pytest.mark.usefixtures(before)9 def test_1(): 10 print test_1() 11 pytest.mark.usefixtures(before) 12 def test_2(): 13 print test_2() 14 15 #封装在类里,类里的每个成员函数声明 16 class Test1: 17 pytest.mark.usefixtures(before) 18 def test_3(self): 19 print test_1() 20 pytest.mark.usefixtures(before) 21 def test_4(self): 22 print test_2() 23 24 #封装在类里在前声明 25 pytest.mark.usefixtures(before) 26 class Test2: 27 def test_5(self): 28 print test_1() 29 def test_6(self): 30 print test_2() result (venvX) F:\test_jiaxinpytest -v -s test_compute.pytest session starts platform win32 -- Python 2.7.9, pytest-3.7.1, py-1.5.3, pluggy-0.7.1 -- f:\projects\sandom\venvx\scripts\python.exe cachedir: .pytest_cache rootdir: F:\test_jiaxin, inifile: plugins: allure-adaptor-1.7.10 collected 6 items test_compute.py::test_1 before each test test_1() PASSED test_compute.py::test_2 before each test test_2() PASSED test_compute.py::Test1::test_3 before each test test_1() PASSED test_compute.py::Test1::test_4 before each test test_2() PASSED test_compute.py::Test2::test_5 before each test test_1() PASSED test_compute.py::Test2::test_6 before each test test_2() PASSED 6 passed in 0.11 seconds 3.  fixture还可以带参数可以把参数赋值给params默认是None。对于param里面的每个值fixture都会去调用执行一次就像执行for循环一样把params里的值遍历一次。 1 import pytest 2 3 pytest.fixture(params[1, 2, 3]) 4 def test_data(request): 5 return request.param 6 7 def test_not_2(test_data): 8 print(test_data: %s % test_data) 9 assert test_data ! 2 result (venvX) F:\test_jiaxinpytest -v -s test_compute.pytest session starts platform win32 -- Python 2.7.9, pytest-3.7.1, py-1.5.3, pluggy-0.7.1 -- f:\projects\sandom\venvx\scripts\python.exe cachedir: .pytest_cache rootdir: F:\test_jiaxin, inifile: plugins: allure-adaptor-1.7.10 collected 3 items test_compute.py::test_not_2[1] test_data: 1 PASSED test_compute.py::test_not_2[2] test_data: 2 FAILED test_compute.py::test_not_2[3] test_data: 3 PASSED FAILURES ________________________________ test_not_2[2] ________________________________test_data 2def test_not_2(test_data):print(test_data: %s % test_data)assert test_data ! 2 E assert 2 ! 2test_compute.py:64: AssertionError1 failed, 2 passed in 0.12 seconds 转载于:https://www.cnblogs.com/sunshine-blog/p/9468399.html
http://www.zqtcl.cn/news/697727/

相关文章:

  • 鹤岗市建设局网站可信网站认证有用吗
  • 网站注册的账号怎么注销如何百度推广
  • 用wordpress制作网站模板阿里云网站建设合作
  • 金华建设公司网站宝武马钢集团公司招聘网站
  • 万州网站制作公司阳江市网站建设
  • 下载建设网站软件投资公司注册资金多少
  • 如何创建一个论坛网站免费域名解析平台
  • 国外经典手机网站设计单位做网站有哪些
  • 网站备案 优帮云百度提交入口网址截图
  • 广州五羊建设官方网站富阳区住房和城乡建设局网站
  • 网站代理怎么做的wordpress有什么缺点
  • 哪些网站可以做免费外贸Wordpress首图自动切换
  • 建网站几个按钮公司黄页企业名录在哪里查
  • 网站建设类外文翻译游戏开科技软件免费
  • 黄山家居网站建设怎么样济南在线制作网站
  • 东莞电子产品网站建设营销型网站推广方式的论文
  • 如何寻找做网站的客户聚名网查询
  • 甘肃制作网站凡科快图官网登录入口在线
  • discuz网站建设教学视频教程哪些大型网站有做互联网金融
  • jquery动画特效网站物流网站前端模板下载
  • 上海集团网站建设网站都是用什么语言写的
  • 地铁公司招聘信息网站网站推广页面 英语
  • 廊坊做网站的企业哪家好做网站app价格多少钱
  • wap网站制作当阳网站建设电话
  • 服装电子商务网站建设3000字中装建设有限公司
  • 河南卓越建设工程有限公司网站怎么做垂直门户网站
  • 接单做网页的网站手机端app开发公司
  • 古田路9号设计网站在线制作图片拼图
  • 深圳网站开发ucreator售后服务 网站建设
  • 做网站的语北京比较好的it公司