0

I have a python functional test which I am running from another function using:

unittest.main(module=ft1.y1, argv=sys.argv[:1], exit=False)

I would like to capture the output into a variable.

I've tried

out = unittest.main(module=ft1.y1, argv=sys.argv[:1], exit=False)
print 'out ' + str(out)

this results in a response of

out

What am I doing wrong?

user1592380
  • 34,265
  • 92
  • 284
  • 515

2 Answers2

1

You need to redirect sys.stdout to a string buffer.

See the answers of this question for a couple of ways to do this: Can I redirect the stdout in python into some sort of string buffer?

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
1

Why you run your unit test from another function? Why you don't use standard runner? For example

  class MyTestCase(unittest.TestCase):
      ...
      # tests

  suite = unittest.TestLoader().loadTestsFromTestCase(MyTestCase)

  unittest.TextTestRunner(verbosity=2).run(suite)

p.s. I had write this post as answer, because I didn't have enough points to write it as comment.

Jimilian
  • 3,859
  • 30
  • 33
  • Interesting idea. I've run into a problem discovering the test, so I may post a follow up question - Thanks - Bill – user1592380 Feb 04 '14 at 18:44