python 自动加载_python自动加载类的方法(测试用例使用)

import importlib

import os

import optparse

'''

import re

from app.BaiduRand import BaiduRand

class TestLink:

@staticmethod

def test_link():

baidu = BaiduRand()

link = baidu.createRandUrl(5, "{数字3}/{数字6}")

# 366/943047

# 716/754962

# 498/386146

# 815/454851

# 591/220477

r = re.match("^\d{3}/\d{6}", link)

if r is not None:

print("验证链接格式成功100%.")

assert True

else:

print("验证链接格式失败0%.")

assert False

'''

def test_module(module_name):

module = importlib.import_module(module_name)

module_dir = dir(module)

for element in module_dir:

if (isinstance(element, object) and (element.startswith('Test'))):

print("加载类:%s()" % (element))

TestClass = getattr(module, element)

obj = TestClass()

# print(obj)

func_names = dir(TestClass)

for func_name in func_names:

if func_name.startswith('test_'):

print("执行方法:%s.%s()" % (element,func_name))

# get function

func = getattr(obj, func_name)

# call this function

func()

def main():

parse = optparse.OptionParser(usage='"usage:%prog [options] arg1,arg2"', version="%prog 1.0")

parse.add_option("-f", "--file", dest="file", type=str, metavar="file", help="请输入测试用例的类")

option, args = parse.parse_args()

class_name = ""

if option.file is None:

if len(args) > 0:

class_name = args[0]

if class_name != "":

# 单个调用

test_module("test.Test"+class_name.capitalize())

else:

# 所有用例测试

path = "./test"

for x in os.listdir(path):

if x.startswith("Test"):

file = os.path.join(path,x)

module = os.path.splitext(os.path.basename(file))[0]

module_class = "test."+module

test_module(module_class)

#test_module("test.TestLink")

if __name__ == "__main__":

main()

如果本文对您有所帮助,请支持下本站哦!!!^_^

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值