The video owner has disabled playback on external websites.
This video is no longer available on YouTube.
This video cannot be played right now.
Watch on YouTube
KI-gestützte Lerntools freischalten
Registriere dich, um leistungsstarke Tools zu nutzen, die dir helfen, schneller aus jedem Video zu lernen.
How to Write Great Unit Tests in Python
Untertitel (573 Segmente)
This video is brought to you by
Squarespace. Here's a simple code
example. This class is a weather service
that retrieves weather information from
an API, weatherapp.com.
Now, you might wonder, how do you
actually write tests for this? It's kind
of hard, right? Because this does an
HTTP request. So, you can't really test
that without actually doing the HTTP
request. This is probably part of the
code that you actually want to test
whether you're getting the correct JSON
data and that it has the uh current
object and the temperature object. So,
how do you test those? In this video,
I'm going to show you how to write tests
for code like this, what monkey patching
or mocking is and when you use it. And
I'm also going to cover a few advanced
testing strategies, as well as a few
tips to help you write better tests for
your Python code. Because writing tests
for your code might seem daunting or not
necessary, but actually software tests
are really helpful, especially once
things get serious in production. Let's
go. One of the main types of tests that
you hear a lot about is unit tests. And
what these do is that they validate the
behavior of a small isolated piece of
code. That's usually a single function
or method. These unit tests are
typically fast, deterministic, and easy
to run. Now, why would you actually
write unit test? Well, of course, they
catch bugs if you write them well. Uh
they make refactoring safer because they
provide a sort of safety net for you uh
in order to make sure that when you make
changes that things don't break that you
didn't expect to. And often that can
happen that you change something in one
part of the code but you didn't realize
it actually affects a completely
unrelated part of the code or actually
it was not as unrelated as you thought
it was. Another thing that unit tests do
is that they also document how your code
is supposed to behave. So there's sort
of a specification of the behavior of
your code and that's of course also very
close to what happens with test-driven
development where you actually write the
test before you write the code and then
the test give a specification of what
the code is supposed to do. Are unit
test the only type of test you should
do? No, of course not. In production
systems, there's plenty of other
different types of tests. for example,
end to end test to help verify complete
user workflows or security tests to help
you protect against vulnerabilities or
data consistency tests to make sure that
your data stays reliable. So, unit tests
are just one type of testing, but
they're not the whole thing. And
something else I want to mention is that
writing software test is not an excuse
to write sloppy code and then just tweak
it until it works. It's really important
that you think things through before you
actually start coding. I'm not saying
you should go full-on waterfall
methodology, but you know, use your
brain a bit and make sure you understand
how everything is supposed to fit
together. Good design is really
important and actually often leads to
code that is easier to test. So they go
hand in hand, good design, good test. If
you look at Python specifically, it
comes with a built-in unit test module.
So technically you could don't even need
anything else to start writing tests for
your Python code. However, I always
recommend using Piest. This lets you
write your tests as simple functions in
modules. It provides uh more options
with assertions. Overall, it's much more
capable and pleasant to use than unit
test in my opinion. Now let's take a
look at this weather surface class. So
if we want to test this get temperature
method, it means we need to do something
about this HTTP request because we don't
want to do that request every time we
call that method in our test code. So
one thing that you can do to avoid that
problem is by using something called
monkey patching. And that basically
means that you are dynamically replacing
a function or attribute at runtime. Why
is this called monkey patching and not
elephant patching or uh raccoon
patching? Well, I'm not really sure, but
it kind of comes from the idea that
you're sort of doing something that
you're not really supposed to be doing.
But for tests, sometimes you can't get
around it. So, how do you then write a
test? Well, here's an example of how you
could do it with Piest. So, I've created
a test file, test weather patch, because
Vollständige Untertitel im Videoplayer verfügbar
Mit Übungen trainieren
Erstelle Vokabel-, Grammatik- und Verständnisübungen aus diesem Video
Kommentare (0)
Zum Kommentieren AnmeldenRegistriere dich, um alle Features freizuschalten
Verfolge deinen Fortschritt, speichere Vokabeln und übe mit Übungen
Interaktiver Modus
Quiz
Richtige Antwort:
Ähnliche Videos
Late Night with Seth Meyers
Quiz
Richtige Antwort:
Quizfragen erscheinen beim Anschauen des Videos
Merkhilfe
Aus diesem Video
Kostenlos Sprachen lernen