Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit-testing client-server interaction in Twisted

In my latest Python project, utilizing Twisted, I've tried to be good at using the unittest module. At a high level, I'm building two RESTful APIs designed specifically to talk to each other. For most requests, I can just use DummyRequest and test the rendered values against an expected constant and that's been working fine.

However, I've got a few cases where the design requires a request on one server that (among other things) then sends a request to the other server, but doesn't really care about the response. What matters is that the request happens.

Like I said, I can test that functionality on the other side perfectly fine, but I'm getting stumped on how to test to ensure that the data was sent over. My ideas so far are either

  • Set up a dummy test server that just checks to see if the request was made and validates the input - Seems flaky and too much effort
  • Set up a decorator to wrap certain tests and modify urllib.urlopen to report when it was called, what we tried to retrieve, and allow me to simply return a known result there.

I'm leaning towards the second option as it seems more pythonic, but also a bit hacky.

Thoughts?

like image 272
Michael Pratt Avatar asked Apr 24 '26 00:04

Michael Pratt


1 Answers

Twisted comes with its own unit-testing framework, called Trial. As you might imagine, it's well-suited for testing your networking code. Here's a tutorial to start you off.

like image 97
Etaoin Avatar answered Apr 25 '26 14:04

Etaoin