Getting started with pytest Transcripts
Chapter: Test Functions
Lecture: Using test classes
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
I'd like to show you how to group test together with classes. I don't often do it but a lot of other test frameworks use classes and you
0:09
can use classes with pytest as well and some people really like to use them mostly for grouping. So I want to show you how that's done within our test
0:18
card file. We have a handful of different equality tests. Here's these three equality tests. As an example for classes.
0:29
Let's just grab these these test functions and put them in a class. So for a class we would say something like class test equality and then we can
0:44
just put our our functions there. But they need to be methods so we will indent them for one. But then they also all have to start with self.
0:54
So we'll stick a self everywhere. And I think that's it. Let's see if we can run it.
1:24
So our tests have run let's do verbose and we can see that it's a little bit of a test node, different
1:32
test node. So the test node name now has the class name in front of it and we can take that and run it individually.
1:41
So if we say pytest and then plump that whole thing down, we can run one individual, let's make sure we did that with V, yep,
1:52
ran that one individual test. So this is just sort of a grouping thing. If we can have other tests here. So if we had def test foo.
2:08
The reason why you might want to do this is just to run the class. So if we do the entire funk file,
2:14
we get everything including foo, But we can also just say I want to run all of the test equality, That one class full of tests.
2:25
So that's handy. The when we get into fixtures, we can use fixtures at the class level, too. And that's another reason to use classes.
2:32
But I just wanted to show you that. Yes, this is a way you can group tests with classes pretty easy.