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

网站排名优化和竞价龙岗网站建设

网站排名优化和竞价,龙岗网站建设,猪八戒网做网站被骗,响应式web设计实践文章目录 08.PyTest框架什么是PyTestPyTest的优点PyTest的测试环境PyTest常用参数跳过测试 09.PyTest fixture基础PyTest fixture定义和使用引用多个Fixture 10. conftest.pyconftest.py的用途 11. 参数化测试用例为什么需要参数化测试用例使用parameterizer插件实现使用pytest…

文章目录

    • 08.PyTest框架
      • 什么是PyTest
      • PyTest的优点
      • PyTest的测试环境
      • PyTest常用参数
      • 跳过测试
    • 09.PyTest fixture基础
      • PyTest fixture定义和使用
      • 引用多个Fixture
    • 10. conftest.py
      • conftest.py的用途
    • 11. 参数化测试用例
      • 为什么需要参数化测试用例
      • 使用parameterizer插件实现
      • 使用pytest实现

python单元测试学习笔记1: https://blog.csdn.net/qq_42761751/article/details/141144477?spm=1001.2014.3001.5501

python单元测试学习笔记2 :https://blog.csdn.net/qq_42761751/article/details/141202123?spm=1001.2014.3001.5501

python单元测试学习笔记3 : https://blog.csdn.net/qq_42761751/article/details/141233236?spm=1001.2014.3001.5501

08.PyTest框架

unittest是python内置的框架,pytest是第三方测试框架,在目前实际应用中比较流行

  1. 什么是PyTest
  2. PyTest的优点
  3. PyTest的测试环境
  4. PyTest的常用参数
  5. 跳过测试

什么是PyTest

PyTest是一个基于Python语言的第三方的测试框架

PyTest的优点

  1. 语法简单
  2. 自动检测测试代码
  3. 跳过指定测试
  4. 开源

PyTest的测试环境

安装PyTest

pip install pytest

运行PyTest

# -v 可以显示详细信息
pytest -vpytest -v test_xxx.py
  1. 自动查找test_*.py, *_test.py测试文件
  2. 自动查找测试文件中的test_开头的函数和Test开头的类中的test_开头的方法。如果不是test_开头的函数或者不是Test开头的类中的test_开头的方法是不会被执行的
  3. 之前unittest的代码不用改变,可以直接用pytest执行

代码示例:

class Student:def __init__(self, id, name) -> None:self.id = idself.name = name def test_student():student = Student(id=1, name="Jack")assert student.id ==1assert student.name == "Jack"class TestStudent:def test_student(self):student = Student(id=1, name="Jack")assert student.id ==1assert student.name == "Jack"

PyTest常用参数

-v : 输出详细的执行信息,比如文件及用例名称等
-s:输出调试信息,比如print的打印信息
-x:遇到错误用例立即停止

跳过测试

跳过测试的方式有两种

  1. @pytest.mark.skip 无论如何都跳过
  2. @pytest.mark.skipif
import sys
import pytest
def is_skip():# 判断是否为MacOS操作系统return sys.platform.casefold() == "darwin".casefold()# @pytest.mark.skip(reason="hahaha")  # 无论如何都跳过
@pytest.mark.skipif(condition=is_skip(), reason="skip on macos")  # 如果is_skip返回是True就跳过,is_skip不是True就不跳过
def test_student():student = Student(id=1, name="Jack")assert student.id ==1assert student.name == "Jack"

09.PyTest fixture基础

PyTest fixture定义和使用

使用@pytest.fixture定义

定义一个student.py

class Student:def __init__(self, id, name) -> None:self.id = idself.name = namedef rename(self, new_name: str) ->bool:if 3 < len(new_name) < 10:self.name = new_namereturn Truereturn False def vaild_student(self) ->bool:if self.name:return 3 < len(self.name) < 10

测试代码:

import pytest
from student import Studentclass TestStudent:@pytest.fixturedef vaild_student(self):"""使用yield很方便在测试用力之前做setup, 之后做cleanup"""student = Student(id=1, name = "Mary")# setup....yield student# cleanup...def test_rename_false(self, vaild_student):# setupnew_name = "dsadadddddddddasssssssssssss"excepted_result = False# Actionactural_result = vaild_student.rename(new_name)# assertassert actural_result == excepted_result

