unittest and threading

R

Ross Boylan

Is it safe to use unittest with threads?

In particular, if a unit test fails in some thread other than the one
that launched the test, will that information be captured properly?

A search of the net shows a suggestion that all failures must be
reported in the main thread, but I couldn't find anything definitive.

If it matters, I'm using CPython 2.7.

Thanks. If you're using email, I'd appreciate a cc.
Ross Boylan
 
S

Steven D'Aprano

Is it safe to use unittest with threads?

I see nobody else has answered, so I'll have a go.

I think you need to explain what you mean here in a little more detail.

If you mean, "I have a library that uses threads internally, and I want
to test it with unittest", then the answer is almost certainly yes it is
safe.

If you mean, "I want to write unit tests which use threads as part of the
test", then the answer again remains almost certainly yes it is safe.

Provided, of course, that your test code is not buggy. Tests, being code,
are not immune to bugs, and the more complex your tests, the more likely
they contain bugs.

Lastly, if you mean, "I want to execute each unit test in a separate
thread, so that all my tests run in parallel instead of sequentially",
then the answer is that as far as I know the unittest framework does not
support this.

You would have to write your own framework. You might be able to inherit
some of the behaviour from the unittest module, but all the threading
would be up to you. So only you will know whether it will be safe or not.

Alternatively, you could try the nose or py.test frameworks, which I
understand already support running tests in parallel.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,150
Messages
2,570,853
Members
47,394
Latest member
Olekdev

Latest Threads

Top