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?

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

Interesting idea. I've run into a problem discovering the test, so I may post a follow up question - Thanks - Bill

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.