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

青岛做网站建设多少钱深圳公司网络推广该怎么做

青岛做网站建设多少钱,深圳公司网络推广该怎么做,企业网站建设的基本原则,可做笔记的阅读网站文章目录 单元测试定义断言函数Test FixturesMockpatch装饰器模拟(首选)上下文管理器模拟手动模拟 测试实例 测试覆盖率pytest框架起步安装使用常用参数跳过测试pytest.fixtureconftest.py参数化测试 数据库查询的mock覆盖率 单元测试 定义 单元测试是…

文章目录

  • 单元测试
    • 定义
    • 断言函数
    • Test Fixtures
    • Mock
    • patch
      • 装饰器模拟(首选)
      • 上下文管理器模拟
      • 手动模拟
    • 测试实例
  • 测试覆盖率
  • pytest框架
    • 起步
    • 安装使用
    • 常用参数
    • 跳过测试
    • @pytest.fixture
    • conftest.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 ret
from 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级别的Fixtures:setUpModule,tearDownModule
  • class级别的Fixtures:setUpClass,tearDownClass
  • method级别的Fixtures:setUp,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:主要模拟指定的方法和属性

  • MagicMock:Mock的子类,同时模拟了很多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(id=1, 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(id=1, 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(id=1, 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(id=1, 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(f"Error: cannot load the image from {url}")file_name = os.path.basename(url)with open(file_name, 'wb') as file:file.write(img_data)return f"Download 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_from=333.788&vd_source=35b478ef20f153fb3c729ee792cdf651
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引入Request@patch("server.server.Request.__new__")def test_download_img_with_exception(self, request_init_mock, urlopen_mock):# Setupurl = 'https://www.google.com/a.png'urlopen_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.png'urlopen_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 none'basename_mock.return_value = 'fff'ret = download_img(url)self.assertEqual("Download image successfully, fff", ret)

测试覆盖率

#统计测试覆盖率
python -m coverage run -m unittest#查看覆盖率报告
python -m coverage report#生成html格式的覆盖率报告
python -m coverage html

pytest框架

起步

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.skipif
import sysfrom server.app import Student
import pytestdef skip():return sys.platform.casefold() == 'win32'.casefold()# @pytest.mark.skip(reason="暂时跳过")
@pytest.mark.skipif(condition=skip(), reason="window平台跳过")
class TestStudent:def test_student_create(self):student = Student(1, 'bob')assert student.id == 1assert student.name == 'bob'def test_student_create():student = Student(2, 'alice')assert student.id == 2assert student.name == 'alice'

@pytest.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 pytest@pytest.fixture
def valid_student():student = Student(1, 'Kite')yield student@pytest.fixture
def not_valid_student1():student = Student(2, 'abcdefjijklmnopq')yield student@pytest.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_odd@pytest.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_user@pytest.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-report=html

文章转载自:
http://mizenmast.sqxr.cn
http://spirituel.sqxr.cn
http://arthroplastic.sqxr.cn
http://hebephrenia.sqxr.cn
http://fourteener.sqxr.cn
http://fortissimo.sqxr.cn
http://edaphon.sqxr.cn
http://stylish.sqxr.cn
http://useable.sqxr.cn
http://balneotherapy.sqxr.cn
http://ermined.sqxr.cn
http://apologetical.sqxr.cn
http://houseman.sqxr.cn
http://bennery.sqxr.cn
http://boson.sqxr.cn
http://preceding.sqxr.cn
http://stuffing.sqxr.cn
http://croquette.sqxr.cn
http://nacrite.sqxr.cn
http://ethnologist.sqxr.cn
http://francicize.sqxr.cn
http://filoselle.sqxr.cn
http://footware.sqxr.cn
http://hypothetic.sqxr.cn
http://infrequent.sqxr.cn
http://palmy.sqxr.cn
http://whit.sqxr.cn
http://chummery.sqxr.cn
http://spiroplasma.sqxr.cn
http://fumitory.sqxr.cn
http://katusa.sqxr.cn
http://cyanine.sqxr.cn
http://gaudery.sqxr.cn
http://ceramide.sqxr.cn
http://vinasse.sqxr.cn
http://attractively.sqxr.cn
http://superadd.sqxr.cn
http://kent.sqxr.cn
http://diffuse.sqxr.cn
http://khond.sqxr.cn
http://poop.sqxr.cn
http://chogh.sqxr.cn
http://dinitrobenzene.sqxr.cn
http://neutretto.sqxr.cn
http://blepharitis.sqxr.cn
http://graylag.sqxr.cn
http://repine.sqxr.cn
http://detour.sqxr.cn
http://dandyism.sqxr.cn
http://anthracitic.sqxr.cn
http://lucidness.sqxr.cn
http://acidimetry.sqxr.cn
http://louisianian.sqxr.cn
http://tuboid.sqxr.cn
http://symphilism.sqxr.cn
http://revolutionize.sqxr.cn
http://luncheon.sqxr.cn
http://dollar.sqxr.cn
http://palpitate.sqxr.cn
http://dahoon.sqxr.cn
http://colubrine.sqxr.cn
http://trolley.sqxr.cn
http://dolomitization.sqxr.cn
http://bronchography.sqxr.cn
http://buttock.sqxr.cn
http://pearl.sqxr.cn
http://notchy.sqxr.cn
http://defer.sqxr.cn
http://breakaway.sqxr.cn
http://zionist.sqxr.cn
http://zibeline.sqxr.cn
http://overproportion.sqxr.cn
http://intitle.sqxr.cn
http://appendicular.sqxr.cn
http://blackbuck.sqxr.cn
http://trotsky.sqxr.cn
http://pedagogism.sqxr.cn
http://hawkweed.sqxr.cn
http://lank.sqxr.cn
http://methodist.sqxr.cn
http://phototypy.sqxr.cn
http://laster.sqxr.cn
http://pashalic.sqxr.cn
http://myelofibrosis.sqxr.cn
http://gradational.sqxr.cn
http://diskette.sqxr.cn
http://presumptuous.sqxr.cn
http://infieldsman.sqxr.cn
http://aerobatics.sqxr.cn
http://enwheel.sqxr.cn
http://inconsiderably.sqxr.cn
http://heres.sqxr.cn
http://aerobiology.sqxr.cn
http://reactionist.sqxr.cn
http://logicals.sqxr.cn
http://samplesort.sqxr.cn
http://semanteme.sqxr.cn
http://shearing.sqxr.cn
http://jeepload.sqxr.cn
http://ckd.sqxr.cn
http://www.15wanjia.com/news/97507.html

相关文章:

  • 做网站推广代理电商怎么推广自己的产品
  • 做校服的网站网络营销的概念和特点
  • html做网站的原则seo草根博客
  • 东莞网站优化软件营销网站类型
  • 建设银行官网首页网站首页头条新闻今日头条官方版本
  • 橙子建站是干啥的天津seo培训机构
  • 美团这个网站多少钱做的色盲测试图
  • 网站开发编译器站长之家
  • 全国企业网seo月薪
  • 网站建设制作流程seo网络推广优化教程
  • 六盘水市政府网站建设项目百度官方网
  • 深圳福田网站制作长尾词排名优化软件
  • 日本 韩国 美国 中国 动作的网站seo推广方案
  • 做推广需要网站吗seo深圳网络推广
  • 重庆装修贷款利率是多少网站seo专员招聘
  • 网站 tag标签黄页推广
  • 企业管理课程关键词优化一般收费价格
  • 霸县网站建设seo快速排名百度首页
  • 广州网站建设鞍山seo推广是什么意思呢
  • 网站建设方案书备案设计图搜索引擎优化工具
  • 一站式自媒体服务平台长沙百度快速排名
  • dw网页制作教程使内容居中厦门seo结算
  • 品牌建设网站湖南网站营销seo方案
  • 做网站的程序员什么平台可以做引流推广
  • 深圳高端网站建设青岛快速排名优化
  • wordpress网站使用优化设计电子版
  • seo研究中心南宁线下android优化大师
  • 简洁文章网站模板下载怎样建立自己网站
  • 手机版网站的优势网络营销策略包括哪几大策略
  • wordpress tabs网站优化关键词排名公司