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

官方网站建设报价表南京怎样做网站

官方网站建设报价表,南京怎样做网站,汽车门店管理系统,音平商城谁做的网站文章目录 单元测试定义断言函数Test FixturesMockpatch装饰器模拟#xff08;首选#xff09;上下文管理器模拟手动模拟 测试实例 测试覆盖率pytest框架起步安装使用常用参数跳过测试pytest.fixtureconftest.py参数化测试 数据库查询的mock覆盖率 单元测试 定义 单元测试是… 文章目录 单元测试定义断言函数Test FixturesMockpatch装饰器模拟首选上下文管理器模拟手动模拟 测试实例 测试覆盖率pytest框架起步安装使用常用参数跳过测试pytest.fixtureconftest.py参数化测试 数据库查询的mock覆盖率 单元测试 定义 单元测试是指一个自动化的测试 用来验证一小段代码的正确性可以快速执行在独立的环境中执行 断言函数 assertEqual assertNotEqual assertTrue assertFalse assertIs assertIsNot assertIsNone assertIsNotNone assertIn assertNotIn assertIsInstance assertNotIsInstance assertRaises示例一assertEqual class Calculator:def add(self, *args):ret 0for item in args:ret itemreturn retfrom unittest import TestCasefrom server.app import Calculatorclass TestCalculator(TestCase):def test_add(self):calculator Calculator()expect_result 10actual_result calculator.add(1, 2, 3, 4)self.assertEqual(expect_result, actual_result)示例二assertRaises class Service:def download_img(self, url: str):if url:return Trueraise ValueError(url error)from unittest import TestCase from server.app import Serviceclass TestService(TestCase):def test_download_img_success(self):service Service()ret service.download_img(http://www.baidu.com/1.png)self.assertTrue(ret)def test_download_img_with_exception(self):service Service()with self.assertRaises(ValueError):service.download_img()Test Fixtures 在测试方法执行之前或者之后执行的函数或者方法被称为Test Fixtures module级别的FixturessetUpModule,tearDownModuleclass级别的FixturessetUpClass,tearDownClassmethod级别的FixturessetUp,tearDown class Service:def download_img(self, url: str):if url:return Trueraise ValueError(url error) from unittest import TestCase from server.app import Servicedef setUpModule():print(执行module前...)def tearDownModule():print(执行module后...)class TestService(TestCase):classmethoddef setUpClass(cls):print(执行class前...)classmethoddef tearDownClass(cls):print(执行class后...)def setUp(self):self.service Service()print(执行任意测试方法前...)def tearDown(self):print(执行任意测试方法后...)def test_download_img_success(self):ret self.service.download_img(http://www.baidu.com/1.png)self.assertTrue(ret)def test_download_img_with_exception(self):with self.assertRaises(ValueError):self.service.download_img() 执行module前... 执行class前... 执行任意测试方法前... 执行任意测试方法后... 执行任意测试方法前... 执行任意测试方法后... 执行class后... 执行module后...Mock 模拟函数方法类的行为。 Mock主要模拟指定的方法和属性 MagicMockMock的子类同时模拟了很多Magic方法__len____str__方法等 示例一 from unittest.mock import Mockdef test_hello():hello Mock()hello.find_user.return_value {name: 旺财,age: 1}print(hello.find_user())if __name__ __main__:test_hello() {name: 旺财, age: 1}示例二 class Student:def __init__(self, id: int, name: str):self.id idself.name namedef find_name_by_id(id):passdef save_student(student):passdef chang_name(id: int, new_name: str):student find_name_by_id(id)if student:student.name new_namesave_student(student) from unittest.mock import Mock from unittest import TestCase from server.app import chang_name from server import appclass TestService(TestCase):def test_change_name_with_record(self):service.find_name_by_id Mock()student Mock(id1, name旧名字)service.find_name_by_id.return_value studentservice.save_student Mock()chang_name(1, 新名字)self.assertEqual(新名字, student.name)service.find_name_by_id.assert_called()service.save_student.assert_called()def test_change_name_without_record(self):service.find_name_by_id Mock()service.find_name_by_id.return_value Noneservice.save_student Mock()chang_name(1, 新名字)# 断言没有被调用service.save_student.assert_not_called() patch path可以临时用Mock对象替换一个目标函数方法类。本质还是上一节的Mock操作。 path可以替换的目标 目标必须是可import的是在使用的目标的地方替换而不是替换定义 path的使用方式 装饰器的方式上下文管理器的方式手动方式 装饰器模拟首选 class Student:def __init__(self, id: int, name: str):self.id idself.name namedef find_name_by_id(id):passdef save_student(student):passdef chang_name(id: int, new_name: str):student find_name_by_id(id)if student:student.name new_namesave_student(student) from unittest.mock import Mock, patch from unittest import TestCase from server.app import chang_nameclass TestService(TestCase):patch(server.server.save_student)patch(server.server.find_name_by_id)def test_change_name_decorator(self, find_name_by_id_mock, save_student_mock):student Mock(id1, name旧名字)find_name_by_id_mock.return_value studentchang_name(1, 新名字)self.assertEqual(新名字, student.name)find_name_by_id_mock.assert_called()save_student_mock.assert_called() 上下文管理器模拟 from unittest.mock import Mock, patch from unittest import TestCase from server.app import chang_nameclass TestService(TestCase):def test_change_name_context(self):student Mock(id1, name旧名字)with patch(server.server.find_name_by_id) as find_name_by_id_mock, patch(server.server.save_student):find_name_by_id_mock.return_value studentchang_name(1, 新名字)self.assertEqual(新名字, student.name) 手动模拟 from unittest.mock import Mock, patch from unittest import TestCase from server.app import chang_nameclass TestService(TestCase):patch(server.server.find_name_by_id)def test_change_name_manual(self, find_name_by_id_mock):student Mock(id1, name旧名字)find_name_by_id_mock.return_value studentpather patch(server.server.save_student)pather.start()chang_name(1, 新名字)pather.start()self.assertEqual(新名字, student.name) 测试实例 path里面的模拟对象已经对所有魔术方法都进行了mock如果不关心返回值可以不用后续return_value了 import os.path from urllib.request import urlopen, Requestdef download_img(url: str):site_url Request(url, headers{User-Agent: Mozilla/5.0})with urlopen(site_url) as web_file:img_data web_file.read()if not img_data:raise Exception(fError: cannot load the image from {url})file_name os.path.basename(url)with open(file_name, wb) as file:file.write(img_data)return fDownload image successfully, {file_name} from unittest.mock import patch, MagicMock from unittest import TestCase from server.app import download_img# https://www.bilibili.com/video/BV1EK411B7LX/?spm_id_from333.788vd_source35b478ef20f153fb3c729ee792cdf651 class TestService(TestCase):# urlopen在方法参数中被mock为urlopen_mock# urlopen_mock的返回值是一个urlopen_result_mock# urlopen_result_mock的__enter__方法返回值是一个web_file_mock# web_file_mock的read方法返回值需要定义patch(server.server.urlopen)# 因为在service.service文件中引入了所以可以直接使用service.service引入Requestpatch(server.server.Request.__new__)def test_download_img_with_exception(self, request_init_mock, urlopen_mock):# Setupurl https://www.google.com/a.pngurlopen_result_mock MagicMock()web_file_mock MagicMock()urlopen_mock.return_value urlopen_result_mockurlopen_result_mock.__enter__.return_value web_file_mockweb_file_mock.read.return_value Nonewith self.assertRaises(Exception):download_img(url)patch(builtins.open)patch(os.path.basename)patch(server.server.urlopen)patch(server.server.Request.__new__)def test_download_img_with_success(self, request_init_mock, urlopen_mock, basename_mock, open_mock):# Setupurl https://www.google.com/a.pngurlopen_result_mock MagicMock()web_file_mock MagicMock()urlopen_mock.return_value urlopen_result_mockurlopen_result_mock.__enter__.return_value web_file_mockweb_file_mock.read.return_value not nonebasename_mock.return_value fffret download_img(url)self.assertEqual(Download image successfully, fff, ret) 测试覆盖率 #统计测试覆盖率 python -m coverage run -m unittest#查看覆盖率报告 python -m coverage report#生成html格式的覆盖率报告 python -m coverage htmlpytest框架 起步 pytest是一个基于python语言的第三方测试框架。 有以下优点 语法简单自动检测测试代码跳过指定测试开源 安装使用 #安装 pip install pytest#运行(自动查找test_*.py,*_test.py测试文件。自动查找测试文件中test_开头的函数和Test开头的类中的test_开头的方法) pytest pytest -v#测试指定测试类 pytest test_xxx.py常用参数 -v 输出详细的执行信息比如文件和用例名称 -s 输出调试信息比如print的打印信息 -x 遇到错误用例立即停止跳过测试 pytest.mark.skip pytest.mark.skipifimport sysfrom server.app import Student import pytestdef skip():return sys.platform.casefold() win32.casefold()# pytest.mark.skip(reason暂时跳过) pytest.mark.skipif(conditionskip(), reasonwindow平台跳过) class TestStudent:def test_student_create(self):student Student(1, bob)assert student.id 1assert student.name bobdef test_student_create():student Student(2, alice)assert student.id 2assert student.name alicepytest.fixture class Student():def __init__(self, id: int, name: str):self.id idself.name namedef valid_name(self):if self.name:return 3 len(self.name) 10return False from server.app import Student import pytestpytest.fixture def valid_student():student Student(1, Kite)yield studentpytest.fixture def not_valid_student1():student Student(2, abcdefjijklmnopq)yield studentpytest.fixture def not_valid_student2(not_valid_student1):# 这里不能对valid_student的name进行赋值修改哟student Student(3, Bob)student.name not_valid_student1.nameyield studentdef test_student(valid_student, not_valid_student1, not_valid_student2):ret valid_student.valid_name()assert retret not_valid_student1.valid_name()assert not retret not_valid_student2.valid_name()assert not ret conftest.py 作用使得fixture可以被多个文件中的测试用例复用。 在tests目录下建立conftest.py文件这里引入其他文件中的fixture那么其他用例中就可以使用这些fixture你也可以定义fixture在这个文件中但是不推荐哈 参数化测试 # 判断是否是奇数 def is_odd(x: int):return x % 2 ! 0 import pytestfrom server.app import is_oddpytest.mark.parametrize(num,expect_ret, [(1, True), (2, False)]) def test_is_odd(num, expect_ret):actual_ret is_odd(num)assert expect_ret actual_ret 数据库查询的mock import pytest from unittest.mock import patch, MagicMock from server.controller.message_controller import create_userpytest.fixture def mock_session_scope():with patch(server.db.session.session_scope) as mock_session_scope:mock_session_scope_return_value MagicMock()mock_session_scope.return_value mock_session_scope_return_valuesession_mock MagicMock()mock_session_scope_return_value.__enter__.return_value session_mockyield session_mockdef test_create_user(mock_session_scope):ret create_user(alice)assert ok retdef test_create_user_exception(mock_session_scope):with pytest.raises(ValueError):create_user() 覆盖率 pip install pytest pip install pytest-cov pytest --cov --cov-reporthtml
http://www.hkea.cn/news/14558554/

