Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/python
import sys
with open(sys.argv[1],'r') as input_file:
print input_file.read()
--Contents of arg_file.txt--
# content of tests/conftest.py
import pytest
import os, shutil
BASEDIR = os.path.dirname(__file__)
@pytest.fixture()
def set_up(tmpdir):
"""Create tmpdir, copy arg_file over"""
tmpdir.chdir()
shutil.copyfile(os.path.join(BASEDIR, 'arg_file.txt'),
os.path.join(os.getcwd(), 'arg_file.txt'))
print('\nset_up: In directory ' + os.getcwd())
# content of tests/test_in_scriptdir.py
import subprocess, shlex, os
import pytest
BASEDIR = os.path.dirname(__file__) #this points to the tests/ directory
@pytest.mark.usefixtures('set_up')
class TestInScriptdir:
def test_1(self):
"""Execute script in script dir where my_script.py resides"""
# remember path to tmpdir
temporary = os.getcwd()
# change to script directory
os.chdir(os.path.join(BASEDIR,'..'))
print('Running in directory ' + os.getcwd())
# run script
command = './my_script.py ' + os.path.join(temporary, 'arg_file.txt')
print('Command: ' + command)
subprocess.call(shlex.split(command),
stderr=subprocess.STDOUT,
cwd=os.getcwd())
# content of tests/test_in_tmpdir.py
import subprocess, shlex, os
import pytest
BASEDIR = os.path.dirname(__file__) #this points to the tests/ directory
@pytest.mark.usefixtures('set_up')
class TestInTempdir:
def test_1(self):
"""Execute script in tmpdir where arg_file.txt resides"""
print('Running in directory ' + os.getcwd())
# run script
command = os.path.abspath(os.path.join(BASEDIR, '..', 'my_script.py')) + ' arg_file.txt'
print('Command: ' + command)
output = subprocess.call(shlex.split(command),
stderr=subprocess.STDOUT,
cwd=os.getcwd())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.