当前位置:网站首页>(六)装饰器扩展之[email protected]使用原理
(六)装饰器扩展之[email protected]使用原理
2022-07-24 02:37:00 【覆水难收呀】
(一)@pytest.mark.XXX是怎么实现的?
玩过接口自动化的童鞋都用过pytest,基本都是用装饰器实现的,上几个文章我们讲了装饰器的使用原理,这次我们拓展一个新的知识点:@pytest.mark.XXX这个实现原理是什么?
import pytest
@pytest.mark.skip
def test():
pass我们举个例子 :
@pytest.mark.skip 实际上就是使用pytest模块里面的mark这个对象.skip方法
@模块.对象.方法
(二)我们来自己实现一个这种形式的装饰器
(1)我们新建一个lemon.py文件,创建一个Mark类,里面写了skip函数,skip函数里面有一个嵌套函数wrapper。

(2)再建一个demo1_装饰器扩展.py文件, 创建一个test2方法,然后@lemon.mark.skip, 最后在下面调用test2()试试。

打印结果:
----skip----1
------test2------
----skip----2
下面上源码给大家自己去试一下
from py09_07day import lemon
class Mark:
def skip(self, func):
def wrapper(*args, **kwargs):
print("----skip----1")
res = func()
print("----skip----2")
return res
return wrapper
mark = Mark()
# @lemon.mark.skip ===> @lemon这个类.mark对象.方法
# @skip test2 = skip(test2)
@lemon.mark.skip
def test2():
print('------test2------')
test2()
# 打印结果
----skip----1
------test2------
----skip----2边栏推荐
- Tutoriel sur l'utilisation de la ligne de temps unitaire
- Combined with actual combat, analyze gb/t 28181 (II) -- equipment directory synchronization
- Rylstim Screen Recorder
- Programmers can't JVM? Ashes Engineer: all waiting to be eliminated! This is a must skill!
- [diary of supplementary questions] [2022 Hangdian summer school 1] c-backpack
- js传参时传入 string有数据;传入 number时没有数据;2[0]是对的!number类型数据可以取下标
- Mysql数据库,排序与单行处理函数篇
- Digital transformation behind the reshaping growth of catering chain stores
- Dynamically set the navigationbartitletext of the applet
- Unity TimeLine使用教程
猜你喜欢

Ggplot2 displays png

ssm的求职招聘系统兼职应聘求职

Digital transformation behind the reshaping growth of catering chain stores

Detailed vector

分享一个基于Abp 和Yarp 开发的API网关项目

Diablo king, analysis of low illumination image enhancement technology

Use the hiflow scene connector to view the epidemic situation in the region every day

Composition API (in setup) watch usage details

Research and analysis of the third-party dependency library Ag grid

关于缺少编程基础的朋友想转行 ABAP 开发岗提出的一些咨询问题和解答
随机推荐
Vscade connects to the server. The password is correct, but it has been unable to connect
Sharing a case of controller restart caused by a CIFS bug in NetApp Fas series
Camper recruitment | AI youth with the world in mind, the United Nations needs your help for sustainable development!
Chinese scientists have made new progress in high security quantum key distribution networks
让生活充满幸福感
Detailed vector
Force open web page
[FPGA tutorial case 38] communication case 8 - serial parallel serial data transmission based on FPGA
The practical influence of educational robot on students' learning effect
Share two interesting special effects
分享一个基于Abp 和Yarp 开发的API网关项目
Rylstim Screen Recorder
理解加载class到JVM的时机
Réalisation d'un diagramme de zone de ligne brisée - Diagramme Rose - Diagramme à barres
Go基础笔记_5_数组切片
Redis data type concept
wallys/WiFi6 MiniPCIe Module 2T2R2 × 2.4GHz 2x5GHz MT7915 MT7975
Maximize, minimize, restore, close and move the WinForm form form of C #
Go basic notes_ 5_ Array slice
【补题日记】[2022牛客暑期多校2]K-Link with Bracket Sequence I