相关文章:

  • 网站绑定别名好吗wordpress 显示页面
  • 大连建设网站哪家好逆冬seo
  • 电子商务网站设计与网络营销实验国内空间没备案可以打开网站吗
  • 领航 网站设计网站平台建设需要多少钱
  • 奥远网站建设流程济阳做网站多少钱
  • 云加速应用于html网站福州企业建站系统
  • 大型网站建设兴田德润实惠图片网站 代码
  • aspx网站开发2022互联网+创新创业项目
  • 大气企业网站欣赏网站建设方案书是什么
  • 丹阳网站建设开发大庆免费网站建设公司
  • 响应式网站开发周期什么网站可以做数据调查
  • 新乡做网站的公司有那些网站优化公司大家好
  • 网站建设视频教程最新网站开发的经验技巧
  • 网站建设技巧网站设计的风格有哪些
  • 51的网站是啥建立网站导航栏的方法
  • 单纯做网站的公司做盗链网站
  • 苏州优秀网站设计公司纯静态网站 后台
  • 土特产网站建设事业计划书网站自助授权系统
  • 专业的o2o网站建设网站怎么做自己站长
  • 自己做壁纸的网站微信小程序商城需要多少钱
  • 在国内做推广产品用什么网站好什么网站可以做论坛app
  • 科技类公司网站怎么设计建一个公司网站费用
  • 顺义专业建站公司wordpress 多demo
  • 淘宝app网站建设wordpress怎么使用插件
  • 手机怎么做弹幕小视频网站设计免费
  • 海口网站建设中心设计工作室宣传文案
  • 用focusky做h5微网站湖南微信网站建设
  • 可以做宣传的网站有哪些西地那非片吃了多久会硬起来
  • 网站服务器异常是什么意思wordpress网站后台
  • 专业旅游网站建设什么网上平台可以找客源