00:0.14 Now that you've seen the app and have some idea of the library that we're going
00:3.72 to test. It's time to actually get things set up for running unit tests.
00:8.99 See the test runner, make sure we have our requirements set up correctly for that
00:13.03 as well. There's a couple of things we can do that will make working with
00:17.36 the test runner as well as PyCharm a little bit better around the specific naming
00:21.41 So what I'm going to do is create a folder or we're gonna put our
00:25.5 test and I'm going to name it.
00:28.98 Test plural when we do that,
00:32.17 we can come over here and right click and say run 'pytest' in tests.
00:38.84 If I were to click on app,
00:40.86 I don't get any run if I click on a file,
00:43.71 I just get run that file.
00:45.06 But because it's called tests, PyCharm knows that it's going to look here for
00:50.5 running the tests Now, how does it,
00:52.92 know 'pytest', There's other types of testing framework,
00:55.49 including the built in unit tests.
00:57.64 But let's go check that out really quick.
00:59.48 If we go down here to tools Python integrated tools,
01:2.79 you can set up the default test runner and notice it can auto detect it can
01:7.07 be 'pytest'  'nosetest' 'twisted trial' or the built in unit tests.
01:11.24 I recommend as long as you don't mind having external dependencies using py test.
01:15.73 If you want to avoid dependencies,
01:17.59 use unit test, 'pytesters' a decent option because even if things are built with unit tests
01:21.52 you can still run them by running 'py test' against it.
01:25.24 Okay, So that's why we saw pytest and not some other test framework run
01:29.32 right click and around there for this to work.
01:31.61 Of course we're going to have to have pytest in our requirements and make sure
01:35.35 that we have. pytest installed,
01:39.64 luckily we already do. But if for some reason you don't make sure that you've
01:42.96 installed pytest does not come with python,
01:45.09 but it is clearly the most popular way to write tests in python.
01:50.34 We'll go down here. If we ran run test,
01:52.37 it wouldn't do anything. So let's go and add some files again.
01:55.84 Naming matters here. So this is going to be table tests.
01:59.16 I would love to say tests,
02:1.06 url because this file chances are is going to more than one test.
02:6.14 Let's go ahead and try that and just see what happens.
02:8.44 So we can come down here in the way we write a pytest test is
02:12.31 we just write the name. So the first test that we're gonna write is test
02:17.04 hello_pytest( ) or something like that.
02:20.64 In in here we can just assert one is greater than zero or something.
02:25.18 This is how you write tests or pytest.
02:27.86 It has the name. test_ and the method here other than that you can
02:32.64 name it whatever descriptive thing you want and then you just assert things and pytest
02:37.24 hooks into the assert infrastructure here and runs a test.
02:40.81 So let's go and try to run our tests,
02:43.84 sadly no tests found what's going on here.
02:46.87 Well, I wanted to show you that because I'm sure at some point you run
02:49.07 into it. The problem is that these have to be named test singular,
02:53.2 even though they contain multiple tests,
02:55.64  All right,
02:56.82 try it again. There we go.
02:59.14 So our test is running down here.
03:1.07 You see, we got the test.
03:2.51 Hello pytest Past 100%.
03:6.74 Perfect. If we wanted to see what it looks like if it failed,
03:9.24 we could change our assert to something that's false and you can see it's said over
03:13.95 here this this is not true.
03:16.84 Right So that's the problem.
03:18.74 There's a lot of other interesting things we can test with pytest,
03:21.48 but that's a start right now.
03:24.12 If we come down here notice we've got this cool test runner.
03:28.0 It says okay, these are all the test results.
03:31.64 Here's the table tests. Hello pytest.
03:34.37 Let me actually rename this one to.
03:38.74 'hello_test'. All right.
03:41.85 Again. That way we can have our table tests as well and we'll see multiple
03:45.77 things going on down here to this test.
03:47.69 Runner is really fantastic notice when we first right,
03:50.74 click here and said run pytest in tests that created a run configuration up there
03:57.54 and now I can just hit command R or rather or just press this button like
04:3.3 we have been. Additionally, you can run down here and that means the same
04:6.98 thing. But we can hide,
04:9.39 let's have a failing test. We run this now.
04:18.79 You can see we can hide just the hide the passing tests and just show the
04:23.67 failing tests right now. One thing that's really cool about this is we can not
04:27.54 just run the test, but if we've got a bunch we can run just the
04:31.16 failed ones. So maybe you've got 1000 tests and there's just one that's failing.
04:34.72 You're trying to fix it. Do you really want to wait for all 999 of
04:38.63 the other ones to run to tell you if the progress you're making on the one
04:41.53 test works. No switch to this now if you hit CTRL+R we can reset
04:46.28 it over there. But if you press this and you just say rerun the failed
04:49.97 ones. Rerunning this will just keep running the failed one.
04:53.1 So a full project rerun is what you need.
04:57.24 We can sort alphabetically, we can sort by duration.
05:0.66 Notice these are fast but if it took a little while it will show you how
05:4.55 long it takes. You can expand collapse and so on.
05:7.91 Let's go rerun it to get our other non failing run back.
05:11.25 Beautiful, beautiful. One other thing that is quite interesting here is this we can
05:16.75 actually toggle the test to run all the time.
05:19.66 We'll see that later. But yeah,
05:21.55 this test runner down here is really fantastic.
05:24.84 We're going to be using it while we're working on these various tests.
