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