Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Testing web apps
Lecture: Getting started with tests
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Now, it's time to start writing some code
0:01
and getting our tests in place.
0:03
So, let's go and open our new project.
0:06
I've copied this over
0:07
as you can see into Chapter 14 testing.
0:09
We're going to work here.
0:11
Now, probably the first thing that we want to test
0:14
is this register thing.
0:17
We've already talked about that
0:18
it's kind of tricky. I think it's a good example.
0:20
We also have this register_get.
0:23
Now that doesn't really do anything.
0:25
Theoretically, our view model could come up
0:27
with like a list of countries
0:28
and do a bunch of cool stuff to verify that
0:31
it's going to give you drop downs and what not.
0:33
It doesn't do any of that stuff
0:34
So we're not going to test it.
0:35
We also have this index one
0:37
which doesn't really do anything, just verifies the user.
0:39
Now, that would be great, and we can test that.
0:42
But I think the most instructive part will be to test
0:45
this part of our application here.
0:47
Now we have a couple of options.
0:49
Recall, we talked about testing the view model
0:52
that would be this part
0:53
talk to about testing the view method that would be
0:56
directly calling this function.
0:58
And we've also talked about an integration test
1:01
where maybe we fire up the web app
1:02
we issue a request in this URL with a post.
1:06
And it will figure out what parameters
1:08
have to be passed to the method and so on.
1:10
So we're going to start by doing the simple thing
1:12
working with this RegisterViewModel.
1:15
Now, let's get going by creating the section for our tests.
1:18
So, we're going to create a new folder
1:20
for organizing our tests
1:21
as we already talked about, over here.
1:24
I'm going to put it next to our project.
1:28
And in here, again, we're going to do some organizations.
1:31
So I'll have account_tests, plural.
1:33
We're having more than one test.
1:35
Now we could test this with a built in unit test framework
1:38
we could build it, test it with pytest
1:39
we could test it with something like Nose
1:42
or you know, other ones.
1:43
However, in the flask documentation
1:46
all the examples are using pytest.
1:48
So we're going to use pytest as well.
1:51
So, in order to use pytest, we have to install it
1:53
and say that it's a requirement.
1:54
And actually, there's a few things we might want to use.
1:57
So I'm going to come over here.
1:58
And in our development one, this is only for dev
2:02
it's not a production thing.
2:04
I'm going to put 4 libraries, pytest
2:06
pytest-cov, webtest and clarity.
2:09
And then we're going to go down here to the terminal
2:11
and make sure that we pip install those.
2:15
All right, looks like I already have them, that's great.
2:17
So, those are installed for our project and ready for us.
2:21
When we're writing tests with pytest
2:24
what we've got to do is come over here
2:27
and just create a function and has to start with the word
2:29
test_ underscore and be a void method.
2:33
So, we can put whatever what.
2:34
Let's put an example here like this
2:36
and then we could print test test example, like so.
2:41
And then the way you test with pytest is you say assert
2:45
and then you could assert something
2:46
like one plus two equals three.
2:50
Now, how are we going to run it?
2:52
So we come over here and we'll right click
2:54
and we say run, says run pytest in there.
2:56
Now sometimes it won't say that.
2:59
Sometimes it will.
3:00
We run it, looks like it's working great.
3:02
If it doesn't, you can come over here and say Edit
3:04
add Python tests, pytests, and then swipe the script path
3:09
and then script, swipe that.
3:11
Okay, so that's what you could do
3:12
but it looks like it already discovered it for us.
3:14
So here you can see in our example.
3:16
We printed out test example
3:18
and just look at all the results.
3:19
It looks like it passed.
3:20
And you should always verify that if it's not passing
3:23
you're also detecting that.
3:25
So here you go. When I had it wrong, it says it looks like we expected three
3:30
but we got four, not the same.
3:32
Alright, so I'll put this back.
3:34
This is not really our test.
3:35
This is just a super, super simple example to show you
3:37
how to get things set up and running with pytest.
3:40
But I'll go and leave it here
3:41
for since it's a course, I wouldn't leave it in a real lab.