[Twisted-Python] Using Twisted with pytest fixtures

meejah meejah at meejah.ca
Mon Jul 17 12:08:09 MDT 2017


Hamza Sheikh <fehrist at codeghar.com> writes:

> From what I understand -- forgive my beginner-level grasp of the
> subject matter -- once the reactor starts no more listenTCP() or
> connectTCP() (for example) can be added.

You can call these methods after the reactor starts with no problems.

> And until the reactor stops code execution does not progress beyond
> ``reactor.run()``.

Yes, this is true.

> * How can I use Twisted that fits the way my tests are written?
> * If I have to rewrite my tests to fit the Twisted model, do I start the
>   reactor every time for each test and stop it at the end?

No, I would use a "session"-scoped "reactor" fixture that starts the
reactor once, and stops it once. This is also what the function
twisted.internet.task.react does for you (but probably not appropriate
for pytest tests).

> * Are there any known issues with start/stop reactor multiple times in a
>   single, long-running process?

You cannot re-start the reactor.

> * Are there any good examples of using Twisted in integration tests that I
>   have failed to find?

I have a blog post on this, outlining some integration tests that I
wrote for Tahoe-LAFS (which uses Twisted) and which also starts a
25-node Tor test network.

   https://meejah.ca/blog/pytest-integration-testing

You probably also want the pytest-twisted extension for pytest.  I'm
often around in #twisted or #tahoe-lafs on Freenode IRC if you'd like to
discuss.

-- 
meejah




More information about the Twisted-Python mailing list