4

I was recently added to a project to add test coverage (Python/Django unittest module). This app deals heavily with web APIs and JSON requests and such, and part of my job is to make sure that none of the existing or future tests require live network data to function.

What I'd like to know is - is there a way I can wrap my test suite to detect all attempts at network traffic?

As an example, for an earlier portion of this app I overwrote sys.stdout as a way to detect and catch when any submodules wrote to stdout. Is there something as simple I can do to catch attempts at network access by my tests?

EDIT: I appreciate the quick responses. Pulling the cable is of course a simple solution, but I'm afraid breaking my network connection isn't an option (much of the work is over SSH).

I am somewhat familiar with tools like Wireshark but I was hoping for something that could be intelligently integrated with the code itself. jcollado's urlopen patch could potentially be what I'm looking for, but if not it looks like a general network monitor tool might be good enough.

Josh
  • 179
  • 1
  • 9
  • You can tell if existing tests are hitting the network by pulling the network connection and running them. – johnsyweb Dec 07 '11 at 18:53
  • Can you use grep to find all the places where a network connection is created? They're not surprises. They're usually painfully obvious because they involve `import` and an appropriate library like `urllib`. What's stopping you from using search? – S.Lott Dec 07 '11 at 19:09
  • 1
    I'm not the only one working on this code, and if possible I'd like to avoid having to change the code that's being tested or require the other developers to change theirs. – Josh Dec 07 '11 at 20:47
  • Have you considered copying the code to another machine? Unit tests should run independently of filesystems, databases and networks even when the production code doesn't. – johnsyweb Dec 08 '11 at 19:31
  • There's [a different approach to this over here](http://stackoverflow.com/questions/18601828/python-block-network-connections-for-testing-purposes). – mlissner Oct 30 '15 at 16:02

2 Answers2

3

You could patch all methods that are used to get a url and log every attempt, fire an event or whatever it's more convenient for you to detect that.

Look for example at dingus mocking library. In the patching section in the pypi page, there's an example that patches urllib2.urlopen that can be useful. I remember I saw a talk (sorry, I don't find the link) from Gary Bernhardt (dingus author) in which he showed how to drastically reduce testing time in a django application by using this technique. So you can use it not only to detect network access, but also to avoid using the network in your test cases.

jcollado
  • 39,419
  • 8
  • 102
  • 133
1

There are many tools to detect attempts at network traffic.

For example netstat output enter image description here

or wireshark

enter image description here

Alexey Savanovich
  • 1,893
  • 11
  • 19