Skip to content

Instantly share code, notes, and snippets.

@lbillingham
Created March 30, 2016 11:41
Show Gist options
  • Save lbillingham/c9bca4e411a265a52457ed5788adb444 to your computer and use it in GitHub Desktop.
Save lbillingham/c9bca4e411a265a52457ed5788adb444 to your computer and use it in GitHub Desktop.
python sub process test (blocking IO) works on py 2.6 on _old_ server
import inspect
import os
import subprocess
import unittest
class TestAThing(unittest.TestCase):
def test_this_file_seen_by_ls(self):
proc_ls = subprocess.Popen(['/bin/bash'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
ls_output_from_stdout, errs = proc_ls.communicate('ls -lash')
name_of_this_file = os.path.basename(__file__)
this_file_seen_by_ls = name_of_this_file in ls_output_from_stdout
self.assertTrue(name_of_this_file in ls_output_from_stdout)
def test_that_will_fail(self):
self.fail()
def test_that_always_passes(self):
proc_hostname = subprocess.Popen(['/bin/bash'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
hostname_from_stdout, errs = proc_hostname.communicate('hostname')
this_func_name_got_evilly = inspect.stack()[0][3]
subprocess.Popen(['cat'], stdin=subprocess.PIPE).communicate('''
Hello, I'm a multiline-string
being passed to `cat` on stdout
simliar to a heredoc.
also, I got this from running
`hostname`
{0}
in method `{1}`\n
'''.format(hostname_from_stdout, this_func_name_got_evilly))
self.assertTrue(True)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment