Skip to content

Commit

Permalink
Clean up test_return_code tests slightly
Browse files Browse the repository at this point in the history
This is a refinement on the previous commit to reduce unecessary changes
to the functional tests in the test_return_codes module. Mainly always
decoding the output from the subprocess for testing broken things
unexpectedly when a bytes object was expected.
  • Loading branch information
mtreinish committed Nov 24, 2019
1 parent 4196953 commit e68b506
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions stestr/tests/test_return_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,21 @@ def _add_dict(test):
self.assertThat(len(tests), testtools.matchers.GreaterThan(0))

def assertRunExit(self, cmd, expected, subunit=False, stdin=None):
env = os.environ
if stdin:
p = subprocess.Popen(
"%s" % cmd, shell=True, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
env=env)
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate(stdin)
else:
p = subprocess.Popen(
"%s" % cmd, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
env=env)
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
if not subunit:
out = out.decode('utf8')
err = err.decode('utf8')
self.assertEqual(
p.returncode, expected,
"Stdout: %s; Stderr: %s" % (out, err))
"Stdout: %s; Stderr: %s" % (out.decode('utf8'),
err.decode('utf8')))
return (out, err)
else:
self.assertEqual(p.returncode, expected,
Expand Down Expand Up @@ -239,10 +235,8 @@ def test_list(self):
self.assertRunExit('stestr list', 0)

def _get_cmd_stdout(self, cmd):
env = os.environ
p = subprocess.Popen(cmd, shell=True,
stdout=subprocess.PIPE,
env=env)
stdout=subprocess.PIPE)
out = p.communicate()
self.assertEqual(0, p.returncode)
return out
Expand Down Expand Up @@ -297,14 +291,14 @@ def test_load_force_init_invalid(self):
def test_load_from_stdin_quiet(self):
out, err = self.assertRunExit('stestr --user-config stestr.yaml -q '
'run passing', 0)
self.assertEqual(out, '')
self.assertEqual('', out.decode('utf-8'))
# FIXME(masayukig): We get some warnings when we run a coverage job.
# So, just ignore 'err' here.
stream = self._get_cmd_stdout('stestr last --subunit')[0]
out, err = self.assertRunExit('stestr --user-config stestr.yaml -q '
'load', 0, stdin=stream)
self.assertEqual(out, '')
self.assertEqual(err, '')
self.assertEqual(out.decode('utf-8'), '')
self.assertEqual(err.decode('utf-8'), '')

def test_no_subunit_trace_force_subunit_trace(self):
out, err = self.assertRunExit(
Expand Down Expand Up @@ -394,29 +388,29 @@ def test_list_from_func(self):
def test_run_no_discover_pytest_path(self):
passing_string = 'tests/test_passing.py::FakeTestClass::test_pass_list'
out, err = self.assertRunExit('stestr run -n %s' % passing_string, 0)
lines = out.splitlines()
lines = out.decode('utf8').splitlines()
self.assertIn(' - Passed: 1', lines)
self.assertIn(' - Failed: 0', lines)

def test_run_no_discover_pytest_path_failing(self):
passing_string = 'tests/test_failing.py::FakeTestClass::test_pass_list'
out, err = self.assertRunExit('stestr run -n %s' % passing_string, 1)
lines = out.splitlines()
lines = out.decode('utf8').splitlines()
self.assertIn(' - Passed: 0', lines)
self.assertIn(' - Failed: 1', lines)

def test_run_no_discover_file_path(self):
passing_string = 'tests/test_passing.py'
out, err = self.assertRunExit('stestr run -n %s' % passing_string, 0)
lines = out.splitlines()
lines = out.decode('utf8').splitlines()
self.assertIn(' - Passed: 2', lines)
self.assertIn(' - Failed: 0', lines)
self.assertIn(' - Expected Fail: 1', lines)

def test_run_no_discover_file_path_failing(self):
passing_string = 'tests/test_failing.py'
out, err = self.assertRunExit('stestr run -n %s' % passing_string, 1)
lines = out.splitlines()
lines = out.decode('utf8').splitlines()
self.assertIn(' - Passed: 0', lines)
self.assertIn(' - Failed: 2', lines)
self.assertIn(' - Unexpected Success: 1', lines)

0 comments on commit e68b506

Please sign in to comment.