[Python-de] Code testen, der twisted.web.client.getPage() verwendet

Hartmut Goebel h.goebel at goebel-consult.de
Mi Jul 2 09:21:20 CEST 2014


Am 30.06.2014 07:05, schrieb Karsten Schulz:
> für die Unittests würde ich mit Hilfe von mock
> (<https://pypi.python.org/pypi/mock>, ab Python 3.3 im Standard) die
> Funktion twisted.web.client.getPage patchen und - wenn nötig - per
> side_effect Strings zurückgeben.

Herzlichen dank für den Tipp (auch an Stefan). Ich hatte schon
Code-Beispiele gesehen, die mock benutzen, aber nicht gewusst, dass das
in die Std-Lib geht – darum wollte ich es vermeiden. Aber damit geht das
super einfach.

Ich verwende mock.patch als Context-Manager. Hier ein Beispiel und unten
die Funktionen, die ich dafür verwende. (Mögen sie andern nützlich sein.)

class TextInvalidDescriptionXML(DescriptionNotFound):
    """
    Same as DescriptionNotFound, except now we pass a
    description which is invalid XML. Results should be the same,
    except for scpdXML.
    """

    def setUp(self):
        with mock.patch('coherence.upnp.core.utils.getPage',
                        fakeGetPage('<x>')):
            self.setUp_main()


def raiseError(url):
    "Behaves as if the file was not read."
    def _raiseError(*args): raise Exception('Meaningless Error')
    d = Deferred()
    d.addCallback(_raiseError)
    return d

def fakeGetPage(content):
    def returnEmptyPage(url):
        "Behaves as if the file contains `content`."
        d = Deferred()
        d.callback((content, {}))
        return d
    return returnEmptyPage

Und für die ursprüngliche Frage, damit einen extra-simplen "Web-Server"
zu implementieren:

def fakeGetPageURL(url):
    """
    Returns the
    content of the file with the name taken from the final component
    of a url-path.

    Example:
      http://1.2.3.4/a/b/c/some.xml -> <module-dir>/some.xml
    """
    path = urlparse.urlparse(url).path
    path = posixpath.normpath(path)
    words = path.split('/')
    file = FilePath(os.path.join(FILE_BASE, words[-1]))
    d = Deferred()
    d.callback((file.open().read(), {}))
    return d


-- 
Schönen Gruß
Hartmut Goebel
Dipl.-Informatiker (univ), CISSP, CSSLP
Information Security Management, Security Governance, Secure Software
Development

Goebel Consult, Landshut
http://www.goebel-consult.de

Blog: http://www.goebel-consult.de/blog/kritik-an-change.org-datensammler
Kolumne: http://www.cissp-gefluester.de/2010-07-passwoerter-lieben-lernen

Goebel Consult ist Mitglied bei http://www.7-it.de/

-------------- nächster Teil --------------
Ein Dateianhang mit HTML-Daten wurde abgetrennt...
URL: <http://mail.python.org/pipermail/python-de/attachments/20140702/d7ab33f5/attachment.html>
-------------- nächster Teil --------------
Ein Dateianhang mit Binärdaten wurde abgetrennt...
Dateiname   : smime.p7s
Dateityp    : application/pkcs7-signature
Dateigröße  : 3883 bytes
Beschreibung: S/MIME Cryptographic Signature
URL         : <http://mail.python.org/pipermail/python-de/attachments/20140702/d7ab33f5/attachment.bin>


Mehr Informationen über die Mailingliste python-de