比如我们在函数B想要调用函数A,可以使用调用函数A的函数名,然后得到函数A的return值
但是如果调用函数A需要传入参数才能运行,或者不想再去调用整个函数A获取值,此时可以将需要调用的参数初始化,变成全局参数,这样就不需要再次运行函数A,并且不仅仅只能调用得到一个return值(一个函数只能有一个return,且只能return一个变量值)
1、普通类里面的初始化
使用_init_
def __init__(self):
self.para1 = []
|
para1即可作为类里的全局变量使用
函数使用para1时,用self.para1即可
2、pytest 的Test 开头的类下边是不能包含 _init_初始化方法的,只能用setup()进行初始化
类级别的,写在类开头
其他函数调用时:
self.sel_total.append(total)
class TestCost:
def setup_class(self): #类开始时初始化一次
self.sel_total = []
print( "---setup_class---" )
def teardown_class(self): #类运行结束运行一次
print( "\n---teardown_class---" )
def test_costList_select(self, caseinfo):
del_extract = caseinfo.pop( "extract" )
data_file = caseinfo[ 'data' ]
print( "data_file:" , data_file)
case_data = read_testcase_yaml(data_file)
print( "casedata:" ,case_data)
generator = selectHandle(caseinfo,case_data)
for i in generator:
#print( "新请求体:" ,i)
res = RequestsUtil(redload()).standard_yaml(i)
#print( '最终返回:' ,res)
total = res[ 'data' ][ 'total' ]
self.sel_total.append(total)
print( "sel_total:" , self.sel_total)
|
还有
模块级别的,写在模块开头
def setup_module():
print( "---setup_class---" )
def teardown_module():
print( "\n---teardown_class---" )
|
函数级别的,写在函数开头
def setup_function():
print( "---setup_class---" )
def teardown_function():
print( "\n---teardown_class---" )
|
方法级别的,也写在类开头
def setup_method():
print( "---setup_class---" )
def teardown_method():
print( "\n---teardown_class---" )
|