user1592380
user1592380

Reputation: 36205

How to capture ouput from python unittest

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?

Upvotes: 0

Views: 83

Answers (2)

Jimilian
Jimilian

Reputation: 3919

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.

Upvotes: 1

Aaron Digulla
Aaron Digulla

Reputation: 328556

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?

Upvotes: 1

Related Questions