引用多个Fixture

  1. 一个unit test或fixture可以使用多个其他的fixture:
import pytest@pytest.fixture
def student():yield Student()@pytest.fixture
def course():yield Course()def test_regisiter_course(student, course):...
  1. 一个fixture可以被一个test引用多次
@pytest.fixture
def student():return Student()@pytest.fixture
def course():return Course()@pytest.fixture
def course_student(course, student):course.add_student(student)return coursedef test_regisiter_course(course_student, course):...

10. conftest.py

conftest.py的用途

使得fixture可以被多个文件中的测试用例复用

在项目目录下直接新建conftest.py:

import pytest@pytest.ficture
def male_student_fixture():student = Student(id =1, name = "Jack")

在测试代码中可直接使用,pytest会自动找到这个文件:

def test_vaild_gender_true(self, male_student_fixture):expected_result = Trueactural_result = male_student_fixture.vaild_gender()assert actual_result == expected_result

方式2:在conftest.py中引入fixture(建议使用这种方式)

# conftest.py
import pytest
from student_fixture import male_student_fixture

11. 参数化测试用例

为什么需要参数化测试用例

针对同一个被测试函数需要进行多组数据的测试,比如测试一个奇偶性判断的函数,我们一个可以n个数一起测试写到一个测试函数中,而不是每个数单独写一个测试函数

使用parameterizer插件实现

安装parameterizer

pip install parameterized

测试代码:(为了方便将代码与测试代码放在一个py文件下)

class Calculator:def add(self, *args):result = 0for n in args:result += nif n==2:result += 5return resultdef is_odd(self, num:int)->bool:return num%2 !=0import unittest
import parameterized
class TestCalculator(unittest.TestCase):@parameterized.expend([[1,True], [2, False]])def test_is_odd(self,num, excepted_out):# SetUpcal = Calculator()# Actionactural_result = cal.is_odd(num)# assertassert actural_result == excepted_out

使用pytest实现

import pytest
@pytest.mark.parametrize("num, excepted_result", [(1, True), (2,False)])
def test_is_odd(num, excepted_result):# setupcal = Calculator()# Actionresult = cal.is_odd(num)# assertassert result == excepted_result

本文参考:https://www.bilibili.com/video/BV1z64y177VF/?spm_id_from=pageDriver&vd_source=cf0b4c9c919d381324e8f3466e714d7a


