0

I need to access current test suite Directory path inside the test code. So as to access some files in the test suite directory:

Example:

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

I need to get the path of perf.sh(/home/student/pytest/) inside the test_abc.py. Since i do not know from which directory py.test command will be run, and the user can move(git clone) myproj to any directory. I can't use hardcoded path.

since the test suite directory will be passed as parameter , is there a way access this directory path and use it inside my test code (test_abc.py).

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Niranjan M.R
  • 343
  • 1
  • 6
  • 23
  • This is a duplicate indeed. Please see my reply to https://stackoverflow.com/questions/3430372/how-to-get-full-path-of-current-files-directory-in-python/50098973#50098973 – Ron Kalian Jun 07 '18 at 10:03

1 Answers1

-1

Below code may help you.

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'
H. Jang
  • 338
  • 3
  • 14