How do I know if Python / Django network tests get on the network?

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

What I would like to know is is there a way I can wrap my test suite in order to detect all network traffic attempts?

As an example, for an earlier part of this application, I rewrote sys.stdout as a way to detect and catch when any submodules were writing to stdout. Is there something as simple as I can do to catch my network access attempts with my tests?

EDIT: I appreciate the quick answers. Pulling the cable is, of course, a simple solution, but I am afraid that breaking the network connection is not an option (most of the work involves SSH).

I'm a little familiar with tools like Wireshark, but I was hoping that I could intelligently integrate with the code itself. The jcollado urlopen patch could potentially be what I'm looking for, but if it doesn't look like a general network monitoring tool could be good enough.

+4
source share
2 answers

You can fix all the methods that are used to get the URL, and log every attempt, fire an event, or something more convenient for you to detect this.

Look, for example, into a library from a disus. The fix section of the urllib2.urlopen page shows an example in which urllib2.urlopen fixes can be useful. I remember that I saw a conversation (sorry, I did not find the link) from Gary Bernhardt (author of dingus), in which he showed how to drastically reduce the testing time in the django application using this technique. Thus, you can use it not only to detect access to the network, but also to avoid using the network in test cases.

+3
source

There are many tools to detect network traffic attempts.

For example netstat enter image description here

or wireshark

enter image description here

+1
source

Source: https://habr.com/ru/post/1385183/


All Articles