【问题标题】:How to access test suite directory path inside the test code [duplicate]如何访问测试代码中的测试套件目录路径[重复]
【发布时间】:2018-10-04 08:36:02
【问题描述】:

我需要访问测试代码内的当前测试套件目录路径。以便访问测试套件目录中的一些文件:

例子:

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

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

由于测试套件目录将作为参数传递,有没有办法访问此目录路径并在我的测试代码(test_abc.py)中使用它。

【问题讨论】:

标签: python pytest


【解决方案1】:

以下代码可能对您有所帮助。

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'

【讨论】:

    猜你喜欢
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-08
    • 2012-10-19
    • 1970-01-01
    相关资源
    最近更新 更多