如何访问测试代码内的测试套件目录路径



我需要在测试代码中访问当前测试套件目录路径。为了访问测试套件目录中的某些文件:

示例:

/home/stu/myproj/pytest
                    |_test_abc.py
                    |_perf.sh
                    |_conftest.py

我需要在test_abc.py内获取perf.sh(/home/persition/pytest/(的路径。由于我不知道将运行哪个目录py.test命令,因此用户可以将MyProj移动到任何目录。我不能使用硬编码路径。

由于测试套件目录将作为参数传递,是否有一种方式访问此目录路径并在我的测试代码(test_abc.py(中使用它。

代码下面可能会帮助您。

import os
import inspect
# find out the directory of current script
directory = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
# make a full path of the testfile(perf.sh)
the_filename = os.path.join(directory, 'perf.sh')
if os.path.isfile(the_filename):
    print 'Found the file'
else:
    print 'Failed to find the file'

最新更新