[Tutor] Help error 504

Peter Otten __peter__ at web.de
Wed Aug 26 11:43:41 CEST 2015


Danny Yoo wrote:

> Unit tests that depend on external dependencies can be "flaky": they
> might fail for reasons that you don't anticipate.  I'd recommend not
> depending on an external web site like this: it puts load on someone
> else, which they might not appreciate.

If you have a well-defined error like an exception raised in a specific 
function by all means write a unit test that ensures that your code can 
handle it. Whether you use unittest.mock or the technique you describe below 
is a matter of taste.

Even if your ultimate goal is a comprehensive set of unit tests a tool like 
httbin has its merits as it may help you find out what actually happens in 
real life situtations. 

Example: Will a server error raise an exception in urlopen() or is it 
sometimes raised in the request.read() method?

Also, mocking can give you a false sense of security. coverage.py may report
100% coverage for a function like

def process(urlopen=urllib.request.urlopen):
    result = []
    for url in [1, 2]:
        try:
            r = urlopen(url)
            result.append(r.read())
        except urllib.error.HTTPError:
            pass
    return result

which will obviously fail once you release it into the wild.



More information about the Tutor mailing list