httprunner源码

Httprunner中运用了很多Python的高级语法

1、python字典get函数返回指定键的值,指定键不存在,则返回默认值

get(key,default=None)

2、os.getcwd返回当前的工作目录

3、untils模块:将封装的接口放进untils工具类,可以直接调用

4、format字符串格式化,采用{}或:表示字符串位置

5、insert() 函数用于将指定对象插入列表的指定位置。

list.insert(index,object)

6、testcase/testsuite都有各自的run方法,引入TextTestRunner一般是为了更好对测试结果html格式控制,TextTestRunner.run方法最后执行的也还是run(case)/run(suite)

7、enumerate将一个可遍历对象(列表、元组或字符串)组合成一个索引序列,同时列出数据和下标

8、setattr用于设置属性值,属性不一定存在

setattr(str,name,value)#name为属性名称,value为设置属性的值

9、get_args获取一个函数的所有参数

10、pop方法删除字典给定键Key及对应值,返回值为被删除的值,key值必须给出,否则返回default值

pop(key[,default])

11、__all__定义了import的内容中哪些接口可以暴露出去 

12、split跟splitext主要的区别在于一个从前往后搜索字符串,一个从后往前搜索 " . ",对于文件名字的分割常用splitext,效率较高

13、闭包:是指有权访问上级父作用域的变量的函数,并且不会让函数整体因为函数的执行完毕而被销毁

14、__dict__ 表示获取对象(类)的属性

15、动态创建类

(1)定义类的两种方式:① 用class定义的类  ② 用type创建的类

(2)创建一个class对象,需要传入3个参数

  1. class的名称;
  2. 继承的父类集合,注意Python支持多重继承,如果只有一个父类,别忘了tuple的单元素写法;
  3. class的方法名称与函数绑定,这里我们把函数fn绑定到方法名hello上。

(3)type函数创建类其实与定义class一致,Python解释器遇到class类时,也是调用type函数创建类,type函数允许我们动态创建类,也就是动态语言本身支持运行期动态创建类

(4)元类:造类的类叫元类,默认所有使用class定义的类,造他们的类都是元类

(5)Hello是一个class,他的类型是type,而h是一个实例,类型就是class  Hello

class Hello(object):
    def hello(self, name='world'):
        print('Hello, %s.' % name)
 from hello import Hello
h=Hello()
h.hello()
>>Hello,world
print(type(Hello))
>><class 'type'>
print(type(h))
>><class 'hello.Hello'>

(6)控制类的创建行为,可以使用Metaclass

归结为:定义metaclass,创建类,创建实例,metaclass允许创建或修改类,可以把类看做mataclass创建出的实例

(7)关于对类的操作,setattr的原理理解辅助代码

class TestFoo(unittest.TestCase):
    def test_method_name(self):
        assert 1==1
        print('1==1')
        
TestFoo1 = type('TestFoo1',(unittest.TestCase,),{})
def test_method_name(self):
    assert 1==1
    print('1==1')
    
setattr(TestFoo1,'test_method_name',test_method_name)
TestFoo.__dict__
Out[19]: 
mappingproxy({'__module__': '__main__',
              'test_method_name': <function __main__.TestFoo.test_method_name(self)>,
              '__doc__': None})
TestFoo1.__dict__
Out[20]: 
mappingproxy({'__module__': '__main__',
              '__doc__': None,
              'test_method_name': <function __main__.test_method_name(self)>})

 16、argument模块用法:

(1)创建 ArgumentParser() 对象

(2)调用 add_argument() 方法添加参数

(3)使用 parse_args() 解析添加的参数

*执行  python   test02.py
import argparse parser = argparse.ArgumentParser() parser.add_argument("square", help="请输入一个数字", type=int) parser.add_argument("input_str", help="请输入一个字母", type=str) args = parser.parse_args() print(args.square ** 2) print(args.input_str)

 输出:

usage: test02.py [-h] square input_str
test02.py: error: the following arguments are required: square, input_str
*执行 python test02.py -h
输出
usage: test02.py [-h] square input_str

positional arguments:
  square      请输入一个数字
  input_str   请输入一个字母

optional arguments:
  -h, --help  show this help message and exit

17、惰性求值,就是延迟求值,表达式不会再他绑定到变量后立即求值,而是等到要用时才求值;python有很多方法没有直接返回一个列表,而是返回一个可迭代的generator对象,这便是惰性求值;惰性求值不要求在事先准备好所有迭代对象的取值,而是在迭代到某元素后才计算某元素,但要求在此之前或之后,该元素不能被销毁

为了创建生成器对象,可以在list comprehension两边放上(),这样它就有了惰性求值的特性

 

posted on 2020-03-12 19:49  ChanXM  阅读(825)  评论(0)    收藏  举报

导航