文章转载自:
http://wec.wwxg.cn
http://wirepull.wwxg.cn
http://snakelike.wwxg.cn
http://dictator.wwxg.cn
http://regina.wwxg.cn
http://inquisitive.wwxg.cn
http://habenula.wwxg.cn
http://commiserative.wwxg.cn
http://fattest.wwxg.cn
http://beakiron.wwxg.cn
http://rarefied.wwxg.cn
http://unsay.wwxg.cn
http://photoreconnaissance.wwxg.cn
http://covariation.wwxg.cn
http://brushed.wwxg.cn
http://ineffectually.wwxg.cn
http://fermata.wwxg.cn
http://auditorial.wwxg.cn
http://movably.wwxg.cn
http://attentively.wwxg.cn
http://lyophiled.wwxg.cn
http://uhlan.wwxg.cn
http://impetuosity.wwxg.cn
http://carnification.wwxg.cn
http://bodmin.wwxg.cn
http://datamation.wwxg.cn
http://anorectic.wwxg.cn
http://invected.wwxg.cn
http://ultrafine.wwxg.cn
http://escritoire.wwxg.cn
http://dunedin.wwxg.cn
http://strictness.wwxg.cn
http://scolion.wwxg.cn
http://lottery.wwxg.cn
http://kneesy.wwxg.cn
http://logography.wwxg.cn
http://barodynamics.wwxg.cn
http://camp.wwxg.cn
http://eupneic.wwxg.cn
http://dardanian.wwxg.cn
http://virogene.wwxg.cn
http://kilocycle.wwxg.cn
http://weekend.wwxg.cn
http://luoyang.wwxg.cn
http://heating.wwxg.cn
http://keynote.wwxg.cn
http://yeld.wwxg.cn
http://octogenarian.wwxg.cn
http://rumble.wwxg.cn
http://desensitize.wwxg.cn
http://heller.wwxg.cn
http://glazier.wwxg.cn
http://sorbol.wwxg.cn
http://hesitance.wwxg.cn
http://haddie.wwxg.cn
http://inelegance.wwxg.cn
http://bracteate.wwxg.cn
http://riksdag.wwxg.cn
http://clockwork.wwxg.cn
http://moistly.wwxg.cn
http://gemmy.wwxg.cn
http://cantina.wwxg.cn
http://emulsify.wwxg.cn
http://anchorman.wwxg.cn
http://negate.wwxg.cn
http://fresher.wwxg.cn
http://precooler.wwxg.cn
http://foster.wwxg.cn
http://watchword.wwxg.cn
http://unprivileged.wwxg.cn
http://milliosmol.wwxg.cn
http://hydroponist.wwxg.cn
http://syria.wwxg.cn
http://catalepsis.wwxg.cn
http://ndis.wwxg.cn
http://ptilosis.wwxg.cn
http://slaveholding.wwxg.cn
http://convalesce.wwxg.cn
http://junius.wwxg.cn
http://arapunga.wwxg.cn
http://choctaw.wwxg.cn
http://disarrange.wwxg.cn
http://abscission.wwxg.cn
http://excremental.wwxg.cn
http://cuspy.wwxg.cn
http://vaccinization.wwxg.cn
http://caseate.wwxg.cn
http://dorsigrade.wwxg.cn
http://enthralment.wwxg.cn
http://eurocurrency.wwxg.cn
http://cariogenic.wwxg.cn
http://champ.wwxg.cn
http://mediaperson.wwxg.cn
http://bootie.wwxg.cn
http://jutland.wwxg.cn
http://technomania.wwxg.cn
http://blastula.wwxg.cn
http://overendowed.wwxg.cn
http://incapacitation.wwxg.cn
http://antipyic.wwxg.cn
http://www.hrbkazy.com/news/91045.html

相关文章:

  • 旅游网站开发目标新闻发布最新新闻
  • 江门建设建筑网站小程序开发公司哪里强
  • 乌鲁木齐网站建设百度网盘云资源搜索引擎
  • 免费网站建设哪家好网络推广工作内容
  • 做化工回收的 做那个网站百度一下你就知道官方网站
  • 安卓市场wordpress主题北京网络推广公司wyhseo
  • 网站东莞优化建设宁波seo链接优化
  • 网站怎样做https网络推广的基本渠道
  • 青岛网站优化多少钱网站权重排名
  • 贵阳网站建设建站系统扫描图片找原图
  • wordpress怎么变中文版网站seo关键词排名优化
  • 市住房和城乡建设委员会网站今天的特大新闻有哪些
  • 广州建设工程信息网站seo优化靠谱吗
  • 西安网站建设管理东莞网站seo公司
  • 网站先做前端还是后台成人就业技术培训机构
  • 韶关做网站的公司百度怎么免费推广自己的产品
  • 郑州富士康是干什么工作的山西seo基础教程
  • 开了360网站卫士ssl如何做301网络推广员工资多少钱
  • 全网推广服务semseo是什么意思
  • 苏州正规网站制作公司濮阳网站推广
  • 小型的b2c网站网络营销工程师是做什么的
  • 添加qq好友的超链接做网站怎么做平台推广
  • html 做网站案例简单网上软文发稿平台
  • 深圳市官方网站社区推广
  • wordpress国产主题网站seo服务公司
  • 便宜的网站设计企业网络推广方案怎么写
  • 网站特色分析图怎么做亚马逊关键词优化软件
  • js获取网站广告点击量怎么做个人如何推广app
  • 网站建设多长时间网站建设网络推广公司
  • 做海岛旅游预定网站的最好看免费观看高清视频了