Effective PyCharm Transcripts
Chapter: Unit testing
Lecture: Running pytest tests

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now that you've seen the app and have some idea of the library that we're going
0:04 to test. It's time to actually get things set up for running unit tests.
0:09 See the test runner, make sure we have our requirements set up correctly for that
0:14 as well. There's a couple of things we can do that will make working with
0:18 the test runner as well as PyCharm a little bit better around the specific naming So what I'm going to do is create a folder or we're gonna put our
0:26 test and I'm going to name it. Test plural when we do that, we can come over here and right click and say run 'pytest' in tests.
0:39 If I were to click on app, I don't get any run if I click on a file, I just get run that file.
0:46 But because it's called tests, PyCharm knows that it's going to look here for running the tests Now, how does it,
0:53 know 'pytest', There's other types of testing framework, including the built in unit tests. But let's go check that out really quick.
1:00 If we go down here to tools Python integrated tools, you can set up the default test runner and notice it can auto detect it can
1:08 be 'pytest' 'nosetest' 'twisted trial' or the built in unit tests. I recommend as long as you don't mind having external dependencies using py test.
1:16 If you want to avoid dependencies, use unit test, 'pytesters' a decent option because even if things are built with unit tests
1:22 you can still run them by running 'py test' against it. Okay, So that's why we saw pytest and not some other test framework run
1:30 right click and around there for this to work. Of course we're going to have to have pytest in our requirements and make sure
1:36 that we have. pytest installed, luckily we already do. But if for some reason you don't make sure that you've
1:43 installed pytest does not come with Python, but it is clearly the most popular way to write tests in Python. We'll go down here. If we ran run test,
1:53 it wouldn't do anything. So let's go and add some files again. Naming matters here. So this is going to be table tests. I would love to say tests,
2:02 url because this file chances are is going to more than one test. Let's go ahead and try that and just see what happens.
2:09 So we can come down here in the way we write a pytest test is we just write the name. So the first test that we're gonna write is test
2:18 hello_pytest( ) or something like that. In in here we can just assert one is greater than zero or something. This is how you write tests or pytest.
2:28 It has the name. test_ and the method here other than that you can
2:33 name it whatever descriptive thing you want and then you just assert things and pytest hooks into the assert infrastructure here and runs a test.
2:41 So let's go and try to run our tests, sadly no tests found what's going on here. Well, I wanted to show you that because I'm sure at some point you run
2:50 into it. The problem is that these have to be named test singular, even though they contain multiple tests, All right, try it again. There we go.
3:00 So our test is running down here. You see, we got the test. Hello pytest Past 100%. Perfect. If we wanted to see what it looks like if it failed,
3:10 we could change our assert to something that's false and you can see it's said over here this this is not true. Right So that's the problem.
3:19 There's a lot of other interesting things we can test with pytest, but that's a start right now.
3:25 If we come down here notice we've got this cool test runner. It says okay, these are all the test results. Here's the table tests. Hello pytest.
3:35 Let me actually rename this one to. 'hello_test'. All right. Again. That way we can have our table tests as well and we'll see multiple
3:46 things going on down here to this test. Runner is really fantastic notice when we first right,
3:51 click here and said run pytest in tests that created a run configuration up there
3:58 and now I can just hit command R or rather or just press this button like we have been. Additionally, you can run down here and that means the same
4:07 thing. But we can hide, let's have a failing test. We run this now. You can see we can hide just the hide the passing tests and just show the
4:24 failing tests right now. One thing that's really cool about this is we can not just run the test, but if we've got a bunch we can run just the
4:32 failed ones. So maybe you've got 1000 tests and there's just one that's failing. You're trying to fix it. Do you really want to wait for all 999 of
4:39 the other ones to run to tell you if the progress you're making on the one test works. No switch to this now if you hit CTRL+R we can reset
4:47 it over there. But if you press this and you just say rerun the failed ones. Rerunning this will just keep running the failed one.
4:54 So a full project rerun is what you need. We can sort alphabetically, we can sort by duration.
5:01 Notice these are fast but if it took a little while it will show you how long it takes. You can expand collapse and so on.
5:08 Let's go rerun it to get our other non failing run back. Beautiful, beautiful. One other thing that is quite interesting here is this we can
5:17 actually toggle the test to run all the time. We'll see that later. But yeah, this test runner down here is really fantastic.
5:25 We're going to be using it while we're working on these various tests.


Talk Python's Mastodon Michael Kennedy's Mastodon