Python的点点滴滴(filesystem paths)

来源:互联网 发布:网络很火的粤语歌 编辑:程序博客网 时间:2024/05/16 17:42

测试用例的Script要动态加载,这涉及到文件系统中可用测试用例的遍历问题。在Python3.4中,提供了一个OO的路径管理工具pathlib,可以使用pathlib来完成列出系统可用测试用例集的功能。

1. 继承关系:

在Linux系统中,使用的是PosixPath。

2. 用到的几个方法与属性:

首先,构造一个PosixPath对象:

import pathlib

casesPath = pathlib.Path( './cases' )#这是使用了相对路径

过滤该目录下非py的文件:

cases = list( casesPath.glob( '*.py' )

列出有效的测试用例:

i = 0
for c in cases:
    print( '[' + str(i) + ']' + '\t' + c.name )
    i += 1

输入测试用例的索引:

index = input( 'Please input the test case index[ e.g., 0 for ' + cases[0].name + ' ]:\n' )

动态导入该测试用例:
case   =   importlib.import_module( 'cases.' + cases[ int( index )].stem )
case.run()

0 0