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.

Szenen-Erklärer Phrasen-Jäger Karteikarten-Review Nachsprechübung Sprachausgabe
Kostenlos registrieren
Englisch 22:57 Science & Tech

How to Write Great Unit Tests in Python

Late Night with Seth Meyers · 40,173 Aufrufe · Hinzugefügt vor 1 Monat

Untertitel (573 Segmente)

00:00

This video is brought to you by

00:01

Squarespace. Here's a simple code

00:04

example. This class is a weather service

00:06

that retrieves weather information from

00:09

an API, weatherapp.com.

00:12

Now, you might wonder, how do you

00:15

actually write tests for this? It's kind

00:17

of hard, right? Because this does an

00:18

HTTP request. So, you can't really test

00:21

that without actually doing the HTTP

00:23

request. This is probably part of the

00:26

code that you actually want to test

00:28

whether you're getting the correct JSON

00:29

data and that it has the uh current

00:31

object and the temperature object. So,

00:34

how do you test those? In this video,

00:36

I'm going to show you how to write tests

00:38

for code like this, what monkey patching

00:40

or mocking is and when you use it. And

00:42

I'm also going to cover a few advanced

00:44

testing strategies, as well as a few

00:47

tips to help you write better tests for

00:49

your Python code. Because writing tests

00:52

for your code might seem daunting or not

00:54

necessary, but actually software tests

00:56

are really helpful, especially once

00:58

things get serious in production. Let's

01:01

go. One of the main types of tests that

01:03

you hear a lot about is unit tests. And

01:06

what these do is that they validate the

01:08

behavior of a small isolated piece of

01:11

code. That's usually a single function

01:14

or method. These unit tests are

01:17

typically fast, deterministic, and easy

01:20

to run. Now, why would you actually

01:22

write unit test? Well, of course, they

01:23

catch bugs if you write them well. Uh

01:26

they make refactoring safer because they

01:28

provide a sort of safety net for you uh

01:31

in order to make sure that when you make

01:33

changes that things don't break that you

01:35

didn't expect to. And often that can

01:37

happen that you change something in one

01:40

part of the code but you didn't realize

01:42

it actually affects a completely

01:44

unrelated part of the code or actually

01:46

it was not as unrelated as you thought

01:47

it was. Another thing that unit tests do

01:50

is that they also document how your code

01:51

is supposed to behave. So there's sort

01:53

of a specification of the behavior of

01:56

your code and that's of course also very

01:58

close to what happens with test-driven

02:00

development where you actually write the

02:02

test before you write the code and then

02:04

the test give a specification of what

02:07

the code is supposed to do. Are unit

02:09

test the only type of test you should

02:11

do? No, of course not. In production

02:13

systems, there's plenty of other

02:15

different types of tests. for example,

02:17

end to end test to help verify complete

02:20

user workflows or security tests to help

02:24

you protect against vulnerabilities or

02:27

data consistency tests to make sure that

02:29

your data stays reliable. So, unit tests

02:32

are just one type of testing, but

02:34

they're not the whole thing. And

02:36

something else I want to mention is that

02:37

writing software test is not an excuse

02:39

to write sloppy code and then just tweak

02:43

it until it works. It's really important

02:45

that you think things through before you

02:47

actually start coding. I'm not saying

02:49

you should go full-on waterfall

02:51

methodology, but you know, use your

02:54

brain a bit and make sure you understand

02:55

how everything is supposed to fit

02:57

together. Good design is really

02:59

important and actually often leads to

03:01

code that is easier to test. So they go

03:04

hand in hand, good design, good test. If

03:06

you look at Python specifically, it

03:08

comes with a built-in unit test module.

03:10

So technically you could don't even need

03:12

anything else to start writing tests for

03:14

your Python code. However, I always

03:16

recommend using Piest. This lets you

03:18

write your tests as simple functions in

03:21

modules. It provides uh more options

03:24

with assertions. Overall, it's much more

03:26

capable and pleasant to use than unit

03:29

test in my opinion. Now let's take a

03:31

look at this weather surface class. So

03:34

if we want to test this get temperature

03:36

method, it means we need to do something

03:37

about this HTTP request because we don't

03:39

want to do that request every time we

03:42

call that method in our test code. So

03:45

one thing that you can do to avoid that

03:46

problem is by using something called

03:49

monkey patching. And that basically

03:51

means that you are dynamically replacing

03:54

a function or attribute at runtime. Why

03:57

is this called monkey patching and not

04:01

elephant patching or uh raccoon

04:05

patching? Well, I'm not really sure, but

04:07

it kind of comes from the idea that

04:09

you're sort of doing something that

04:11

you're not really supposed to be doing.

04:13

But for tests, sometimes you can't get

04:15

around it. So, how do you then write a

04:17

test? Well, here's an example of how you

04:19

could do it with Piest. So, I've created

04:22

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

Vokabeln & Grammatik Verständnisquiz IELTS-Prüfung Schreib-Übung
Registrieren zum Üben
Noch keine Kommentare. Sei der Erste, der seine Gedanken teilt!

Registriere dich, um alle Features freizuschalten

Verfolge deinen Fortschritt, speichere Vokabeln und übe mit Übungen

Kostenlos Sprachen lernen