Wie kann ich einen Unit -Test für Funktionen schreiben, der eine Netzwerkanforderung erstellt, ohne die Schnittstelle zuPython

Python-Programme
Anonymous
 Wie kann ich einen Unit -Test für Funktionen schreiben, der eine Netzwerkanforderung erstellt, ohne die Schnittstelle zu

Post by Anonymous »

Ich habe das gelesen < /p>

Code: Select all

Unit tests run fast.
If they don’t run fast, they aren’t unit tests.

A test is not a unit test if
1. It talks to a database.
2. It communicates across a network.
3. It touches the file system.
4. You have to do special things to your [url=viewtopic.php?t=25360]environment[/url] (such as editing configuration files) to run it.

in Working Effectively with legacy code (book).
< /code>
Ich habe eine Funktion, die den Reißverschluss aus dem Internet herunterlädt und ihn dann in ein Python-Objekt für eine bestimmte Klasse konvertiert.import typing as t

def get_book_objects(date: str) -> t.List[Book]:
# download the zip with the date from the endpoint
res = requests.get(f"HTTP-URL-{date}")

# code to read the response content in BytesIO and then use the ZipFile module
# to extract data.

# parse the data and return a list of Book object
return books
Nehmen wir an, ich möchte einen Unit -Test für die Funktion Get_Book_Objects schreiben. Wie soll ich dann einen Unit -Test schreiben, ohne eine Netzwerkanforderung zu stellen? Ich meine, ich bevorzuge das Lesen von Dateisystemen auf eine Netzwerkanforderung, da es viel schneller ist, als eine Anfrage an das Netzwerk zu stellen, obwohl ein guter Unit-Test das Dateisystem auch nicht berührt. Unit -Testfunktion.
Was werden Sie tun, um einen guten Unit -Test in dieser Art von Situation zu schreiben?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post