[TIP] Testing asynchronous method
holger at merlinux.eu
Wed Oct 19 11:25:07 PDT 2011
On Wed, Oct 19, 2011 at 00:40 +0200, Pere Martir wrote:
> On Tue, Oct 18, 2011 at 9:35 PM, Tom Davis <tom at recursivedream.com> wrote:
> >> I'm biased, but I'd strongly recommend using Twisted Trial or
> >> something else that already exists.
> > Unless you have a technical reason for not being able to depend on Trial (e.g. you literally cannot deploy Twisted), I concur. Trial is great and widely used.
> I am actually surprised that testing asynchronous methods is not
> addressed in any unit test frameworks (correct me if I am wrong),
> given that there are a lot of Python unittest alternatives/extensions
> - nose, py.test, etc. Rationale ?
You might like to check out py.test and its depdendency injection
mechanism, see here:
With it you could probably write your test function like this:
def test_async_method(self, myasync):
assert ticket == 998
here "myasync" manages asynchronous resources such as the setup of
"someasyncmodules" and also provides a helper like the "event" object
to allow a test to synchronize easily. "Myasync" could also make
sure to run event loops, determine if to re-use daemons for multiple tests,
terminate daemons etc. Depending on your actual framework the code
above might look slightly different but i hope it's possible to see
the general idea.
The other bit that some people have done with py.test is using
it with greenlets, allowing for writing synchronous tests
for async behaviour.
> I'd also like to know the other widely used asynchronous unit test framework ?
> - Twisted Trial
> - tornado.testing
> (I miss anything?)
> I don't have any experience, anyone ?
> PS: I found this question was also asked last November:
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
More information about the testing-in-python